keySet(): This method is used to fetch the Set view of all keys. It returns all the key set. This Set worked on backed of TreeMap, if any change makes in TreeMap also reflects in this Set and vice versa. So, this keySet in Java returns type is Set.
Set<K> keySet();
Where, K represents the type keys in TreeMap.
import java.util.HashMap; public class ExampleOfHashMap { public static void main(String[] args) { HashMap<Integer,String> listOfNames = new HashMap<Integer, String>(); listOfNames.put(1, "JAVA"); listOfNames.put(2, "GOAL"); listOfNames.put(3, "RAVI"); System.out.println("Key set of Map = "+listOfNames.keySet()); } }
Output: Key set of Map = [1, 2, 3]