What is AbstractMap keyset() in Java?
Overview
The keyset() method of the AbstractMap class is used to get the set view of the keys in the abstract map.
Syntax
public Set<K> keySet()
Parameters
The method has no parameters.
Return value
The method returns a set view of the keys in the map.
Example
import java.util.*;public class Main{public static void main(String[] args) {AbstractMap<String, String> abstractMap = new HashMap<>();abstractMap.put("hello-1", "Educative");abstractMap.put("hello-2", "edpresso");abstractMap.put("hello-3", "answers");System.out.println(abstractMap.keySet());}}
Explanation
- Line 5: We define an abstract map. The abstract map has the implementation of the hash map.
- Lines 6–8: We add entries to the map using the
put()method. - Line 9: We obtain the keys of the map by using the
keySet()method and print them.
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved