Basic Linked
HashMap Operations
package com.java2novice.linkedhashmap;
import java.util.LinkedHashMap;
public class BasicLinkedHashMap
{
public static void main(String a[])
{
LinkedHashMap lhm = new LinkedHashMap();
lhm.put("one",
"This is first element");
lhm.put("two",
"This is second element");
lhm.put("four",
"this element inserted at 3rd position");
System.out.println(lhm);
System.out.println("Getting
value for key 'one': "+lhm.get("one"));
System.out.println("Size
of the map: "+lhm.size());
System.out.println("Is
map empty? "+lhm.isEmpty());
System.out.println("Contains
key 'two'? "+lhm.containsKey("two"));
System.out.println("Contains
value 'This is first element'? "
+lhm.containsValue("This is first element"));
System.out.println("delete
element 'one': "+lhm.remove("one"));
System.out.println(lhm);
}
}
How to iterate through Linked HashMap
package com.java2novice.linkedhashmap;
import java.util.LinkedHashMap;
import java.util.Set;
public class MyMapIterate
{
public static void main(String a[])
{
LinkedHashMap lhm = new LinkedHashMap();
lhm.put("one",
"This is first element");
lhm.put("two",
"This is second element");
lhm.put("four",
"Element inserted at 3rd position");
Set
keys = lhm.keySet();
for(String
k:keys)
{
System.out.println(k+"
-- "+lhm.get(k));
}
}
}
How to check whether the value exists or not in a Linked HashMap
package com.java2novice.linkedhashmap;
import java.util.LinkedHashMap;
public class MyMapValueCheck
{
public static void main(String a[])
{
LinkedHashMap lhm = new LinkedHashMap();
lhm.put("one",
"This is first element");
lhm.put("two",
"This is second element");
lhm.put("four",
"Element inserted at 3rd position");
System.out.println("Map
contains value 'This is first element'? "
+lhm.containsValue("This
is first element"));
}
}
How to delete all entries from Linked HashMap object
package com.java2novice.linkedhashmap;
import java.util.LinkedHashMap;
public class MyMapClear
{
public static void main(String a[])
{
LinkedHashMap lhm = new LinkedHashMap();
lhm.put("one",
"This is first element");
lhm.put("two",
"This is second element");
lhm.put("four",
"Element inserted at 3rd position");
System.out.println(lhm);
lhm.clear();
System.out.println(lhm);
}
}
No comments:
Post a Comment