How to Get All the Values of the LinkedHashMap in Java?
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);
}
}
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
// 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);
}
}
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
// 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);
}
}
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}
Similar Reads
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
2 min read
How to Print all Keys of the LinkedHashMap in Java?
LinkedHashMap is a predefined class in Java that is similar to HashMap, contains a key and its respective value. Unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to print all the Keys present in our LinkedHashMap in java. We have to iterate through each Key in our LinkedHas
2 min read
How to Update the Value of an Existing Key in a LinkedHashMap in Java?
In Java programming, a LinkedHahMap is like HashMap with additional features in the Java Collections framework. It keeps track of the order in which elements were added. A regular HashMap doesn't have a fixed order for elements. LinkedHashMap uses an approach with a doubly-linked list to remember th
3 min read
How to Get a Value From LinkedHashMap by Index in Java?
LinkedHashMap is a predefined class in Java which is similar to HashMap, contain key and its respective value unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to get value from LinkedHashMap by their Index in other words, an order of their insertion. As an advantage of Link
4 min read
How to Add Key-Value pairs to LinkedHashMap in Java?
LinkedHashMap is a Hash table and linked list implementation of the Map interface. In LinkedHashMap order of key-value pair depends on the order in which keys were inserted into the map. Insertion order does not affect if a key is reinserted into the map. Example: Input: Key: 1 Value : 1221 Key: 2 V
2 min read
How to Check if LinkedHashMap Contains a value in Java?
LinkedHashMap is a predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to check if LinkedHashMap contains any value in java. to check we have to iterate through our LinkedHashMap and if
3 min read
How to Convert LinkedHashMap to List in Java?
LinkedHashMap is predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, in LinkedHashMap insertion order is preserved. We to convert LinkedHashMap to ArrayList. A Map store data in pair of Key and Value while converting a LinkedHashMAp to ArrayLis
2 min read
How to Remove Elements from a LinkedHashMap in Java?
A LinkedHashMap is a part of the Collection Framework from java.util package Java and is similar to a HashMap, except that a LinkedHashMap preserves the insertion order among the keys/entries. In this article, we will look at how to remove the elements from a LinkedHashMap in Java. Program to Remove
2 min read
How to Merge Two LinkedHashMaps in Java?
In Java, LinkedHashMap is a class that extends HashMap and maintains the order of elements based on the order of insertion. Merging two LinkedHashMaps involves combining their key-value pairs while ensuring that the order is preserved. In, this article we will explore different approaches to merging
3 min read
Java Program to Sort LinkedHashMap By Values
The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion, but it never maintained the track and order of insertion which the LinkedHashMap provides where the element
3 min read