What is LinkedHashMap?
LinkedHashMap is a combination of Hash table and linked list implementation of the Map interface, with predictable iteration order.
It maintains a doubly-linked list running through all of its entries.
The iteration order is normally the order in which keys were inserted into the map i.e FIFO.
The insertion order is not affected if a key is re-inserted into the map.
This class is different from both HashMap and TreeMap:
- HashMap doesn’t maintain any order.
- TreeMap sort the entries in ascending order of keys.
- LinkedHashMap maintains the insertion order.
How to perform Basic Operations in LinkedHashMap?
The Basic Operations are:
- create object for LinkedHashMap
- adding elements
- getting size
- checking empty or not
- deleting an element
Basic Operations
import java.util.LinkedHashMap; public class Basic { public static void main(String a[]){ LinkedHashMapTry it Yourselflhm = 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 LinkedHashMap?
Example
import java.util.LinkedHashMap; import java.util.Set; public class IterateDemo { public static void main(String a[]){ LinkedHashMapTry it Yourselflhm = 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 LinkedHashMap?
The method containsValue() returns boolean value true if the value exists.
Example
import java.util.LinkedHashMap; public class ValueCheck { public static void main(String a[]){ LinkedHashMapTry it Yourselflhm = 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 LinkedHashMap object?
By calling clear() method on LinkedHashMap object, we can delete all elements at at time.
Example
import java.util.LinkedHashMap; public class Clear { public static void main(String a[]){ LinkedHashMapTry it Yourselflhm = 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); } }
Solve this!!
#20943,20944,20945- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling