package com.yjf.esupplier.common.test; import java.util.Set; import java.util.TreeMap; /** * @author shusheng * @description * @Email shusheng@yiji.com * @date 2018/12/18 13:46 */ public class TreeMapDemo { //鍵是紅黑樹結構,能夠保證鍵的排序和惟一性 public static void main(String[] args) { //建立集合對象 TreeMap<String,String> tm = new TreeMap<String,String>(); //添加元素 tm.put("hello", "你好"); tm.put("world", "世界"); tm.put("java", "爪哇"); tm.put("world", "世界2"); tm.put("javaee", "你好ee"); tm.put("aaa", "你好ee"); //遍歷集合 Set<String> set = tm.keySet(); for(String key : set){ String value = tm.get(key); System.out.println(key + "---" + value); } } }