EnumMap compute(K,V) in Java

compute(K,V): This method is available in java.util.EnumMap class of Java.

Syntax:

String java.util.Map.compute(K key, BiFunction<? super K, ? super V, ? extends V> remapping function)

This method takes two arguments. This method Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).

Parameters: Two parameters are required for this method.

key: key with which the specified value is to be associated.

remappingFunction: the remapping function to compute a value.

Returns: the new value associated with the specified key, or null if none.

Throws:

1. NullPointerException - if the specified key is null and this map does not support null keys, or the remappingFunction is null.

2. UnsupportedOperationException - if the put operation is not supported by this map.

3. ClassCastException - if the class of the specified key or value prevents it from being stored in this map.

4. IllegalArgumentException - if some property of the specified key or value prevents it from being stored in this map(optional).

Approach 1: When no exception

Java

import java.util.EnumMap;

public class EnumMapcompute {

    public enum Colour {
        RED, GREEN, YELLOW, ORANGE
    };

    public static void main(String[] args) {

        EnumMap<Colour, String> enumMap =
new EnumMap<Colour, String>(Colour.class);

        System.out.println(enumMap.compute(Colour.RED,
(k, v) -> (v == null) ? "Hello" : v.concat("Hello")));
    }
}

Output:

Hello


Approach 2: NullPointerException

Java

import java.util.EnumMap;

public class EnumMapcompute {

    public enum Colour {
        RED, GREEN, YELLOW, ORANGE
    };

    public static void main(String[] args) {

        EnumMap<Colour, String> enumMap =
new EnumMap<Colour, String>(Colour.class);

        System.out.println(enumMap.compute(Colour.RED, null));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.Map.compute(Map.java:1167)


EnumMap clone() in Java

clone(): This method is available in java.util.EnumMap class of Java.

Syntax:

EnumMap<K, V> java.util.EnumMap.clone()

This method returns a shallow copy of this enum map. The values themselves are not cloned.

Parameters: NA

Returns: a shallow copy of this enum map.

Exceptions: NA

Approach

Java

import java.util.EnumMap;

public class EnumMapclone {

    public enum Colour {
        RED, GREEN, YELLOW, ORANGE
    };

    public static void main(String[] args) {

        EnumMap<Colour, String> enumMap =
new EnumMap<Colour, String>(Colour.class);

        enumMap.put(Colour.RED, "Red");
        enumMap.put(Colour.GREEN, "Green");
        enumMap.put(Colour.YELLOW, "Yellow");

        System.out.println(enumMap.clone());
    }
}

Output:

{RED=Red, GREEN=Green, YELLOW=Yellow}


EnumMap clear() in Java

clear(): This method is available in java.util.EnumMap class of Java.

Syntax:

void java.util.EnumMap.clear()

This method removes all mappings from this map.

Parameters: NA

Returns: NA

Exceptions: NA

Approach

Java

import java.util.EnumMap;

public class EnumMapclear {

    public enum Colour {
        RED, GREEN, YELLOW, ORANGE
    };

    public static void main(String[] args) {

        EnumMap<Colour, String> enumMap =
new EnumMap<Colour, String>(Colour.class);

        enumMap.clear();

        System.out.println(enumMap);
    }
}

Output:

{}


Dictionary Class Methods in Java

java.util.Dictionary<K, V>

The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. Every key and every value is an object. In any one Dictionary object, every key is associated with at most one value.


Some methods of Dictionary class.


get(Object)This method returns the value to which the key is mapped in this dictionary.


isEmpty()This method tests if this dictionary maps no keys to value.


elements()This method returns an enumeration of the values in this dictionary.


equals(Object)This method indicates whether some other object is "equal to" this one.


keys()This method returns an enumeration of the keys in this dictionary.


put(K, V)This method maps the specified key to the specified value in this dictionary.


remove(Object)This method removes the key (and its corresponding value) from this dictionary.


size()This method returns the number of entries (distinct keys) in this dictionary.


toString()This method returns a string representation of the object.


Dictionary toString() in Java

toString(): This method is available in java.util.Dictionary class of Java.

Syntax:

String java.lang.Object.toString()

This method returns a string representation of the object.

Parameters: NA

Returns: a string representation of the object.

Exceptions: NA

Approach

Java

import java.util.Dictionary;
import java.util.Hashtable;

public class DictionarytoString {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);

        System.out.println(dictionary.toString());

    }
}

Output:

{Java=4, C++=17, Hello=10}


Dictionary size() in Java

size(): This method is available in java.util.Dictionary class of Java.

Syntax:

int java.util.Dictionary.size()

This method returns the number of entries (distinct keys) in this dictionary.

Note: Size is equivalent to the number of distinct keys in the dictionary.

Parameters: NA

Returns: the number of keys in this dictionary.

Exceptions: NA

Approach

Java

import java.util.Dictionary;
import java.util.Hashtable;

public class Dictionarysize {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);
        dictionary.put("C++", 6);

        System.out.println(dictionary.size());

    }
}

Output:

3


Dictionary remove(Object) in Java

remove(Object): This method is available in java.util.Dictionary class of Java.

Syntax:

Integer java.util.Dictionary.remove(Object key)

This method takes one argument of type Object as its parameter. This method removes the key (and its corresponding value) from this dictionary.

Note: This method does nothing if the key is not in this dictionary.

Parameters: One parameter is required for this method.

key: the key that needs to be removed.

Returns: the value to which the key had been mapped in this dictionary, or null if the key did not have a mapping.

Throws:

NullPointerException - if key is null.

Approach 1: When no exception

Java

import java.util.Dictionary;
import java.util.Hashtable;

public class Dictionaryremove {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);

        dictionary.remove("Hello");

        System.out.println(dictionary);

    }
}

Output:

{Java=4, C++=17}


Approach 2: NullPointerException

Java

import java.util.Dictionary;
import java.util.Hashtable;

public class Dictionaryremove {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);

        dictionary.remove(null);

        System.out.println(dictionary);

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" is null at java.base/java.util.Hashtable.remove(Hashtable.java:508)


Dictionary put(K, V) in Java

put(K, V): This method is available in java.util.Dictionary class of Java.

Syntax:

Integer java.util.Dictionary.put(K key, V value)

This method takes two arguments. This method maps the specified key to the specified value in this dictionary. Neither the key nor the value can be null.

Note:

1. If this dictionary already contains an entry for the specified key, the value already in this dictionary for that key is returned, after modifying the entry to contain the new element.

2. If this dictionary does not already have an entry for the specified key, an entry is created for the specified key and value, and null is returned.

Parameters: One parameter is required for this method.

key: the hashtable key.

value: the value.

Returns: the previous value to which the key was mapped in this dictionary, or null if the key did not have a previous mapping.

Throws:

NullPointerException - if the key or value is null.

Approach 1: When no exception

Java

import java.util.Dictionary;
import java.util.Hashtable;

public class Dictionaryput {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);

        System.out.println(dictionary);

    }
}

Output:

{Java=4, C++=17, Hello=10}


Approach 2: NullPointerException

Java

import java.util.Dictionary;
import java.util.Hashtable;

public class Dictionaryput {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", null);

        System.out.println(dictionary);

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Hashtable.put(Hashtable.java:476)


Dictionary keys() in Java

keys(): This method is available in java.util.Dictionary class of Java.

Syntax:

Enumeration<K> java.util.Dictionary.keys()

This method returns an enumeration of the keys in this dictionary.

Parameters: NA

Returns: an enumeration of the keys in this dictionary.

Exceptions: NA

Approach

Java

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;

public class Dictionarykeys {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);

        Enumeration<String> enumeration = dictionary.keys();

        while (enumeration.hasMoreElements()) {
            System.out.print(enumeration.nextElement() + " ");

        }

    }
}

Output:

Java C++ Hello 

Dictionary equals(Object) in Java

equals(Object): This method is available in java.util.Dictionary class of Java.

Syntax:

boolean java.lang.Object.equals(Object obj)

This method takes one argument of type Object as its parameter. This method indicates whether some other object is "equal to" this one.

Parameters: One parameter is required for this method.

obj: the reference object with which to compare.

Returns: true if this object is the same as the obj argument; false otherwise.

Exceptions: NA

Approach

Java

import java.util.Dictionary;
import java.util.Hashtable;

public class Dictionaryequals {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);
        Dictionary<String, Integer> dictionary2 =
new Hashtable<String, Integer>();

        System.out.println(dictionary.equals(dictionary2));

    }
}

Output:

false


Dictionary elements() in Java

elements(): This method is available in java.util.Dictionary class of Java.

Syntax:

Enumeration<Integer> java.util.Dictionary.elements()

This method returns an enumeration of the values in this dictionary.

Parameters: NA

Returns: an enumeration of the values in this dictionary.

Exceptions: NA

Approach

Java

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;

public class Dictionaryelements {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();
        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);
        Enumeration<Integer> enumeration = dictionary.elements();

        while (enumeration.hasMoreElements())
            System.out.print(enumeration.nextElement() + " ");

    }
}

Output:

4 17 10 

Dictionary isEmpty() in Java

isEmpty(): This method is available in java.util.Dictionary class of Java.

Syntax:

boolean java.util.Dictionary.isEmpty()

This method tests if this dictionary maps no keys to value.

Note: The general contract for the isEmpty method is that the result is true if and only if this dictionary contains no entries.

Parameters: NA

Returns: true if this dictionary maps no keys to values; false otherwise.

Exceptions: NA

Approach

Java

import java.util.Dictionary;
import java.util.Hashtable;

public class DictionaryeisEmpty {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);

        System.out.println(dictionary.isEmpty());

    }
}

Output:

false


Dictionary get(Object) in Java

get(Object): This method is available in java.util.Dictionary class of Java.

Syntax:

Integer java.util.Dictionary.get(Object key)

This method takes one argument of type Object as its parameter. This method returns the value to which the key is mapped in this dictionary.

Parameters: One parameter is required for this method.

key: a key in this dictionary. null if the key is not mapped to any value in this dictionary.

Returns: the value to which the key is mapped in this dictionary.

Throws:

1. NullPointerException - if the key is null.

Approach 1: When no exception

Java

import java.util.Dictionary;
import java.util.Hashtable;

public class Dictionaryeget {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);

        System.out.println(dictionary.get("Hello"));

    }
}

Output:

10


Approach 2: NullPointerException 

Java

import java.util.Dictionary;
import java.util.Hashtable;

public class Dictionaryeget {
    public static void main(String[] args) {

        Dictionary<String, Integer> dictionary =
new Hashtable<String, Integer>();

        dictionary.put("Hello", 10);
        dictionary.put("Java", 4);
        dictionary.put("C++", 17);

        System.out.println(dictionary.get(null));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" is null at java.base/java.util.Hashtable.get(Hashtable.java:381)


Date class Methods in Java

java.util.Date

The class Date represents a specific instant in time, with millisecond precision.

Some methods of Date class.


after(Date)This method tests if this date is after the specified date.


before(Date)This method tests if this date is before the specified date.


clone()This method returns a copy of this object.


compareTo(Date)This method compares two Dates for ordering.


equals(Object)This method compares two dates for equality.


Date.from(Instant)This method obtains an instance of Date from an Instant object.


getTime()This method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.


hashCode()This method returns a hash code value for this object.


setTime(long)This method sets this Date object to represent a point in time that is time milliseconds after January 1, 1970, 00:00:00 GMT.


toInstant()This method converts this Date object to an Instant.


 toString()This method converts this Date object to a string of the form: dow mon dd hh:mm: ss zzz yyyy



Date toString() in Java

toString(): This method is available in java.util.Date class of Java.

Syntax:

String java.util.Date.toString()

This method converts this Date object to a string of the form: dow mon dd hh:mm:ss zzz yyyy

where:

1. dow is the day of the week.

2. mon is the month.

3. dd is the day of the month (01 through 31), as two decimal digits.

4. hh is the hour of the day (00 through 23), as two decimal digits.

5. mm is the minute within the hour (00 through 59), as two decimal digits.

6. ss is the second within the minute (00 through 61, as two decimal digits.

7. zzz is the time zone.

8. yyyy is the year, as four decimal digits.

Parameters: NA

Returns: a string representation of this date.

Exceptions: NA

Approach

Java

import java.util.Date;

public class DatetoString {
    public static void main(String[] args) {

        Date date = new Date();

        System.out.println(date.toString());

    }
}

Output:

Sun Jan 30 18:26:09 IST 2022