Java TreeMap get sorted key one by one

Description

Java TreeMap get sorted key one by one

import java.util.TreeMap;

public class Main {
  public static void main(String[] a) {

    TreeMap<String, String> map = new TreeMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    String last = map.lastKey();/*from  w w w.  j av  a  2 s.  co m*/
    do {
      System.out.println(last);
      last = map.headMap(last).lastKey();
    } while (last != map.firstKey());
    System.out.println(map.firstKey());

  }
}



PreviousNext

Related