Thursday, October 16, 2025
HomeLanguagesJavaHow to Convert all LinkedHashMap Values to a List in Java?

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 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);
        }
    }
}


Output

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);
        }
    }
}


Output

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);
        }
    }
}


Output

List contains:
Geeks
4
Geeks
Welcomes
You
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11953 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS