Example:
import java.util.LinkedHashMap; import java.util.Set; import java.util.Iterator; import java.util.Map; public class LinkedHashMapDemo { public static void main(String args[]) { // HashMap Declaration LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); //Adding elements to LinkedHashMap lhmap.put(22, "Abey"); lhmap.put(33, "Dawn"); lhmap.put(1, "Sherry"); lhmap.put(2, "Karon"); lhmap.put(100, "Jim"); // Generating a Set of entries Set set = lhmap.entrySet(); // Displaying elements of LinkedHashMap Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry me = (Map.Entry)iterator.next(); System.out.print("Key is: "+ me.getKey() + "& Value is: "+me.getValue()+"\n"); } } }
Output:
Key is: 22& Value is: Abey Key is: 33& Value is: Dawn Key is: 1& Value is: Sherry Key is: 2& Value is: Karon Key is: 100& Value is: Jim