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.
1 |
public Set<K> keySet() |
The keySet
method returns a Set view of all the keys contained in the LinkedHashMap 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 |
import java.util.LinkedHashMap; import java.util.Set; public class LinkedHashMapKeySetExample { 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 keys of the LinkedHashMap, use * the keySet method */ Set<Integer> setKeys = lhmap.keySet(); System.out.println(setKeys); } } |
Output
1 |
[1, 2, 3, 4, 5] |
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.
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 |
import java.util.LinkedHashMap; import java.util.Set; public class LinkedHashMapKeySetExample { 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"); Set<Integer> setKeys = lhmap.keySet(); /* * remove a key from the key set. The * change will be reflected in the LinkedHashMap too */ setKeys.remove(2); System.out.println("Key set contains: " + setKeys); System.out.println("LinkedHashMap contains: " + lhmap); /* * Add a mapping to LinkedHashMap, the change * will be reflected in the key set too. */ lhmap.put(6, "Six"); System.out.println("Key set contains: " + setKeys); System.out.println("LinkedHashMap contains: " + lhmap); } } |
Output
1 2 3 4 |
Key set contains: [1, 3, 4, 5] LinkedHashMap contains: {1=One, 3=Three, 4=Four, 5=Five} Key set contains: [1, 3, 4, 5, 6] LinkedHashMap contains: {1=One, 3=Three, 4=Four, 5=Five, 6=Six} |
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.
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"); Set<Integer> setKeys = lhmap.keySet(); //get an iterator Iterator<Integer> iterator = setKeys.iterator(); //use hasNext and next methods while( iterator.hasNext() ){ System.out.println( iterator.next() ); } |
Output
1 2 3 4 5 |
1 2 3 4 5 |
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