Java.util - SPLessons

Java.util LinkedHashMap

Home > Lesson > Chapter 10
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.util LinkedHashMap

Java.util LinkedHashMap

shape Description

The java.util.LinkedHashMap class is Hash table and Linked rundown usage of the Map interface, with unsurprising cycle arrange. The following is the syntax declaration for java.util.LinkedHashMap class. [java] public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V> [/java]

shape Methods

The following are the various methods of the LinkedHashMap.
Methods Description
protected boolean removeEldestEntry(Map.Entry eldest) To givenack genuine if this guide ought to expel its eldest section.
boolean containsValue(Object value) To return genuine if this guide maps at least one keys to the predetermined esteem.
V get(Object key) To give back the esteem to which the predefined key is mapped, or invalid if this guide contains no mapping for the key.
void clear( To expel the greater part of the mappings from this guide.

java.util.LinkedHashMap.clear()

The following is the syntax declaration of the method. [java]public void clear()[/java] The following is an example. [java] packges com.splessons; import java.util.*; public class BitSetDemo { public static void main(String[] args) { // create a new linked hash map LinkedHashMap map = new LinkedHashMap(5); // add some values in the map map.put("HELLO", 1); map.put("Two", 2); map.put("Three", 3); // print the map System.out.println("Map:" + map); // clear the map map.clear(); // print the map System.out.println("Map:" + map); } }[/java] Now compile the code result will be as follows. [java] Map:{HELLO=1, Two=2, Three=3} Map:{} [/java]

java.util.LinkedHashMap.containsValue()

The following is the syntax declaration of the method. [java] packges com.splessons; public boolean containsValue(Object value)[/java] The following is an example. [java]import java.util.*; public class BitSetDemo { public static void main(String[] args) { // create a new linked hash map LinkedHashMap map = new LinkedHashMap(5); // add some values in the map map.put("hello", 1); map.put("Two", 2); map.put("Three", 3); // print the map System.out.println("Map:" + map); // check if map contains 3 System.out.println("" + map.containsValue(3)); // check if map contains 5 System.out.println("" + map.containsValue(5)); } }[/java] Now compile the code result will be as follows. [java] Map:{hello=1, Two=2, Three=3} true false [/java]

java.util.LinkedHashMap.get()

The following is the syntax declaration of the method. [java]public V get(Object key)[/java] The following is an example. [java] packges com.splessons; import java.util.*; public class LinkedHashMapDemo { public static void main(String[] args) { // create a new linked hash map LinkedHashMap map = new LinkedHashMap(5); // add some values in the map map.put("One", 1); map.put("Two", 2); map.put("Three", 3); // print the map System.out.println("Map:" + map); // get key "Three" System.out.println("" + map.get("Three")); // get key "Five" System.out.println("" + map.get("Five")); } }[/java] Now compile the code result will be as follows. [java] Map:{One=1, Two=2, Three=3} 3 null [/java]

java.util.LinkedHashMap.removeEldestEntry()

The following is the syntax declaration of the method. [java]protected boolean removeEldestEntry(Map.Entry<K,V> eldest)[/java] The following is an example. [java]import java.util.*; public class LinkedHashMapDemo { private static final int MAX_ENTRIES = 5; public static void main(String[] args) { LinkedHashMap lhm = new LinkedHashMap(MAX_ENTRIES + 1, .75F, false) { protected boolean removeEldestEntry(Map.Entry eldest) { return size() > MAX_ENTRIES; } }; lhm.put(0, "S"); lhm.put(1, "P"); lhm.put(2, "L"); lhm.put(3, "E"); lhm.put(4, "S"); System.out.println("" + lhm); } }[/java] Now compile the code result will be as follows. [java] {0=S, 1=P, 2=L, 3=E, 4=S} [/java]

Summary

shape Key Points

  • LinkedHashMap is an implementation of java.util.Map interface with predictable iteration order
  • The Iteration over a HashMap is probably going to be more costly.
  • The class gives the majority of the discretionary Map operations, and grants invalid components.