NavigableMap key-value pair
In this chapter you will learn:
- Removes and returns a key-value mapping associated with the least key in this map
- Removes and returns a key-value mapping associated with the greatest key in this map
Get the least key values
Map.Entry<K,V> pollFirstEntry()
Removes and returns a key-value mapping associated with the least key in this map.
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.Map.Entry;
/*java2s . c o m*/
public class Main {
public static void main(String[] args) {
NavigableMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(2, "two");
map.put(1, "one");
map.put(3, "three");
System.out.println("Original map: " + map + "\n");
Entry firstEntry = map.pollFirstEntry();
System.out.println("First entry: " + firstEntry);
System.out.println("After polling the first entry: " + map + "\n");
Entry lastEntry = map.pollLastEntry();
System.out.println("Last entry:" + lastEntry);
System.out.println("After polling last entry:" + map);
}
}
Greatest key values
Map.Entry<K,V> pollLastEntry()
Removes and returns a key-value mapping associated with the greatest key in this map.
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.Map.Entry;
/* j av a 2 s . c o m*/
public class Main {
public static void main(String[] args) {
NavigableMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(2, "two");
map.put(1, "one");
map.put(3, "three");
System.out.println("Original map: " + map + "\n");
Entry firstEntry = map.pollFirstEntry();
System.out.println("First entry: " + firstEntry);
System.out.println("After polling the first entry: " + map + "\n");
Entry lastEntry = map.pollLastEntry();
System.out.println("Last entry:" + lastEntry);
System.out.println("After polling last entry:" + map);
}
}
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Collections