App下載
首頁(yè)javamapJava Collection - 如何將HashMap轉(zhuǎn)換為T(mén)reeMap,以通過(guò)鍵對(duì)鍵值對(duì)進(jìn)行排序

Java Collection - 如何將HashMap轉(zhuǎn)換為T(mén)reeMap,以通過(guò)鍵對(duì)鍵值對(duì)進(jìn)行排序

我們想知道如何將HashMap轉(zhuǎn)換為T(mén)reeMap,以通過(guò)鍵對(duì)鍵值對(duì)進(jìn)行排序。
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Main {

  public static void main(String[] a) {
    Map<String, String> yourMap = new HashMap<String, String>();
    yourMap.put("1", "one");
    yourMap.put("2", "two");
    yourMap.put("3", "three");

    Map<String, String> sortedMap = new TreeMap<String, String>(yourMap);

    System.out.println(sortedMap);
  }
}