Skip to content
Related Articles

Related Articles

How to Get All the Values of the LinkedHashMap in Java?
  • Difficulty Level : Hard
  • Last Updated : 15 Dec, 2020

LinkedHashMap is a predefined class in Java that is similar to HashMap, contains key and its respective value, unlike HashMap. In LinkedHashMap insertion order is preserved. The task is to get all the values present in our LinkedHashMap that is linked with their respective key. Use Iteration or predefined function to get all the values.

Example:

Input : Key-> 5 : Value->4
    Key-> 8 : Value->2
    Key-> 6 : Value->20
    Key-> 9 : Value->18
    Key-> 1 : Value->66

Output:

Values : [4, 2, 20, 18, 66]

Approach 1:

Use for-each loop to iterate through our LinkedHashMap after each iteration store the value of the respective key in the list. After storing all the values in the list, print the list.

Pseudo Code:



for (Map.Entry<Integer, Integer> i : LHM.entrySet()) {
            list.add(i.getValue());
}

Here, LHM is the name of LinkedHashMap. The list is the name of our list.

Syntax:

hash_map.entrySet()

Return Value: The method returns a set having the same elements as the hash map.

Example:

Java




// Java program to get all the values of the LinkedHashMap
  
import java.util.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // create an instance of linked hashmap
        LinkedHashMap<Integer, Integer> LHM
            = new LinkedHashMap<>();
  
        // Add mappings
        LHM.put(5, 4);
        LHM.put(8, 2);
        LHM.put(6, 20);
        LHM.put(9, 18);
        LHM.put(1, 66);
  
        List<Integer> list = new ArrayList<>();
  
        // Add the values to a list
        for (Map.Entry<Integer, Integer> i :
             LHM.entrySet()) {
            list.add(i.getValue());
        }
  
        System.out.println("values : " + list);
    }
}
Output
values : [4, 2, 20, 18, 66]

Time complexity: O(n)

Approach 2

This approach is an optimal approach for our problem that is to get all the values of our LinkedHashMap. In the above approach, we use iteration to get all the values. In this approach, we use the predefined method to get values of the respective key of our LinkedHashMap.



Use a predefined method to store all the values of every respective key present in our LinkedHashMap. After storing all the values in our list print the list.

Pseudo Code:

Collection<Integer> values = LHM.values();

Here, LHM is the name of LinkedHashMap values is the name of the list contains all the values.

Syntax:

Hash_Map.values()

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

Example:

Java




// Java program to get all the values of the LinkedHashMap
  
import java.util.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // create an instance of linked hashmap
        LinkedHashMap<Integer, Integer> LHM
            = new LinkedHashMap<>();
  
        // Add mappings
        LHM.put(5, 4);
        LHM.put(8, 2);
        LHM.put(6, 20);
        LHM.put(9, 18);
        LHM.put(1, 66);
  
        // get the values of the map
        Collection<Integer> values = LHM.values();
  
        // print the values list
        System.out.println("values : " + values);
    }
}
Output
values : [4, 2, 20, 18, 66]

Time Complexity: O(1).

The Collection view returned by the values() method is backed by the original LinkedHashMap object. 

Java




// Java program to get all the values of the LinkedHashMap
  
import java.util.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // create an instance of linked hashmap
        LinkedHashMap<Integer, Integer> LHM
            = new LinkedHashMap<>();
  
        // Add mappings
        LHM.put(5, 4);
        LHM.put(8, 2);
        LHM.put(6, 20);
        LHM.put(9, 18);
        LHM.put(1, 66);
  
        // get the collection of values
        Collection<Integer> values = LHM.values();
  
        // remove a mapping
        values.remove(20);
  
        // print the values collection
        System.out.println("values : " + values);
  
        // print Linked hashmap
        System.out.println("LinkedHashMap : " + LHM);
  
        // Add mapping
        LHM.put(10, 9);
  
        // print values collection
        System.out.println("values : " + values);
  
        // print linked hash map
        System.out.println("LinkedHashMap : " + LHM);
    }
}
Output
values : [4, 2, 18, 66]
LinkedHashMap : {5=4, 8=2, 9=18, 1=66}
values : [4, 2, 18, 66, 9]
LinkedHashMap : {5=4, 8=2, 9=18, 1=66, 10=9}

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. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :