☰ 总览更新 10-13 10:24

15 · 泛型 + 四种集合对比

我的原话

泛型 = 给集合贴标签:只装什么

四种集合对比(真跑过 T30.java)

像什么 重复? 顺序 怎么取
ArrayList 排队 可以 按放进去的顺序 get(0)
HashMap 柜子 钥匙不能重复,再 put=换掉 不保证 get("钥匙")
HashSet 袋子 不行 不保证 只能 contains
TreeSet 会排序的袋子 不行 自动从小到大 只能 contains
ArrayList<Integer> list   add 30,10,30          → [30, 10, 30]     get(0)=30
HashMap<String,Integer>   put 张三20,李四18,张三21 → {李四=18, 张三=21}
HashSet<String>           add 香蕉,苹果,香蕉      → [香蕉, 苹果]
TreeSet<Integer>          add 30,10,30,20        → [10, 20, 30]

TreeSet 不用自己写 compareTo:Integer 是 Java 写好的类,早就签了 Comparable(14-Set和TreeSet)