Join Regular Classroom : Visit ClassroomTech

JAVA – codewindow.in

Related Topics

JAVA Programming

Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);

Set<String> keySet = map.keySet();
System.out.println(keySet); // prints [one, two, three]

keySet.remove("two");
System.out.println(map); // prints {one=1, three=3}

System.out.println(keySet.contains("one")); // prints true
System.out.println(keySet.size()); // prints 2

In this example, we create a map with three key-value pairs. We then obtain the KeySet view using the keySet() method and print its contents. Next, we remove the key "two" from the KeySet view, which also removes the corresponding entry from the original map. Finally, we use some of the other methods provided by the KeySet view to check if a key is present and get its size.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories