This example shows how to get all values from the LinkedHashMap in Java using the values method. The example also shows how to iterate all values of the LinkedHashMap.
How to get all the values of the LinkedHashMap in Java?
The values
method of the LinkedHashMap class returns all the values of the map.
1 |
public Collection<V> values() |
The values
method returns a Collection view of all the values stored in the map object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Collection; import java.util.LinkedHashMap; public class LinkedHashMapGetAllValuesExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); /* * To get all the values stored in the * LinkedHashMap, use the values method. * It returns a Collection. */ Collection<String> values = lhmap.values(); System.out.println(values); } } |
Output
1 |
[One, Two, Three, Four, Five] |
The Collection view returned by the values
method is backed by the original LinkedHashMap object. It means any changes you make to the Collection will be reflected in the original LinkedHashMap object, and vice versa.
Here is the example to show that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import java.util.Collection; import java.util.LinkedHashMap; public class LinkedHashMapGetAllValuesExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); Collection<String> values = lhmap.values(); /* * Let's remove a value from the values Collection. * The mappings will be removed from the LinkedHashMap too. */ values.remove("Three"); System.out.println("Value collection contains: " + values); System.out.println("LinkedHashMap contains: " + lhmap); /* * Let's add a mapping to the LinkedHashMap. The * added value will be reflected in the values * Collection too. */ lhmap.put(6, "Six"); System.out.println("Value collection contains: " + values); System.out.println("LinkedHashMap contains: " + lhmap); } } |
Output
1 2 3 4 |
Value collection contains: [One, Two, Four, Five] LinkedHashMap contains: {1=One, 2=Two, 4=Four, 5=Five} Value collection contains: [One, Two, Four, Five, Six] LinkedHashMap contains: {1=One, 2=Two, 4=Four, 5=Five, 6=Six |
As you can see from the output, whatever changes you make to any of them, other changes too.
How to iterate over all values?
Use the values
method of the LinkedHashMap class to get values stored in the map object first. Get an iterator for the values collection and then iterate through it using the hasNext
and the next
methods as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); //get all values Collection<String> values = lhmap.values(); //get an iterator Iterator<String> iterator = values.iterator(); while( iterator.hasNext() ){ System.out.println( iterator.next() ); } |
Output
1 2 3 4 5 |
One Two Three Four Five |
Important Note:
The value collection returned from the values
method does not support add and addAll operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); //get all values Collection<String> values = lhmap.values(); /* * Not supported! Will throw exception */ values.add("Hello"); |
Output
1 2 |
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractCollection.add(Unknown Source) |
As you can see from the output, calling the add
method on values collection will throw java.lang.UnsupportedOperationException exception.
This example is a part of the LinkedHashMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap