Saturday, August 30, 2025
HomeLanguagesJavaConvert HashMap to LinkedList in Java

Convert HashMap to LinkedList in Java

HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values.

LinkedList is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node.

Example:

Input:    
    l.put(2, 5);
    l.put(3, 6);
    l.put(4, 1);
    l.put(8, 2);
    
Output:    
    LinkedList of key-> [2, 3, 4, 8]
    LinkedList of values-> [5, 6, 1, 2]
    respectively the output for key and values.

Syntax of keySet() Method

hash_map.keySet()

Parameters: The method does not take any parameters.

Return Value: The method returns a set having the keys of the hash map.

Syntax of values() Method

Hash_Map.values()

Parameters: The method does not accept any parameters.

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

PseudoCode

List<Integer> list = new LinkedList<>(l.keySet());
List<Integer> listOfValue = new LinkedList<>(l.values());

Example 1:

Java




// Java program to Convert HashMap to LinkedList
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // create a hashmap instance
        HashMap<Integer, Integer> l = new HashMap<>();
  
        // add mappings
        l.put(2, 5);
        l.put(3, 6);
        l.put(4, 1);
        l.put(8, 2);
  
        // list of keys
        List<Integer> list = new LinkedList<>(l.keySet());
  
        // list of values
        List<Integer> listOfValue
            = new LinkedList<>(l.values());
  
        // print the list
        System.out.println("LinkedList of key-> " + list);
        System.out.println("LinkedList of values-> "
                           + listOfValue);
    }
}


Output

LinkedList of key-> [2, 3, 4, 8]
LinkedList of values-> [5, 6, 1, 2]

Example 2:

Java




// Java program to Convert HashMap to LinkedList 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    { 
          // create a hashmap instance
        HashMap<Integer, String> l = new HashMap<>();
        
        // add mappings
        l.put(1, "Geeks");
        l.put(4, "For");
        l.put(3, "Geeks");
        
        // list of keys
        List<Integer> list = new LinkedList<>(l.keySet());
        
        // list of values
        List<String> listOfValue
            = new LinkedList<>(l.values());
        
        // print the list
        System.out.println("LinkedList of key-> " + list);
        System.out.println("LinkedList of values-> "
                           + listOfValue);
    }
}


Output

LinkedList of key-> [1, 3, 4]
LinkedList of values-> [Geeks, Geeks, For]
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7013 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS