This example shows how to get all the values of the TreeMap object using the values method of the TreeMap class. The example also shows how to iterate all values of the TreeMap object.
How to get all values of TreeMap using the values method in Java?
The values
method of the TreeMap class returns all the values contained in the TreeMap object.
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 26 27 28 29 30 |
import java.util.Collection; import java.util.TreeMap; public class TreeMapGetAllValuesExample { public static void main(String[] args) { //an empty TreeMap object TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); //add some mappings tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * To get all values of the TreeMap, use * the values method. */ Collection<String> values = tmapColors.values(); /* * Iterate all the values of the TreeMap */ System.out.println("Iterating all vaues of TreeMap"); for(String value : values){ System.out.println( value ); } } } |
Output
1 2 3 4 |
Iterating all vaues of TreeMap Red Green Blue |
Important Note:
The Collection object returned by the values
method is a view that is backed by the original TreeMap object. That means any changes you make to the collection will be reflected in the TreeMap object, and vice versa.
Let’s have a look at an example of that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); Collection<String> values = tmapColors.values(); //remove a value from the collection object values.remove("Red"); //the respective key-value mapping will be removed from the TreeMap too! System.out.println("Values Collection contains: " + values); System.out.println("TreeMap contains: " + tmapColors); //let's add a mapping to the TreeMap tmapColors.put(4, "Black"); //the respective value will be added to the values collection too! System.out.println("Values Collection contains: " + values); System.out.println("TreeMap contains: " + tmapColors); |
Output
1 2 3 4 |
Values Collection contains: [Green, Blue] TreeMap contains: {2=Green, 3=Blue} Values Collection contains: [Green, Blue, Black] TreeMap contains: {2=Green, 3=Blue, 4=Black} |
As you can see from the output, whatever changes you make to the values collection are also reflected in the original TreeMap object, and vice versa.
Please also note that the add
and addAll
operations are not supported by the Collection object obtained using the values
method. If you try to add something to the values collections using these methods, the code will throw java.lang.UnsupportedOperationException exception as shown below.
1 2 3 4 5 6 7 8 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); Collection<String> values = tmapColors.values(); values.add("Purple"); |
Output
1 2 |
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractCollection.add(Unknown Source) |
Please also visit how to get all keys of TreeMap example.
This example is a part of the Java TreeMap Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap