Showing posts with label Dictionary Class. Show all posts
Showing posts with label Dictionary Class. Show all posts

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)