Java NavigableMap get and remove first/last entry

Description

Java NavigableMap get and remove first/last entry

import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.Map.Entry;

public class Main {
  public static void main(String[] args) {
    NavigableMap<Integer, String> map = new TreeMap<Integer, String>();
    map.put(2, "HTML");
    map.put(1, "CSS");
    map.put(3, "Java");
    System.out.println("Original map: " + map + "\n");

    Entry<Integer, String> firstEntry = map.pollFirstEntry();
    System.out.println("First entry: " + firstEntry);
    System.out.println("After polling the first entry: " + map + "\n");

    Entry<Integer, String> lastEntry = map.pollLastEntry();
    System.out.println("Last entry:" + lastEntry);
    System.out.println("After polling last entry:" + map);
  }/*from  www. ja  va  2  s .c om*/
}



PreviousNext

Related