Open In App

How to Convert all LinkedHashMap Key-Value pairs to List in Java?

Last Updated : 28 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their insertion order. 

We have to convert all LinkedHashMap key-values pairs to list, so we have a LinkedHashMap object that contains some pairs of key-values and we have to convert it into list using keySet() and values() method.

keySet(): This method is used to get the keys of its called LinkedHashMap object.

values(): This method is used to get the values of its called LinkedHashMap object.

Approach:

  • Create a LinkedHashMap that contain some keys and values pair.
  • Create a List1 that contain the keys of LinkedHashMap object.
  • Create a List2 that contain the values of LinkedHashMap object.

Code:

Java




// Java program to Convert all LinkedHashMap
// Key-Value pairs to List
  
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
  
public class Sias {
  
    public static void main(String[] args)
    {
  
        // create LinkedHashMap
        LinkedHashMap<Integer, String> lhmap
            = new LinkedHashMap<Integer, String>();
  
        // add elements in LinkedHashMap
        lhmap.put(1, "One");
        lhmap.put(2, "Two");
        lhmap.put(3, "Three");
        lhmap.put(4, "Four");
        lhmap.put(5, "Five");
  
        // Create List 1 that store keys
        List<Integer> list1
            = new ArrayList<Integer>(lhmap.keySet());
  
        // display List 1
        System.out.println("List 1 - " + list1);
  
        // Create List 2 that store values
        List<String> list2
            = new ArrayList<String>(lhmap.values());
  
        // display List 1
        System.out.println("List 2 - " + list2);
    }
}


Output

List 1 - [1, 2, 3, 4, 5]
List 2 - [One, Two, Three, Four, Five]


Similar Reads

How to Add Key-Value pairs to LinkedHashMap in Java?
LinkedHashMap is a Hash table and linked list implementation of the Map interface. In LinkedHashMap order of key-value pair depends on the order in which keys were inserted into the map. Insertion order does not affect if a key is reinserted into the map. Example: Input: Key: 1 Value : 1221 Key: 2 Value : 2112 Output: Keys : [1,2] Values : [1221,21
2 min read
How to Update the Value of an Existing Key in a LinkedHashMap in Java?
In Java programming, a LinkedHahMap is like HashMap with additional features in the Java Collections framework. It keeps track of the order in which elements were added. A regular HashMap doesn't have a fixed order for elements. LinkedHashMap uses an approach with a doubly-linked list to remember the order of inserted keys. Key terminologies: Key-v
3 min read
How to Convert all LinkedHashMap Values to a List in Java?
The task is to convert all LinkedHashMap values to a list in java. LinkedHashMap is an implementation of a Map. The Map and List are two different data structures. The Map stores key-value pairs while the List is an ordered collection of elements. To convert all values of the LinkedHashMap to a List using the values() method. The values() method of
2 min read
How to Convert LinkedHashMap to List in Java?
LinkedHashMap is predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, in LinkedHashMap insertion order is preserved. We to convert LinkedHashMap to ArrayList. A Map store data in pair of Key and Value while converting a LinkedHashMAp to ArrayList we will store keys of Map in a separate List, si
2 min read
How to Eliminate Duplicate User Defined Objects as a Key from Java LinkedHashMap?
Duplicate user-defined objects as a key from Java LinkedHashMap can be removed and achieved by implementing equals and hashcode methods at the user-defined objects. Example: Input : LinkedHashMap = [{[Apple, 40], Kashmir}, {[Grapes, 80], Nashik}] Duplicate key = {[Grapes, 80], Delhi} Output: LinkedHashMap = [{[Apple, 40], Kashmir}, {[Grapes, 80], D
2 min read
Convert ArrayList to LinkedHashMap in Java
LinkedHashMap is a predefined class in Java that is similar to HashMap, contain key and its respective value unlike HashMap, In LinkedHashMap insertion order is preserved. We need to convert ArrayList to LinkedHashMap the Key value of LinkedHashMap will be an index of ArrayList, basically, LinkedHashMap will also be the same as ArrayList in terms o
3 min read
How to Convert LinkedHashMap to Two Arrays in Java?
LinkedHashMap is a pre-defined class in java like HashMap. The only difference between LinkedHashMap and HashMap is LinkedHashMap preserve insertion order while HashMap does not preserve insertion order. LinkedHashMap can convert into two Arrays in java where one array is for key in LinkedHashMap and another array for Values. Example : Input : Link
2 min read
How to Check if LinkedHashMap Contains a value in Java?
LinkedHashMap is a predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to check if LinkedHashMap contains any value in java. to check we have to iterate through our LinkedHashMap and if we get any value we return true. Example : Input
3 min read
How to Get a Value From LinkedHashMap by Index in Java?
LinkedHashMap is a predefined class in Java which is similar to HashMap, contain key and its respective value unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to get value from LinkedHashMap by their Index in other words, an order of their insertion. As an advantage of LinkedHashMap we know that the order of their insertio
4 min read
How to Print all Mappings of the LinkedHashMap in Java?
The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. LinkedHashMap in Java is an implementation that combines HashTable and LinkedList implementation. It implements the Map interface. The key-value pairs of LinkedHashMap have a predictable order of iteration. We can use the entrySet
3 min read