赞
踩
目录
- TreeMap实现SortMap接口,是一个有序的key-value集合;
- TreeMap的数据结构是红黑树;
- 非线性安全;
- 支持null值和null键;
- fail-fast机制;
1、创建TreeMap
TreeMap<String, String> treeMap = new TreeMap<>();
2、插入
- // 插入
- treeMap.put("1", "A");
- treeMap.put("2", "B");
- treeMap.put("3", "C");
- treeMap.put("4", "D");
- treeMap.put("5", "E");
- treeMap.put("6", "F");
- treeMap.put("7", "G");
- treeMap.put("8", "H");
- treeMap.put("9", "I");
- System.out.println("------put()------");
- System.out.println("treeMap: " + treeMap);
- ------put()------
- treeMap: {1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I}
3、访问
- // 访问
- System.out.println("------访问------");
- System.out.println("treeMap.get(1):"+treeMap.get("1"));
- System.out.println("treeMap.get(2):"+treeMap.get("2"));
-
- System.out.println("treeMap.keySet():"+treeMap.keySet()); // key
- System.out.println("treeMap.entrySet():"+treeMap.entrySet()); // key-value
-
- System.out.println("treeMap.firstEntry():"+treeMap.firstEntry()); // the first key-value
- System.out.println("treeMap.lastEntry():"+treeMap.lastEntry()); // the last key-value
-
- System.out.println("treeMap.ceilingEntry(3):"+treeMap.ceilingEntry("3")); // 返回大于等于key的key-value
- System.out.println("treeMap.floorEntry(6):"+treeMap.floorEntry("6")); // 返回小于等于key的key-value
-
- System.out.println("treeMap.headMap(3):"+treeMap.headMap("3")); // 返回所有小于key的key-value
- System.out.println("treeMap.tailMap(3):"+treeMap.tailMap("3")); // 返回所有大于等于key的key-value
- ------访问------
- treeMap.get(1):A
- treeMap.get(2):B
- treeMap.keySet():[1, 2, 3, 4, 5, 6, 7, 8, 9]
- treeMap.entrySet():[1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I]
- treeMap.firstEntry():1=A
- treeMap.lastEntry():9=I
- treeMap.ceilingEntry(3):3=C
- treeMap.floorEntry(6):6=F
- treeMap.headMap(3):{1=A, 2=B}
- treeMap.tailMap(3):{3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I}
4、判断
- // 判断
- System.out.println("------判断------");
- System.out.println("treeMap.isEmpty():"+treeMap.isEmpty());
- System.out.println("treeMap.containsKey(2):"+treeMap.containsKey("3"));
- ------判断------
- treeMap.isEmpty():false
- treeMap.containsKey(2):true
5、修改
- // 修改
- System.out.println("------replace()------");
- System.out.println("treeMap.replace(2, 8):"+treeMap.replace("1", "a"));
- System.out.println(treeMap);
- ------replace()------
- treeMap.replace(2, 8):A
- {1=a, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I}
6、遍历
- // 遍历
- System.out.println("正序treeMap.descendingMap():"+treeMap);
- System.out.println("倒叙treeMap.descendingMap():"+treeMap.descendingMap());
- System.out.println("------for------");
- for (String k: treeMap.keySet()){
- System.out.println("key: " + k + ", value: " + treeMap.get(k));
- }
-
- for (Map.Entry entry: treeMap.entrySet()){
- System.out.println("key - value: " + entry);
- }
- ------for------
- 正序treeMap.descendingMap():{1=a, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I}
- 倒叙treeMap.descendingMap():{9=I, 8=H, 7=G, 6=F, 5=E, 4=D, 3=C, 2=B, 1=a}
- ------for------
- key: 1, value: a
- key: 2, value: B
- key: 3, value: C
- key: 4, value: D
- key: 5, value: E
- key: 6, value: F
- key: 7, value: G
- key: 8, value: H
- key: 9, value: I
- key - value: 1=a
- key - value: 2=B
- key - value: 3=C
- key - value: 4=D
- key - value: 5=E
- key - value: 6=F
- key - value: 7=G
- key - value: 8=H
- key - value: 9=I
- import java.util.Map;
- import java.util.TreeMap;
-
- public class Day33 {
- public static void main(String[] args){
- // TreeMap
- TreeMap<String, String> treeMap = new TreeMap<>();
-
- // 插入
- treeMap.put("1", "A");
- treeMap.put("2", "B");
- treeMap.put("3", "C");
- treeMap.put("4", "D");
- treeMap.put("5", "E");
- treeMap.put("6", "F");
- treeMap.put("7", "G");
- treeMap.put("8", "H");
- treeMap.put("9", "I");
- System.out.println("------put()------");
- System.out.println("treeMap: " + treeMap);
-
- // 访问
- System.out.println("------访问------");
- System.out.println("treeMap.get(1):"+treeMap.get("1"));
- System.out.println("treeMap.get(2):"+treeMap.get("2"));
-
- System.out.println("treeMap.keySet():"+treeMap.keySet()); // key
- System.out.println("treeMap.entrySet():"+treeMap.entrySet()); // key-value
-
- System.out.println("treeMap.firstEntry():"+treeMap.firstEntry()); // the first key-value
- System.out.println("treeMap.lastEntry():"+treeMap.lastEntry()); // the last key-value
-
- System.out.println("treeMap.ceilingEntry(3):"+treeMap.ceilingEntry("3")); // 返回大于等于key的key-value
- System.out.println("treeMap.floorEntry(6):"+treeMap.floorEntry("6")); // 返回小于等于key的key-value
-
- System.out.println("treeMap.headMap(3):"+treeMap.headMap("3")); // 返回所有小于key的key-value
- System.out.println("treeMap.tailMap(3):"+treeMap.tailMap("3")); // 返回所有大于等于key的key-value
-
-
-
- // 判断
- System.out.println("------判断------");
- System.out.println("treeMap.isEmpty():"+treeMap.isEmpty());
- System.out.println("treeMap.containsKey(2):"+treeMap.containsKey("3"));
-
- // 修改
- System.out.println("------replace()------");
- System.out.println("treeMap.replace(2, 8):"+treeMap.replace("1", "a"));
- System.out.println(treeMap);
-
- // 遍历
- System.out.println("正序treeMap.descendingMap():"+treeMap);
- System.out.println("倒叙treeMap.descendingMap():"+treeMap.descendingMap());
- System.out.println("------for------");
- for (String k: treeMap.keySet()){
- System.out.println("key: " + k + ", value: " + treeMap.get(k));
- }
-
- for (Map.Entry entry: treeMap.entrySet()){
- System.out.println("key - value: " + entry);
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。