Let’s learn TreeMap pollFirstEntry() method in java.
TreeMap pollFirstEntry() method in java
pollFirstEntry() method of TreeMap class removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
Syntax:
public Map.Entry<K, V> pollFirstEntry()
Now let’s see example on TreeMap pollFirstEntry() method.
import java.util.TreeMap; public class TreeMapPollFirstEntryMethodExample { public static void main(String[] args) { TreeMap<Integer, String> tm = new TreeMap<Integer, String>(); tm.put(63, "yellow"); tm.put(95, "red"); tm.put(96, "green"); tm.put(21, "violet"); tm.put(28, "blue"); System.out.println("TreeMap before using pollFirstEntry() method: " + tm); System.out.println("Value is: " + tm.pollFirstEntry()); System.out.println("TreeMap after using pollFirstEntry() method: " + tm); } }
Output:
TreeMap before using pollFirstEntry() method: {21=violet, 28=blue, 63=yellow, 95=red, 96=green}
Value is: 21=violet
TreeMap after using pollFirstEntry() method: {28=blue, 63=yellow, 95=red, 96=green}
Also read – java overview