我們先通過一張思維導圖認識一下集合的框架構成,方便理解和記憶。
1、ArrayList和LinkList的區別?
- ArrayList是數組結構,LinkList是鏈表結構。
- ArrayList查詢速度快,增刪改速度慢。
- LinkList剛好和ArrayList相反,LinkList查詢速度慢,增刪改速度快。
2、Collection接口的remove()方法和Iterator接口的remove()方法區別?
- Collection接口的remove()必須通過下標或者集合內的元素去刪除,iterator接口remove()必須配合next()方法一起使用。
3、Array與ArrayList有什麼區別?
- Array是數組,ArrayList是數組集合。
- Array需要指定大小,ArrayList不需要指定大小(ArrayList自動擴容大小,默認大小爲10,當你添加第11個時,ArrayList就會以10*1.5=15去擴容,此時大小爲15,當添加第16個時,ArrayList就會以15*1.5=22去擴容,以此內推擴容)。
4、怎樣將一個數組轉成List,有什麼方法?
- 數組轉list,可以使用Arrays.asList(數組)
- List轉數組,使用list.toArray()
-
public static void main(String[] args) { //數組轉list String[] str = {"zhangsan","lisi","wangwu"}; List<String> arrayList = Arrays.asList(str); //list轉數組 List<String> list = new ArrayList<>(); list.add("zhangsan"); list.add("lisi"); list.add("wangwu"); String[] arrays = list.toArray(new String[list.size()]); }
5、HashSet、TreeSet、LinkedHashSet區別?
- HashSet速度快
- TreeSet有排序功能
- LinkedHashSet先存先取(類似與花名冊,來的早的名字在前面,晚的在後面)
6、HashMap、TreeMap、LinkedHashMap區別?
- HashMap速度快
- TreeMap有排序功能
- LinkedHashMap先存先取(類似與花名冊,來的早的名字在前面,晚的在後面)
7、HashMap和HashSet區別
HashMap | HashSet |
---|---|
調用put()的方法往map裏面填值 | 調用add()的方法往Set裏面填值 |
實現map接口 | 實現set接口 |
以鍵值對存儲 | 存儲對象 |
比HashSet存儲快 | 比HashMap存儲慢 |
使用key計算hashcode | 使用成員對象計算hashcode |
8、HashMap的實現原理
HashMap是以鍵值對存儲,鍵不能重複,值可以重複,通過put(object,object)方法存,通過get(object)取值
9、List、Set、Map之間的區別
List和Set都繼承Collection,但是Map不是Collection的子接口。
List | Set | Map |
---|---|---|
可以有重複值 | 不能有重複值 | 鍵不能重複,值可以重複 |
可以有多個null值 | 只能有一個null值 | 鍵只能有一個null,值可以有多個重複值 |
有序容器,插入的順序和輸出的順序一樣 | 無序容器 | 無序容器 |