Skip to content

Java LinkedHashMap Get All Values Example

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.

The values method returns a Collection view of all the values stored in the map object.

Output

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.

Output

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.

Output

Important Note:

The value collection returned from the values method does not support add and addAll operations.

Output

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

About the author

Leave a Reply

Your email address will not be published.