Skip to content

Java LinkedHashMap keySet – Get All Keys Example

This example shows how to get all keys from the Java LinkedHashMap using the keySet method. This example also shows how to iterate over and remove the keys from LinkedHashMap.

How to get all keys of the LinkedHashMap using the keySet method?

The keySet method of the LinkedHashMap class returns all keys of the map object.

The keySet method returns a Set view of all the keys contained in the LinkedHashMap object.

Output

The Set returned by the keySet method is backed by the original LinkedHashMap object. That means any changes you make to the Set will also be reflected in the LinkedHashMap object, and vice-versa.

Output

How to iterate all keys using the keySet method?

Use the keySet method to get all the keys from the LinkedHashMap as given above. Once you get the key Set, get an iterator for it using the iterator method and then use the hasNext and the next methods to iterate over it as given below.

Output

Note: The key set obtained using the keySet method of the LinkedHashMap class does not support add and addAll operations.

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.