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
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
LinkedHashMap<Integer, Integer> LHM
= new LinkedHashMap<>();
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<>();
for (Map.Entry<Integer, Integer> i :
LHM.entrySet()) {
list.add(i.getValue());
}
System.out.println( "values : " + list);
}
}
|
Outputvalues : [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
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
LinkedHashMap<Integer, Integer> LHM
= new LinkedHashMap<>();
LHM.put( 5 , 4 );
LHM.put( 8 , 2 );
LHM.put( 6 , 20 );
LHM.put( 9 , 18 );
LHM.put( 1 , 66 );
Collection<Integer> values = LHM.values();
System.out.println( "values : " + values);
}
}
|
Outputvalues : [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
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
LinkedHashMap<Integer, Integer> LHM
= new LinkedHashMap<>();
LHM.put( 5 , 4 );
LHM.put( 8 , 2 );
LHM.put( 6 , 20 );
LHM.put( 9 , 18 );
LHM.put( 1 , 66 );
Collection<Integer> values = LHM.values();
values.remove( 20 );
System.out.println( "values : " + values);
System.out.println( "LinkedHashMap : " + LHM);
LHM.put( 10 , 9 );
System.out.println( "values : " + values);
System.out.println( "LinkedHashMap : " + LHM);
}
}
|
Outputvalues : [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}