5 Difference between EnumMap and EnumSet in Java

If you are curious about EnumMap and EnumSet classes in java collections framework then this article is for you. Questions related to the EnumMap and EnumSet are generally not asked in the java developer interview. That includes the question difference between EnumMap and EnumSet. But it does not mean that you avoid these classes altogether. This article will cover the difference between EnumMap and EnumSet, EnumMap example, EnumSet example and similarities between them.   

Read Also: Difference between EnumMap and HashMap in Java

Difference between EnumMap and EnumSet in Java

1. Internal representation :EnumMap internally represented as arrays.EnumSet internally represented as BitVector.

8 Best Java Decompilers in 2019

What is Java Decompiler ?

Java decompiler converts .class file into java source code. In other words, decompiler converts bytecode into readable .java format.You can use java decompiler to look into the code of predefined java classes like HashMap, HashSet , ArrayList etc. You can find out how Sun/Oracle java developers wrote the java classes which are currently used by billion of devices in the world.


Java source code from which the .class file has been generated will not exactly match the java source code generated from the java decompiler. But most of the code will be same.


Read Also :  Java 8 Features

After installing java, you can find the predefined java classes in the rt.jar inside JRE folder. Unzip rt.jar and all the predefined java classes will be present in the bytecode(.class) format. By using java decompiler, I have written the articles how HashMap works in java, how HashSet works in java. So let's find out best java decompilers which I have used and now I am recommending it to you. 

Best Java Decompilers



1. JDProject

Enum in Java - A Complete Guide with Examples

Java enum also called Java enumeration type was introduced in jdk 5. All enums implicitly extend java.lang.Enum. Enum class is an abstract class.Because a class can only extend one parent, the java language does not support multiple inheritance and therefore an enum cannot extend anything else. Using enums can reduce the bugs in your code. In this tutorial I will share what is enum, example of enums , when to use enums and points to remember while using it.

Read Also : Difference between EnumMap and HashMap

1. What Is Enum ?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

1.1 EXAMPLE :

With the following simple declaration,

enum Direction 
{   
   NORTH,SOUTH,EAST,WEST; 
}

you can guarantee that the compiler will stop you from assigning anything to a Direction except NORTH,SOUTH,EAST,WEST.

If you let a Direction such as NORTH-WEST, NORTH-EAST slip in the code without defining in the Direction enum, it might cause an compiler error.

2. When To Use Enum


Difference between User Threads and Daemon Threads in Java

Thread is a concept which comes under the topic Multithreading. There are two types of threads in java, one is User thread and other is Daemon thread. User threads are also called Non Daemon threads. I have already shared the most frequently asked multithreading interview questions in java.
In this article I will share difference between User thread and Daemon thread, important questions and examples. So let's find out

Read Also :  Multithreading Interview Questions in Java


Difference between User threads and Daemon threads in Java

1. Creation :  Any thread you create from main() method is a user thread. On the other hand, If you want to create a daemon thread then you need to call setDaemon(true) method as shown in the below example.

2. JVM exit : JVM will not exit until all user threads complete their execution. While JVM will exit without waiting for daemon thread to finish their work.

5 Difference between EnumMap and HashMap with Example

One of my readers asked me to write the difference between EnumMap and HashMap. EnumMap is not heavily used as compare to HashMap in real world applications. But EnumMap has some advantages like performance which make it a preferred collection to use over others. In this article I will share the difference between HashMap and EnumMap, example of EnumMap and HashMap plus similarities between them.

Read Also: How HashMap works in java


Difference between EnumMap and HashMap in Java

1. Key : In EnumMap keys are enum type.The syntax is EnumMap,V>. In other words, you can not use any type other than Enum as key in EnumMap.
In HashMap, there is no constraint. You can use Enum as well as any other Object as key.

2. Performance : EnumMap is a specialized Map implementation for use with enum type keys. EnumMaps are represented internally as arrays. This representation is extremely compact and efficient. Due to this, EnumMap are faster than the HashMap. 

3. Internal implementation : EnumMaps are represented internally as arrays. While, HashMap is Hashtable based implementation.