158k views
5 votes
What is the signature of a Map method that removes a key-value pair from the Map?

a) void remove(K key)
b) void delete(K key)
c) V remove(K key)
d) K delete(K key)

1 Answer

4 votes

Final answer:

The signature of the Map method that removes a key-value pair from the Map is 'V remove(K key)'.

Step-by-step explanation:

The signature of the Map method that removes a key-value pair from the Map is:

V remove(K key)

This means that the method takes a key as a parameter and returns the corresponding value that was removed from the map. Here's an example:

Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
int removedValue = map.remove("banana");
System.out.println(removedValue); // Output: 2
User CPhil
by
9.5k points