0% found this document useful (0 votes)
37 views

Collection Example

The document discusses HashMap in Java. It defines HashMap as a map that stores key-value pairs without maintaining insertion order. It lists common HashMap methods like put(), get(), size(), and remove(). An example shows adding key-value pairs to a HashMap, iterating over entries using an Iterator, and sorting the keys using a TreeMap.

Uploaded by

Saurabh Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Collection Example

The document discusses HashMap in Java. It defines HashMap as a map that stores key-value pairs without maintaining insertion order. It lists common HashMap methods like put(), get(), size(), and remove(). An example shows adding key-value pairs to a HashMap, iterating over entries using an Iterator, and sorting the keys using a TreeMap.

Uploaded by

Saurabh Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Collection Example

HashMAp
HashMap
• HashMap is a Map based collection class that is used for
storing Key & value pairs, it is denoted as
HashMap<Key, Value> or HashMap<K, V>. This class
makes no guarantees as to the order of the map. It is
similar to the Hashtable class except that it is
unsynchronized and permits nulls(null values and null
key).
• It is not an ordered collection which means it does not
return the keys and values in the same order in which
they have been inserted into the HashMap. It does not
sort the stored keys and Values.
• You need to import java.util.HashMap or its super class
in order to use the HashMap class and methods.
HashMap Class Methods
• void clear(): It removes all the key and value pairs from the specified Map.
• Object clone(): It returns a copy of all the mappings of a map and used for
cloning them into another map.
• boolean containsKey(Object key): It is a boolean function which returns true or
false based on whether the specified key is found in the map.
• boolean containsValue(Object Value): Similar to containsKey() method,
however it looks for the specified value instead of key.
• Value get(Object key): It returns the value for the specified key.
• boolean isEmpty(): It checks whether the map is empty. If there are no
key-value mapping present in the map then this function returns true else false.
• Set keySet(): It returns the Set of the keys fetched from the map.
• value put(Key k, Value v): Inserts key value mapping into the map.
• int size(): Returns the size of the map – Number of key-value mappings.
• Collection values(): It returns a collection of values of map.
• Value remove(Object key): It removes the key-value pair for the specified key.
• void putAll(Map m): Copies all the elements of a map to the another specified
map.
HashMap Example in Java:
public class Details
{
public static void main(String [] args)
{
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
//Adding elements to HashMap
hmap.put(11, "AB");
hmap.put(2, "CD");
hmap.put(33, "EF");
hmap.put(9, "GH");
hmap.put(3, "IJ");

//WHILE LOOP & ITERATOR


System.out.println("While Loop:");
Iterator iterator = hmap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry me2 = (Map.Entry) iterator.next();
System.out.println("Key: "+me2.getKey() + " & Value: " + me2.getValue());
}
}
}
Output
• While Loop:
• Key: 2 & Value: CD
• Key: 3 & Value: IJ
• Key: 33 & Value: EF
• Key: 9 & Value: GH
• Key: 11 & Value: AB
HashMap Sorting by Keys
public class Details {
public static void main(String[] args) {
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
hmap.put(5, "A");
hmap.put(11, "C");
hmap.put(4, "Z");
hmap.put(77, "Y");
hmap.put(9, "P");
hmap.put(66, "Q");
hmap.put(0, "R");
System.out.println("Before Sorting:");

Map<Integer, String> map = new TreeMap<Integer, String>(hmap);


System.out.println("After Sorting:");
Set set2 = map.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry me2 = (Map.Entry)iterator2.next();
System.out.print(me2.getKey() + ": ");
System.out.println(me2.getValue());}}}

You might also like