The Java LinkedHashMap class is a hash table and doubly linked List based implementation of Java’s Map interface. It is more similar to the HashMap class but the advantages of HashMap class in quick insertion, deletion, and search operation, but it does not store the elements in the insertion order. Still, if you are using LinkedHashMap then you can access the elements in their insertion order.
Key Points of LinkedHashMap
- It Stores the elements in the key-value pairs as HashMap.
- It is not allowing duplicate keys to insert.
- It allows one null key and multiple Null values.
- It stores the elements on the same order on which they have entered.
- LinkedHashMap is not threaded like HashMap, but you can make it thread-safe externally.
How to Declare LinkedHashMap Class
You can declare the LinkedHashMap by using the below syntax:
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>();
Constructors in LinkedHashMap
The LinkedHashMap accepts five types of constructors:
- LinkedHashMap(): This is used to construct a default LinkedHashMap constructor.
- LinkedHashMap(int capacity): It is used to initialize a particular LinkedHashMap with a specified capacity.
- LinkedHashMap(Map m_a_p): It is used to initialize a particular LinkedHashMap with the elements of the specified map.
- LinkedHashMap(int capacity, float fillRatio): It is used to initialize both the capacity and fill ratio for a LinkedHashMap.
- LinkedHashMap(int capacity, float fillRatio, boolean Order): This constructor is also used to initialize both the capacity and fill ratio for a LinkedHashMap along with whether to follow the insertion order or not.
True is passed for the last access order.
False is passed for insertion order.
LinkedHashMap Methods And Various Operation In Java: Different LinkedHashMap Methods Used in In Java With Example?
package com.java.Softwaretestingblog; import java.util.LinkedHashMap; public class BasicLinkedHashMap { public static void main(String[] args) { // TODO Auto-generated method stub //www.softwaretestingblog.in LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>(); 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); } }
Output:
{one=This is first element, two=This is second element, four=this element inserted at 3rd position} Getting value for key 'one': This is first element Size of the map: 3 Is map empty? false Contains key 'two'? true Contains value 'This is first element'? true delete element 'one': This is first element {two=This is second element, four=this element inserted at 3rd position}
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Add Values In LinkedHashMap Java Program & Explanation?
package com.softwaretestingo.java.basics; import java.util.LinkedHashMap; public class MyMapValueCheck { public static void main(String[] args) { LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>(); 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")); } }
Ref: article
Leave a Reply