Java Collection Framework for Computer Science and MCA

List out any four methods of Hash Map class.

The HashMap class implements the map interface. It is a collection which stores the elements in the form of a key value pair. It holds the unique keys. Duplicate data for the keys cannot be used. It uses the null values and the null keys.

Syntax:

class HashMap <K, V>

Methods of HashMap class:
  • void clear() : It removes all the key value pairs from the Map.
  • value get(Object key) : It returns the corresponding value when the key is given. If the key does not have a value associated with it, then it returns null.
  • value put (key, value) : It stores the key-value pair into the HashMap.
  • Set <k> keyset() : Converts the HashMap into a Set where only keys will be stored.
  • Collection <v> values() : It returns all the values of the HashMap into a Collection object.
  • value remove (Object key) : It removes the key and the corresponding value from the HashMap.
  • boolean isEmpty() : It returns true if there are no key-value pairs in the HashMap.
  • int size() : It returns the number of key-value pairs in the HashMap.
The HashMap Class also supports some Constructors. They are as follows:
  • HashMap() : It constructs a default HashMap.
  • HashMap(Map m) : It initializes the hash map by use of the elements of the given Map object m.
  • HashMap(int capacity) : It initializes the capacity of the hash map to the given integer value,capacity.
  • HashMap(int capacity,float fillRatio) : It initializes both thr capacity and it fills the ratio of the hash map by using its arguments.

List out four methods of Enumeration Interface.

The Enumeration interface is present in the java.util package. It is used to retrieve the elements one by one like the Iterator but the interface never removes an element from the collection object.

Methods
  • boolean hasMoreElements() : It checks if the Enumeration has any more elements or not.
  • Object nextElement() : It returns the next element that is available in Enumeration.