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

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping