how to create a LinkedHashMap using collections in java
Previous | Home | Next |
In this example we will create a LinkedHashMap.
For creating a LinkedHashMap first of all we have to import the java.util package in which the java.util.LinkedHashMap class is contained. In this example we have created a class named CollectionExample in which we have created a LinkedHashMap and added some elements to it.
In this example we have created a class named CollectionExample and created a object of the LinkedHashMap class using its default constructor as,
LinkedHashMap lhm = new LinkedHashMap();
The above statementc onstructs an empty LinkedHashMap instance with a default capacity (16) and load factor (0.75).
and added some elements to it using the put(Object key, Object value) method on the object of the LinkedHashMap object. The general form of the put() method is ,
put(Object key, Object value)
This method adds the specified value with the specified key in the LinkedHashMap.
In the given example we have created an obejct of the LinkedHashMap by using the default constructor of the LinkedHashMap. Then we have added some elements to this object by using the put(Object key, Object value) method on it.
Fianlly we have displayed the contents of the LinkedHashMap.
package r4r.co.in;
import java.util.*;
public class CollectionExample {
/**
* @param args
*/
public static void main(String args[]) throws IllegalArgumentException, NullPointerException
{
try
{
LinkedHashMap<Integer, Integer> lhm=new LinkedHashMap<Integer, Integer>(); // creating a new LinkedHashMap
lhm.put(1, new Integer(7)); // adding elements to this LinkedHashMap
lhm.put(2, new Integer(5));
lhm.put(3, new Integer(8));
lhm.put(4, new Integer(2));
lhm.put(5, new Integer(9));
lhm.put(6, new Integer(52));
System.out.println("The contents of the LinkedHashMap are as follows: ");
Set set=lhm.entrySet(); // creating a set of the contents of the LinkedHashMap
Iterator itr=set.iterator(); // invoking the iterator on the set created
while(itr.hasNext())
{
Map.Entry<Integer, Integer> map=(Map.Entry<Integer, Integer>)itr.next(); // using the Map.Entry interface
Integer key = map.getKey(); // getting the keys
Integer value = map.getValue(); // getting the values
System.out.print("KEY: "+key); // printing the keys
System.out.print(" "+"VALUE: "+value); // printing the values
System.out.println();
}
lhm.remove(3); // removing the key 3 element
lhm.remove(2); // removing the key 2 element
System.out.println("The contents of the LinkedHashMap after removal of items are: ");
set=lhm.entrySet();
itr=set.iterator();
while(itr.hasNext())
{
Map.Entry<Integer, Integer> map=(Map.Entry<Integer, Integer>)itr.next();
Integer key = map.getKey();
Integer value = map.getValue();
System.out.print("KEY: "+key);
System.out.print(" "+"VALUE: "+value);
System.out.println();
}
System.out.println(lhm.containsKey(2)); // checking whether the LinkedHashMap contains 2 as a key or not
System.out.println(lhm.containsValue(8)); // checking whether the LinkedHashMap contains 8 as an element or not
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
}
The output of the above example is given as:
The contents of the LinkedHashMap are as follows:
KEY: 1 VALUE: 7
KEY: 2 VALUE: 5
KEY: 3 VALUE: 8
KEY: 4 VALUE: 2
KEY: 5 VALUE: 9
KEY: 6 VALUE: 52
The contents of the LinkedHashMap after removal of items are:
KEY: 1 VALUE: 7
KEY: 4 VALUE: 2
KEY: 5 VALUE: 9
KEY: 6 VALUE: 52
false
false
Previous | Home | Next |