当前位置:   article > 正文

Java学习笔记——Map之TreeMap_java treemap

java treemap

目录

一、TreeMap相关概念

二、TreeMap相关方法 

三、代码


一、TreeMap相关概念

  • TreeMap实现SortMap接口,是一个有序的key-value集合;
  • TreeMap的数据结构是红黑树;
  • 非线性安全;
  • 支持null值和null键;
  • fail-fast机制;

二、TreeMap相关方法 

1、创建TreeMap

TreeMap<String, String> treeMap = new TreeMap<>();

2、插入

  1. // 插入
  2. treeMap.put("1", "A");
  3. treeMap.put("2", "B");
  4. treeMap.put("3", "C");
  5. treeMap.put("4", "D");
  6. treeMap.put("5", "E");
  7. treeMap.put("6", "F");
  8. treeMap.put("7", "G");
  9. treeMap.put("8", "H");
  10. treeMap.put("9", "I");
  11. System.out.println("------put()------");
  12. System.out.println("treeMap: " + treeMap);
  1. ------put()------
  2. treeMap: {1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I}

3、访问

  1. // 访问
  2. System.out.println("------访问------");
  3. System.out.println("treeMap.get(1):"+treeMap.get("1"));
  4. System.out.println("treeMap.get(2):"+treeMap.get("2"));
  5. System.out.println("treeMap.keySet():"+treeMap.keySet()); // key
  6. System.out.println("treeMap.entrySet():"+treeMap.entrySet()); // key-value
  7. System.out.println("treeMap.firstEntry():"+treeMap.firstEntry()); // the first key-value
  8. System.out.println("treeMap.lastEntry():"+treeMap.lastEntry()); // the last key-value
  9. System.out.println("treeMap.ceilingEntry(3):"+treeMap.ceilingEntry("3")); // 返回大于等于key的key-value
  10. System.out.println("treeMap.floorEntry(6):"+treeMap.floorEntry("6")); // 返回小于等于key的key-value
  11. System.out.println("treeMap.headMap(3):"+treeMap.headMap("3")); // 返回所有小于key的key-value
  12. System.out.println("treeMap.tailMap(3):"+treeMap.tailMap("3")); // 返回所有大于等于key的key-value
  1. ------访问------
  2. treeMap.get(1):A
  3. treeMap.get(2):B
  4. treeMap.keySet():[1, 2, 3, 4, 5, 6, 7, 8, 9]
  5. treeMap.entrySet():[1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I]
  6. treeMap.firstEntry():1=A
  7. treeMap.lastEntry():9=I
  8. treeMap.ceilingEntry(3):3=C
  9. treeMap.floorEntry(6):6=F
  10. treeMap.headMap(3):{1=A, 2=B}
  11. treeMap.tailMap(3):{3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I}

4、判断

  1. // 判断
  2. System.out.println("------判断------");
  3. System.out.println("treeMap.isEmpty():"+treeMap.isEmpty());
  4. System.out.println("treeMap.containsKey(2):"+treeMap.containsKey("3"));
  1. ------判断------
  2. treeMap.isEmpty():false
  3. treeMap.containsKey(2):true

5、修改

  1. // 修改
  2. System.out.println("------replace()------");
  3. System.out.println("treeMap.replace(2, 8):"+treeMap.replace("1", "a"));
  4. System.out.println(treeMap);
  1. ------replace()------
  2. treeMap.replace(2, 8):A
  3. {1=a, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I}

6、遍历

  1. // 遍历
  2. System.out.println("正序treeMap.descendingMap():"+treeMap);
  3. System.out.println("倒叙treeMap.descendingMap():"+treeMap.descendingMap());
  4. System.out.println("------for------");
  5. for (String k: treeMap.keySet()){
  6. System.out.println("key: " + k + ", value: " + treeMap.get(k));
  7. }
  8. for (Map.Entry entry: treeMap.entrySet()){
  9. System.out.println("key - value: " + entry);
  10. }

 

  1. ------for------
  2. 正序treeMap.descendingMap():{1=a, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I}
  3. 倒叙treeMap.descendingMap():{9=I, 8=H, 7=G, 6=F, 5=E, 4=D, 3=C, 2=B, 1=a}
  4. ------for------
  5. key: 1, value: a
  6. key: 2, value: B
  7. key: 3, value: C
  8. key: 4, value: D
  9. key: 5, value: E
  10. key: 6, value: F
  11. key: 7, value: G
  12. key: 8, value: H
  13. key: 9, value: I
  14. key - value: 1=a
  15. key - value: 2=B
  16. key - value: 3=C
  17. key - value: 4=D
  18. key - value: 5=E
  19. key - value: 6=F
  20. key - value: 7=G
  21. key - value: 8=H
  22. key - value: 9=I

三、代码

  1. import java.util.Map;
  2. import java.util.TreeMap;
  3. public class Day33 {
  4. public static void main(String[] args){
  5. // TreeMap
  6. TreeMap<String, String> treeMap = new TreeMap<>();
  7. // 插入
  8. treeMap.put("1", "A");
  9. treeMap.put("2", "B");
  10. treeMap.put("3", "C");
  11. treeMap.put("4", "D");
  12. treeMap.put("5", "E");
  13. treeMap.put("6", "F");
  14. treeMap.put("7", "G");
  15. treeMap.put("8", "H");
  16. treeMap.put("9", "I");
  17. System.out.println("------put()------");
  18. System.out.println("treeMap: " + treeMap);
  19. // 访问
  20. System.out.println("------访问------");
  21. System.out.println("treeMap.get(1):"+treeMap.get("1"));
  22. System.out.println("treeMap.get(2):"+treeMap.get("2"));
  23. System.out.println("treeMap.keySet():"+treeMap.keySet()); // key
  24. System.out.println("treeMap.entrySet():"+treeMap.entrySet()); // key-value
  25. System.out.println("treeMap.firstEntry():"+treeMap.firstEntry()); // the first key-value
  26. System.out.println("treeMap.lastEntry():"+treeMap.lastEntry()); // the last key-value
  27. System.out.println("treeMap.ceilingEntry(3):"+treeMap.ceilingEntry("3")); // 返回大于等于key的key-value
  28. System.out.println("treeMap.floorEntry(6):"+treeMap.floorEntry("6")); // 返回小于等于key的key-value
  29. System.out.println("treeMap.headMap(3):"+treeMap.headMap("3")); // 返回所有小于key的key-value
  30. System.out.println("treeMap.tailMap(3):"+treeMap.tailMap("3")); // 返回所有大于等于key的key-value
  31. // 判断
  32. System.out.println("------判断------");
  33. System.out.println("treeMap.isEmpty():"+treeMap.isEmpty());
  34. System.out.println("treeMap.containsKey(2):"+treeMap.containsKey("3"));
  35. // 修改
  36. System.out.println("------replace()------");
  37. System.out.println("treeMap.replace(2, 8):"+treeMap.replace("1", "a"));
  38. System.out.println(treeMap);
  39. // 遍历
  40. System.out.println("正序treeMap.descendingMap():"+treeMap);
  41. System.out.println("倒叙treeMap.descendingMap():"+treeMap.descendingMap());
  42. System.out.println("------for------");
  43. for (String k: treeMap.keySet()){
  44. System.out.println("key: " + k + ", value: " + treeMap.get(k));
  45. }
  46. for (Map.Entry entry: treeMap.entrySet()){
  47. System.out.println("key - value: " + entry);
  48. }
  49. }
  50. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/1003441
推荐阅读
相关标签
  

闽ICP备14008679号