Kotlin-Demo/jTest/TreeMapTest.java

41 lines
955 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package jTest;
import java.util.SortedMap;
import java.util.TreeMap;
public class TreeMapTest {
public static void main(String[] args) {
TreeMap<String, Object> treeMap = new TreeMap<>() {{
put("c", 3);
put("a", 1);
put("b", 2);
put("e", 5);
put("d", 4);
}};
// 会自动排序
System.out.println(treeMap);
// 遍历集合
treeMap.forEach((key, value) -> {
System.out.println("key" + key);
System.out.println("value" + value);
});
SortedMap<String, Object> sortedMap = new TreeMap<>() {{
put("c", 3);
put("a", 1);
put("b", 2);
put("e", 5);
put("d", 4);
}};
// 拿到第一个值
System.out.println(sortedMap.firstKey());
// 最后一个值
System.out.println(sortedMap.lastKey());
}
}