【笔记】Java的LinkedHashMap和TreeMap

前言

Java的LinkedHashMap和TreeMap学习笔记

继承结构

graph TD
  Object --> AbstractMap
  AbstractMap --> HashMap
  HashMap --> LinkedHashMap

  AbstractMap --> TreeMap

  Map --> HashMap

  Map --> SortedMap
  SortedMap --> NavigableMap
  NavigableMap --> TreeMap

  style Object fill:#f0f8ff,stroke:#696969
  style AbstractMap fill:#f0f8ff,stroke:#696969
  style HashMap fill:#f0f8ff,stroke:#696969
  style LinkedHashMap fill:#f0f8ff,stroke:#696969

  style TreeMap fill:#f0f8ff,stroke:#696969

  style Map fill:#f0f8ff,stroke:#4169e1
  
  style SortedMap fill:#f0f8ff,stroke:#4169e1
  style NavigableMap fill:#f0f8ff,stroke:#4169e1

LinkedHashMap

创建对象

  • 底层维护插入顺序
1
LinkedHashMap<Object, Object> linkedSet = new LinkedHashMap<>();

Map的相关方法

传送门

映射遍历

传送门

TreeMap

创建对象

  • 底层维护排序顺序,默认正序排序
1
TreeMap<Object, Object> treeMap = new TreeMap<>();

创建对象并指定排序规则

1
2
3
TreeMap<Object, Object> treeMap = new TreeMap<>((Object o1, Object o2) -> {
return o1.compareTo(o2);
});

Map的相关方法

传送门

映射遍历

传送门

完成