NavigableMap key-value pair

In this chapter you will learn:

  1. Removes and returns a key-value mapping associated with the least key in this map
  2. 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;
//from j  a  v  a2 s .  co  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;
//from   j  a  v  a  2  s. c om
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:

  1. How to use LinkedHashMap
Home » Java Tutorial » Map
Map interface
Map element adding
Map.Entry class
Map key
Map value
Map key/value search
Map delete/remove
Map comparison
HashMap Class
HashMap search
HashMap clone
TreeMap
TreeMap key
TreeMap head sub map
TreeMap tail sub map
TreeMap sub map
NavigableMap
NavigableMap key
NavigableMap key-value pair
LinkedHashMap Class
IdentityHashMap
SortedMap