Advertisement
LinkedHahMap() |
---|
This constructor creates an empty LinkedHashMap to hold a particular type of key-value pair objects. Example - |
LinkedHashMap <String,Integer> tm = new LinkedHashMap <String,Integer>();
Methods | Description |
---|---|
void clear() | Removes all the key/value pairs from the HashMap. |
int size() | Returns the total number of key-value pairs in a HashMap. |
boolean isEmpty() | Returns true if HashMap has no key-value pair in it. |
V get(Object key) | Returns the value associated with a specified key. |
put(K key, V value) | Puts the specified key and its associated value in the HashMap. |
boolean containsKey(Object key) | Returns true if HashMap contains a particular key, otherwise false. |
Set<Map.Entry<K,V>> entrySet() | Returns the Set containing Map.Entry objects. |
Advertisement
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"));
}
}
{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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement