Open In App

How to Get All the Values of the LinkedHashMap in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

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}


Last Updated : 15 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads