Example
package com.w3schools;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class Test {
public static void main(String args[]){
LinkedHashMap<Integer, String> linkedHashMap =
new LinkedHashMap<Integer, String>();
//Add objects to the HashSet.
linkedHashMap.put(4, "Roxy");
linkedHashMap.put(2, "Sunil");
linkedHashMap.put(5, "Sandy");
linkedHashMap.put(1, "Munish");
linkedHashMap.put(3, "Pardeep");
//Print the LinkedHashMap object.
System.out.println("LinkedHashMap elements:");
System.out.println(linkedHashMap);
//Get iterator
Set set=linkedHashMap.entrySet();
Iterator iterator=set.iterator();
//Print the LinkedHashMap elements using iterator.
System.out.println("LinkedHashMap elements " +
"using iterator:");
while(iterator.hasNext()){
Map.Entry mapEntry=(Map.Entry)iterator.next();
System.out.println("Key: " + mapEntry.getKey() +
", Value: " + mapEntry.getValue());
}
}
} |
package com.w3schools;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class Test {
public static void main(String args[]){
LinkedHashMap<Integer, String> linkedHashMap =
new LinkedHashMap<Integer, String>();
//Add objects to the HashSet.
linkedHashMap.put(4, "Roxy");
linkedHashMap.put(2, "Sunil");
linkedHashMap.put(5, "Sandy");
linkedHashMap.put(1, "Munish");
linkedHashMap.put(3, "Pardeep");
//Print the LinkedHashMap object.
System.out.println("LinkedHashMap elements:");
System.out.println(linkedHashMap);
//Get iterator
Set set=linkedHashMap.entrySet();
Iterator iterator=set.iterator();
//Print the LinkedHashMap elements using iterator.
System.out.println("LinkedHashMap elements " +
"using iterator:");
while(iterator.hasNext()){
Map.Entry mapEntry=(Map.Entry)iterator.next();
System.out.println("Key: " + mapEntry.getKey() +
", Value: " + mapEntry.getValue());
}
}
}
Output
LinkedHashMap elements:
{4=Roxy, 2=Sunil, 5=Sandy, 1=Munish, 3=Pardeep}
LinkedHashMap elements using iterator:
Key: 4, Value: Roxy
Key: 2, Value: Sunil
Key: 5, Value: Sandy
Key: 1, Value: Munish
Key: 3, Value: Pardeep |
LinkedHashMap elements:
{4=Roxy, 2=Sunil, 5=Sandy, 1=Munish, 3=Pardeep}
LinkedHashMap elements using iterator:
Key: 4, Value: Roxy
Key: 2, Value: Sunil
Key: 5, Value: Sandy
Key: 1, Value: Munish
Key: 3, Value: Pardeep