Skip to content
Related Articles

Related Articles

How to Convert all LinkedHashMap Values to a List in Java?
  • Last Updated : 17 Dec, 2020

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 the LinkedHashMap class returns a Collection view of all the values contained in the map object. You can then use this collection to convert it to a List object.

Syntax:

LinkedHashMap.values()

Return Value: The method is used to return a collection view containing all the values of the map.

Example 1:



Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to Convert all LinkedHashMap values to a
// List
  
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
  
public class LinkedHashMapToListExample {
  
    public static void main(String[] args)
    {
  
        // instance of linkedhashmap
        LinkedHashMap<Integer, Integer> lhmap
            = new LinkedHashMap<Integer, Integer>();
  
        // add mappings
        lhmap.put(1, 11);
        lhmap.put(2, 22);
        lhmap.put(3, 33);
  
        // convert values to a list
        List<Integer> listValues
            = new ArrayList<Integer>(lhmap.values());
  
        // print values
        System.out.println("List contains:");
        for (Integer value : listValues) {
            System.out.println(value);
        }
    }
}

chevron_right


Output

List contains:
11
22
33

Example 2:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to Convert all LinkedHashMap values to a
// List
  
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
  
public class LinkedHashMapToListExample {
  
    public static void main(String[] args)
    {
  
        // instance of linkedhashmap
        LinkedHashMap<Integer, String> lhmap
            = new LinkedHashMap<Integer, String>();
  
        // add mappings
        lhmap.put(10, "Geeks");
        lhmap.put(15, "4");
        lhmap.put(20, "Geeks");
        lhmap.put(25, "Welcomes");
        lhmap.put(30, "You");
  
        // convert values to a list
        List<String> listValues
            = new ArrayList<String>(lhmap.values());
  
        // print values
        System.out.println("List contains:");
        for (String value : listValues) {
            System.out.println(value);
        }
    }
}

chevron_right


Output

List contains:
Geeks
4
Geeks
Welcomes
You

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :