Advertisement



< Prev
Next >



LinkedHashMap Class





LinkedHashMap is a Map class that extends HashMap class. This class stores list of entries(key-value pairs)in the order in which they were inserted. i.e. the first element to enter the LinkedHashMap occupies the first position, the second to enter the Map occupies the second position and so on.




Important features of LinkedHashSet


Creating a LinkedHashMap


In this example we will create a LinkedHashMap to store objects in the pair of key and value, where key is a String object and value is an Integer object. By default, the map entries(key-value pairs) in the LinkedHashMap will be ordered as per their insertion order.

import java.util.*;


class LinkedHashMapDemo
{
public static void main(String... ar)
{
LinkedHashMap<String,Integer> lhm = new LinkedHashMap<String,Integer>();

lhm.put("A", 1000);
lhm.put("C", 4000);
lhm.put("B", 2000);
lhm.put("F", 6000);
lhm.put("D", 5000);

System.out.println(lhm);

System.out.println("Iterating LinkedHashMap using Map.Entry and for-each loop");
Set<Map.Entry<String,Integer>> set= lhm.entrySet();


for(Map.Entry<String,Integer> mapE : set)
{
	System.out.print(mapE.getKey() + " : ");
	System.out.println(mapE.getValue());
}


System.out.println("Value with the key B is "+ lhm.get("B"));
System.out.println("Value with the key F is "+ lhm.get("F"));

}

}


Output is :


{A=1000, C=4000, B=2000, F=6000, D=5000}
Iterating LinkedHashMap using Map.Entry and for-each loop
A : 1000
C : 4000
B : 2000
F : 6000
D : 5000
Value with the key B is 2000
Value with the key F is 6000


Program Analysis


Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement