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
// 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);        }    }} |
List contains: 11 22 33
Example 2:
Java
// 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);        }    }} |
List contains: Geeks 4 Geeks Welcomes You
Another Method : Using List.copyOf()
In java, we can convert the set to list by using List.copyOf(object). For this, we have to import the package java.util.List.*. It is a static factory method which creates the list object from another collection or object.Â
Java
import java.util.*;class GFG {    public static void main (String[] args) {       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 = List.copyOf(lhmap.values());         Â
        // print values        System.out.println("List contains:");        for (String value : listValues) {            System.out.println(value);        }    }} |
List contains: Geeks 4 Geeks Welcomes You
