The Wayback Machine - https://web.archive.org/web/20240907010137/https://www.geeksforgeeks.org/enummap-class-java-example/
Open In App

EnumMap class in Java

Last Updated : 11 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

EnumMap is a specialized implementation of the Map interface for enumeration types. It extends AbstractMap and implements the Map interface in Java. It belongs to java.util package. A few important features of EnumMap are as follows: 

  • EnumMap class is a member of the Java Collections Framework & is not synchronized.
  • EnumMap is an ordered collection and they are maintained in the natural order of their keys(the natural order of keys means the order on which enum constants are declared inside enum type )
  • It’s a high-performance map implementation, much faster than HashMap.
  • All keys of each EnumMap instance must be keys of a single enum type.
  • EnumMap doesn’t allow null key and throws NullPointerException when we attempt to insert the null key.
  • Iterators returned by the collection views are weakly consistent: they will never throw ConcurrentModificationException and they may or may not show the effects of any modifications to the map that occur while the iteration is in progress.
  • EnumMap is internally represented as arrays. This representation is extremely compact and efficient.

Syntax: Declaration

public class EnumMap<K extends Enum<K>,?V> extends AbstractMap<K,?V> implements Serializable, Cloneable

Parameters:

  • Key object type
  • Value object type

K must extend Enum, which enforces the requirement that the keys must be of the specified enum type. 

The EnumMap class in Java is a specialized map implementation that is designed specifically for use with enum keys. An EnumMap is a compact, efficient, and fast alternative to using a HashMap with enum keys.

Here’s an example of how you can use the EnumMap class in Java:

Java




import java.util.EnumMap;
  
enum Days {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
  
public class EnumMapExample {
    public static void main(String[] args) {
        EnumMap<Days, String> schedule = new EnumMap<>(Days.class);
          
        // Adding elements to the EnumMap
        schedule.put(Days.MONDAY, "Work");
        schedule.put(Days.TUESDAY, "Work");
        schedule.put(Days.WEDNESDAY, "Study");
        schedule.put(Days.THURSDAY, "Study");
        schedule.put(Days.FRIDAY, "Relax");
          
        // Getting elements from the EnumMap
        System.out.println(schedule.get(Days.MONDAY)); // Output: Work
        System.out.println(schedule.get(Days.FRIDAY)); // Output: Relax
    }
}


output:
work 

Relax

In this example, we define an enum type Days that represents the days of the week. We then create an EnumMap and add elements to it using the put method. Finally, we retrieve elements from the EnumMap using the get method and print the results to the console.

Advantages of using EnumMap:

  1. Efficient: The EnumMap class provides fast and efficient access to elements stored in the map, making it a good choice for use in performance-critical applications.
  2. Compact: The EnumMap class uses a compact representation for the map, which means that it requires less memory than a HashMap or a TreeMap.
  3. Type-safe: The EnumMap class only allows keys of a specified enum type, which means that it provides type-safe behavior and eliminates the need for casting.
  4. Sorted keys: The EnumMap class provides sorted keys, which means that the elements in the map are ordered by the order in which the enum constants are declared.

Disadvantages of using EnumMap:

  1. Limited to enum keys: The EnumMap class can only be used with keys of an enum type, which means that it’s not suitable for use with other types of keys.
  2. Immutable keys: The EnumMap class uses enum keys, which are immutable and cannot be modified once they are created.

 

EnumMap Hierarchy 

EnumMap-in-Java

 Constructors of EnumMap 

  1. EnumMap(Class keyType): The constructor is used to create an empty EnumMap with the specified keyType.
  2. EnumMap(EnumMap m): The constructor is used to create an enum map with the same keyType as the specified enum map, with initial mappings being the same as the EnumMap
  3. EnumMap(Map m): The constructor is used to create an enum map with initialization from the specified map in the parameter.

Example  

Java




// Java Program to illustrate Working of EnumMap class
// and its functions
  
// Importing EnumMap class
import java.util.EnumMap;
  
// Main class
public class EnumMapExample {
  
    // Enum
    public enum GFG {
        CODE,
        CONTRIBUTE,
        QUIZ,
        MCQ;
    }
  
    // Main driver method
    public static void main(String args[])
    {
  
        // Java EnumMap
        // Creating an empty EnumMap with key
        // as enum type state
        EnumMap<GFG, String> gfgMap
            = new EnumMap<GFG, String>(GFG.class);
  
        // Putting values inside EnumMap in Java
        // Inserting Enum keys different from
        // their natural order
        gfgMap.put(GFG.CODE, "Start Coding with gfg");
        gfgMap.put(GFG.CONTRIBUTE, "Contribute for others");
        gfgMap.put(GFG.QUIZ, "Practice Quizes");
        gfgMap.put(GFG.MCQ, "Test Speed with Mcqs");
  
        // Printing size of EnumMap
        System.out.println("Size of EnumMap in java: "
                           + gfgMap.size());
  
        // Printing Java EnumMap
        // Print EnumMap in natural order
        // of enum keys (order on which they are declared)
        System.out.println("EnumMap: " + gfgMap);
  
        // Retrieving value from EnumMap
        System.out.println("Key : " + GFG.CODE + " Value: "
                           + gfgMap.get(GFG.CODE));
  
        // Checking if EnumMap contains a particular key
        System.out.println(
            "Does gfgMap has " + GFG.CONTRIBUTE + ": "
            + gfgMap.containsKey(GFG.CONTRIBUTE));
  
        // Checking if EnumMap contains a particular value
        System.out.println(
            "Does gfgMap has :" + GFG.QUIZ + " : "
            + gfgMap.containsValue("Practice Quizes"));
        System.out.println("Does gfgMap has :" + GFG.QUIZ
                           + " : "
                           + gfgMap.containsValue(null));
    }
}


Output

Size of EnumMap in java: 4
EnumMap: {CODE=Start Coding with gfg, CONTRIBUTE=Contribute for others, QUIZ=Practice Quizes, MCQ=Test Speed with Mcqs}
Key : CODE Value: Start Coding with gfg
Does gfgMap has CONTRIBUTE: true
Does gfgMap has :QUIZ : true
Does gfgMap has :QUIZ : false

Basic Operations on EnumMap

Operation 1: Adding Elements

In order to add elements to the EnumMap, we can use put() or putAll() method as shown below.

Java




// Java Program to Add Elements to the EnumMap
  
// Importing EnumMap class
import java.util.EnumMap;
  
// Main class
// AddingElementsToEnumMap
class GFG {
  
    enum Color { RED, GREEN, BLUE, WHITE }
    public static void main(String[] args)
    {
  
        // Creating an EnumMap of the Color enum
        EnumMap<Color, Integer> colors1
            = new EnumMap<>(Color.class);
  
        // Insert elements in Map
        // using put() method
        colors1.put(Color.RED, 1);
        colors1.put(Color.GREEN, 2);
  
        // Printing mappings to the console
        System.out.println("EnumMap colors1: " + colors1);
  
        // Creating an EnumMap of the Color Enum
        EnumMap<Color, Integer> colors2
            = new EnumMap<>(Color.class);
  
        // Adding elements using the putAll() method
        colors2.putAll(colors1);
        colors2.put(Color.BLUE, 3);
  
        // Printing mappings to the console
        System.out.println("EnumMap colors2: " + colors2);
    }
}


Output

EnumMap colors1: {RED=1, GREEN=2}
EnumMap colors2: {RED=1, GREEN=2, BLUE=3}

 Operation 2: Accessing the Elements

We can access the elements of EnumMap using entrySet(), keySet(), values(), get(). The example below explains these methods.

Java




// Java Program to Access the Elements of EnumMap
  
// Importing required classes
import java.util.EnumMap;
  
// Main class
// AccessElementsOfEnumMap
class GFG {
  
    // Enum
    enum Color { RED, GREEN, BLUE, WHITE }
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating an EnumMap of the Color enum
        EnumMap<Color, Integer> colors
            = new EnumMap<>(Color.class);
  
        // Inserting elements using put() method
        colors.put(Color.RED, 1);
        colors.put(Color.GREEN, 2);
        colors.put(Color.BLUE, 3);
        colors.put(Color.WHITE, 4);
  
        System.out.println("EnumMap colors : " + colors);
  
        // Using the entrySet() method
        System.out.println("Key/Value mappings: "
                           + colors.entrySet());
  
        // Using the keySet() method
        System.out.println("Keys: " + colors.keySet());
  
        // Using the values() method
        System.out.println("Values: " + colors.values());
  
        // Using the get() method
        System.out.println("Value of RED : "
                           + colors.get(Color.RED));
    }
}


Output

EnumMap colors : {RED=1, GREEN=2, BLUE=3, WHITE=4}
Key/Value mappings: [RED=1, GREEN=2, BLUE=3, WHITE=4]
Keys: [RED, GREEN, BLUE, WHITE]
Values: [1, 2, 3, 4]
Value of RED : 1

Operation 3: Removing elements

In order to remove the elements, EnumMap provides two variations of the remove() method.

Example

Java




// Java program to Remove Elements of EnumMap
  
// Importing EnumMap class
import java.util.EnumMap;
  
// Main class
class GFG {
  
    // Enum
    enum Color {
  
        // Custom elements
        RED,
        GREEN,
        BLUE,
        WHITE
    }
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating an EnumMap of the Color enum
        EnumMap<Color, Integer> colors
            = new EnumMap<>(Color.class);
  
        // Inserting elements in the Map
        // using put() method
        colors.put(Color.RED, 1);
        colors.put(Color.GREEN, 2);
        colors.put(Color.BLUE, 3);
        colors.put(Color.WHITE, 4);
  
        // Printing colors in the EnumMap
        System.out.println("EnumMap colors : " + colors);
  
        // Removing a mapping
        // using remove() Method
        int value = colors.remove(Color.WHITE);
  
        // Displaying the removed value
        System.out.println("Removed Value: " + value);
  
        // Removing specific color and storing boolean
        // if removed or not
        boolean result = colors.remove(Color.RED, 1);
  
        // Printing the boolean result whether removed or
        // not
        System.out.println("Is the entry {RED=1} removed? "
                           + result);
  
        // Printing the updated Map to the console
        System.out.println("Updated EnumMap: " + colors);
    }
}


Output

EnumMap colors : {RED=1, GREEN=2, BLUE=3, WHITE=4}
Removed Value: 4
Is the entry {RED=1} removed? true
Updated EnumMap: {GREEN=2, BLUE=3}

Operation 4: Replacing elements

Map interface provides three variations of the replace() method to change the mappings of EnumMap.

Example

Java




// Java Program to Replace Elements of EnumMap
  
// Importing required classes
import java.util.EnumMap;
  
// Main class
class GFG {
  
    // Enum
    enum Color {
  
        RED,
        GREEN,
        BLUE,
        WHITE
    }
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating an EnumMap of the Color enum
        EnumMap<Color, Integer> colors
            = new EnumMap<>(Color.class);
  
        // Inserting elements to Map
        // using put() method
        colors.put(Color.RED, 1);
        colors.put(Color.GREEN, 2);
        colors.put(Color.BLUE, 3);
        colors.put(Color.WHITE, 4);
  
        // Printing all elements inside above Map
        System.out.println("EnumMap colors " + colors);
  
        // Replacing certain elements depicting colors
        // using the replace() method
        colors.replace(Color.RED, 11);
        colors.replace(Color.GREEN, 2, 12);
  
        // Printing the updated elements (colors)
        System.out.println("EnumMap using replace(): "
                           + colors);
  
        // Replacing all colors using the replaceAll()
        // method
        colors.replaceAll((key, oldValue) -> oldValue + 3);
  
        // Printing the elements of above Map
        System.out.println("EnumMap using replaceAll(): "
                           + colors);
    }
}


Output

EnumMap colors {RED=1, GREEN=2, BLUE=3, WHITE=4}
EnumMap using replace(): {RED=11, GREEN=12, BLUE=3, WHITE=4}
EnumMap using replaceAll(): {RED=14, GREEN=15, BLUE=6, WHITE=7}

Synchronized EnumMap

The implementation of an EnumMap is not synchronized. This means that if multiple threads access a tree set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by using the synchronizedMap() method of the Collections class. This is best done at the creation time, to prevent accidental unsynchronized access. 

 Map<EnumKey, V> m = Collections.synchronizedMap(new EnumMap<EnumKey, V>(...));

Methods of EnumMap

  • K – Type of the key object
  • V – Type of the value object

Method

Action Performed 

clear() Removes all mappings from this map.
clone() Returns a shallow copy of this enum map.
containsKey?(Object key) Returns true if this map contains a mapping for the specified key.
containsValue?(Object value) Returns true if this map maps one or more keys to the specified value.
entrySet() Returns a Set view of the mappings contained in this map.
equals?(Object o) Compares the specified object with this map for equality.
get?(Object key) Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
 hashCode() Returns the hash code value for this map.
 keySet() Returns a Set view of the keys contained in this map.
 put?(K key, V value) Associates the specified value with the specified key in this map.
 putAll?(Map<? extends K,?? extends V> m) Copies all of the mappings from the specified map to this map.
remove?(Object key) Removes the mapping for this key from this map if present.
size() Returns the number of key-value mappings in this map.
values() Returns a Collection view of the values contained in this map.

Methods Declared in AbstractMap Class

Method

Description

isEmpty() Returns true if this map contains no key-value mappings.
toString() Returns a string representation of this map.

Methods Declared in Interface java.util.Map

Method

Descriptionenter 

compute?(K key, BiFunction<? super K,?? super V,?? extends V> remappingFunction) Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
 computeIfAbsent?(K key, Function<? super K,?? extends V> mappingFunction) If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
 computeIfPresent?(K key, BiFunction<? super K,?? super V,?? extends V> remappingFunction) If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
 forEach?(BiConsumer<? super K,?? super V> action) Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
getOrDefault?(Object key, V defaultValue) Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
merge?(K key, V value, BiFunction<? super V,?? super V,?? extends V> remappingFunction) If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
putIfAbsent?(K key, V value) If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.
remove?(Object key, Object value) Removes the entry for the specified key only if it is currently mapped to the specified value.
replace?(K key, V value) Replaces the entry for the specified key only if it is currently mapped to some value.
replace?(K key, V oldValue, V newValue) Replaces the entry for the specified key only if currently mapped to the specified value.
replaceAll?(BiFunction<? super K,?? super V,?? extends V> function) Replaces each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.

EnumMap vs EnumSet

Property

EnumMap

EnumSet

Internal Representation EnumMap is internally represented as arrays. The representation is compact and efficient. EnumSet is internally represented as BitVector or sequence of bits.
Permits Null Elements? Null keys are not allowed but Null values are allowed. Null elements are not permitted.
Is the Abstract Class? No Yes
Instantiation Since EnumMap is not an abstract class, it can be instantiated using the new operator. It is an abstract class, it does not have a constructor. Enum set is created using its predefined methods like allOf(), noneOf(), of(), etc.
Implementation EnumMap is a specialized Map implementation for use with enum type keys. EnumSet is a specialized Set implementation for use with enum types.

See your article appearing on GeeksforGeek’s main page and help other Geeks.  

Example :

Explanation

EnumMap is a specialized implementation of the Map interface in Java that is designed to be used with enums as keys. It is a high-performance implementation that is guaranteed to have constant time performance in many basic operations, such as get() and put().

The EnumMap class is a strongly typed Map implementation, which means that it can only be used with enums as keys. Each instance of EnumMap is associated with a specific enum class, and the key set of the map is a subset of the enum values. This ensures that the keys are always unique and well-defined.

One of the primary advantages of using EnumMap is its performance. Since it is designed specifically for use with enums, it can be optimized to take advantage of the unique properties of enums. For example, the EnumMap implementation uses a compact array-based data structure internally, which provides constant time performance for many basic operations.

Program 

Java




import java.util.EnumMap;
import java.util.Map;
  
public class EnumMapExample {
      
    enum Color {
        RED, GREEN, BLUE
    }
      
    public static void main(String[] args) {
        // create an EnumMap with Color enum as keys and String as values
        EnumMap<Color, String> colorMap = new EnumMap<>(Color.class);
          
        // add some key-value pairs to the map
        colorMap.put(Color.RED, "FF0000");
        colorMap.put(Color.GREEN, "00FF00");
        colorMap.put(Color.BLUE, "0000FF");
          
        // print the map
        System.out.println("Color map: " + colorMap);
          
        // get the value associated with a particular key
        String greenValue = colorMap.get(Color.GREEN);
        System.out.println("Value for GREEN: " + greenValue);
          
        // iterate over the map and print all key-value pairs
        System.out.println("All key-value pairs:");
        for (Map.Entry<Color, String> entry : colorMap.entrySet()) {
            System.out.println(entry.getKey() + " -> " + entry.getValue());
        }
    }
}


Output

Color map: {RED=FF0000, GREEN=00FF00, BLUE=0000FF}
Value for GREEN: 00FF00
All key-value pairs:
RED -> FF0000
GREEN -> 00FF00
BLUE -> 0000FF

Here are some benefits of using EnumMap in Java:

Type safety: EnumMap is a strongly-typed implementation of Map that ensures that only enum constants can be used as keys. This provides type safety and reduces the risk of runtime errors caused by using the wrong key type.

Performance: EnumMap is designed to provide high performance when used with enums as keys. It uses a compact array-based data structure internally, which provides constant time performance for many basic operations.

Memory efficiency: Since EnumMap is designed to be used with enums as keys, it can be optimized to use less memory than other Map implementations. This can be particularly important when dealing with large numbers of enum constants.

Clear and concise code: Using EnumMap can make code clearer and more concise, particularly when dealing with operations that are specific to enums, such as iterating over all enum constants.

Standardized behavior: Since EnumMap is part of the Java Collections Framework, it provides a standardized way of using enums as keys in a Map. This can make code more consistent and easier to understand.

Overall, EnumMap provides a simple, efficient, and type-safe way to use enums as keys in a Map implementation. Its performance benefits and memory efficiency make it a good choice for applications that need to work with enums in this way.

Reference book:

“Java Performance: The Definitive Guide” by Scott Oaks is a comprehensive guide to optimizing the performance of Java applications. It includes a section on the use of specialized map implementations, including the EnumMap class.



Similar Reads

EnumMap remove() Method in Java
The Java.util.EnumMap.remove(key) method in Java is used to remove the specified key from the map. Syntax: remove(Object key) Parameters: The method takes one parameter key which refers to the key whose mapping is to be removed. Return Value: The method does not return any value. Below programs illustrate the working of remove(key) function: Progra
2 min read
EnumMap size() Method in Java
The Java.util.EnumMap.size() method in java is used to know the size of the map or the number of elements present in the map. Syntax: Enum_Map.size() Parameters: The method does not take any parameters. Return Value: The method returns the size of the map. Below programs illustrate the working of size() function: Program 1: // Java program to demon
2 min read
EnumMap entrySet() Method in Java
The Java.util.EnumMap.entrySet() method in Java used to create a set out of the elements contained in the EnumMap. It basically returns a set view of the enum map. Syntax: enum_map.entrySet() Parameter: The method does not take any parameters. Return Value: The method returns the set view of mappings contained in this enum_map. Below programs illus
2 min read
EnumMap clone() Method in Java
The Java.util.EnumMap.clone() method in Java is used to copy the mapped values of one map to another. It basically creates a shallow copy of this map. Syntax: Enum_map_2 = Enum_map_1.clone() Parameters: The method does not accept any argument. Return Value: The method returns a shallow copy of a EnumMap. Below programs illustrate the Java.util.Enum
2 min read
EnumMap containsKey() Method in Java
The Java.util.EnumMap.containsKey(key) method is used to check whether a specified key mentioned in the parameter is present in this map or not. Syntax: boolean containsKey(Object key) Parameter: The method accepts one parameter key which refer to the key to be verified. Return Value: The method returns true if the key is present in the EnumMap oth
2 min read
EnumMap containsValue(value) method in Java
The Java.util.EnumMap.containsValue(value) method in Java is used to determine whether one or more key of the map is associated with a given value or not. It takes the value as a parameter and returns True if that value is mapped by any of the keys in the EnumMap. Syntax: boolean containsValue(Object value) Parameter: The method accepts one paramet
2 min read
EnumMap values() Method in Java
The Java.util.EnumMap.values() method in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the EnumMap. Syntax: EnumMap.values() Parameters: The method does not take any argument. Return Values: The method returns the collection view of the mapped values. Below programs illustr
2 min read
EnumMap putAll(map) Method in Java
The Java.util.EnumMap.putAll(map) method in Java is used to copy all the mappings from one map to a newer one. Older mappings are replaced in a newer one. Syntax: void putAll(map) Parameters: The method takes one parameter map. It is the map which is to be copied to the newer one. Return Value The method does not return any value. Below programs il
2 min read
EnumMap keySet() Method in Java
The Java.util.EnumMap.keySet() method in Java is used to return the set view of the keys contained in the map. Syntax: Enum_Map.keySet() Parameters: The method does not accept any argument. Return Value: The method returns the set view of the keys contained in the map. Below programs illustrate the keySet() function: Program 1: // Java program to d
2 min read
EnumMap get() Method in Java
The Java.util.EnumMap.get(Object key) method in Java is used to give the mapped value of the specified key. Syntax: get(Object key) Parameter: The method takes one parameter key which is passed to return the value associated with it. Return Value: It returns the value mapped with the associated key. Below programs illustrate the working of get() fu
2 min read
Difference Between EnumMap and EnumSet in Java
EnumMap and EnumSet both are the classes defined inside the java collection. In this article, we will learn the differences between EnumMap and EnumSet. EnumMap is the specialized implementation of the Map interface and the EnumSet is the specialized implementation of the Set interface. There are some differences that exist between them. So we have
3 min read
Difference Between IdentityHashMap, WeakHashMap, and EnumMap in Java
The IdentityHashMap, WeakHashMap, and EnumMap all are the classes in java collection that implements the Map interface. But there are few differences exists between them. 1. IdentityHashMap: The IdentityHashMap implements the Map interface. It follows reference-equality in place of object-equality when comparing keys (and values). This class is use
5 min read
EnumMap put() Method in Java with Examples
The Java.util.EnumMap.put() method in Java associates specified key-value pairs. In simple words, the put() method is used to add or modify entries in the EnumMap. If the values are repeated, the older values are replaced. Example Java Code // Java Program to demonstrate the usage of EnumMap import java.util.EnumMap; // Enumeration representing pro
4 min read
Difference Between EnumMap and HashMap
EnumMap and HashMap both are the classes that implement the Map interface. But there are some differences that exist between them. So we have tried to list out the differences between EnumMap and HashMap. 1. EnumMap: EnumMap is a specialized implementation of the Map interface for enumeration types. It extends AbstractMap and implements the Map int
4 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
15+ min read
Java.lang.Class class in Java | Set 2
Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo
15+ min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
TimeZone class (the methods of this class was discussed in this article Java.util.TimeZone Class | Set 1) can be used in many cases like using TimeZone class we can get the time difference in term of hour and minute between two places.Problem : How we can get time difference of time in terms of hours and minutes between two places of Earth?Solution
5 min read
Implement Pair Class with Unit Class in Java using JavaTuples
To implement a Pair class with a Unit class in Java using JavaTuples, you can use the Pair class provided by the library and create a new Unit class that extends the Unit class provided by the library. Here is an example implementation: Here is an example Java code that uses the MyPair class with MyUnit and displays the output: Java Code import org
4 min read
Implement Triplet Class with Pair Class in Java using JavaTuples
Following are the ways to implement Triplet Class with Pair Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Pair Pair&lt;Integer, String&gt; pair = new Pair&lt;Integer, String&gt;( Integer.valueOf(1), &quot;GeeksforGeeks&quot;); // Print the Pair System.out.printl
2 min read
Implement Quintet Class with Quartet Class in Java using JavaTuples
Following are the ways to implement Quintet Class with Quartet Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Quartet Quartet&lt;String, String, String, String&gt; quartet = new Quartet&lt;String, String, String, String&gt;( &quot;Quartet&quot;, &quot;Triplet
4 min read
Implement Quartet Class with Triplet Class in Java using JavaTuples
Following are the ways to implement Quartet Class with Triplet Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Triplet Triplet&lt;String, String, String&gt; triplet = new Triplet&lt;String, String, String&gt;( &quot;Triplet 1&quot;, &quot;1&quot;, &quot;GeeksforGe
3 min read
Implement Octet Class from Septet Class in Java using JavaTuples
Prerequisite: Octet Class, Septet Class Below are the methods to implement a Octet Class using Septet Class in Java: Using direct values // Java program to illustrate // implementing Octet Class // from Septet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create Sep
6 min read
Implement Ennead Class from Octet Class in Java using JavaTuples
Prerequisite: Ennead Class, Octet Class Below are the methods to implement a Ennead Class using Octet Class in Java: Using direct values // Java program to illustrate // implementing Ennead Class // from Octet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create Oct
7 min read
Implement Sextet Class from Quintet Class in Java using JavaTuples
Prerequisite: Sextet Class, Quintet Class Below are the methods to implement a Sextet Class using Quintet Class in Java: Using direct values // Java program to illustrate // implementing Sextet Class // from Quintet Class using // direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // crea
5 min read
Implement Septet Class from Sextet Class in Java using JavaTuples
Prerequisite: Septet Class, Sextet Class Below are the methods to implement a Septet Class using Sextet Class in Java: Using direct values // Java program to illustrate // implementing Septet Class // from Sextet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create
6 min read
Implement Decade Class from Ennead Class in Java using JavaTuples
Prerequisite: Decade Class, Ennead Class Below are the methods to implement a Decade Class using Ennead Class in Java: Using direct values // Java program to illustrate // implementing Decade Class // from Ennead Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create
8 min read
Difference between Abstract Class and Concrete Class in Java
Abstract Class: An abstract class is a type of class in Java that is declared by the abstract keyword. An abstract class cannot be instantiated directly, i.e. the object of such class cannot be created directly using the new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract method along wit
5 min read
Java - Inner Class vs Sub Class
Inner Class In Java, one can define a new class inside any other class. Such classes are known as Inner class. It is a non-static class, hence, it cannot define any static members in itself. Every instance has access to instance members of containing class. It is of three types: Nested Inner ClassMethod Local Inner ClassAnonymous Inner Class Genera
3 min read
Java I/O Operation - Wrapper Class vs Primitive Class Variables
It is better to use the Primitive Class variable for the I/O operation unless there is a necessity of using the Wrapper Class. In this article, we can discuss briefly both wrapper class and primitive data type. A primitive data type focuses on variable values, without any additional methods.The Default value of Primitive class variables are given b
3 min read
Practice Tags :