Java中級進階之集合框架比較

寫在前邊

大家好,今天我又來更新幹貨了,兩天沒更新了。這幾天我在收集和整理公衆號要接下來要更新的知識內容,確保每更新一篇讓每一個讀者都有收穫。

這幾天我也盡全力的爲最近羣裏組織的企業項目做準備,確保第一批參與的每一位小夥伴能夠在實際項目中真正的提高技能和收穫項目經驗。所以公衆號就耽擱了兩天。我估計今天再不更新幹貨小夥伴們都要取關了,哈哈!

今天帶來整理的內容是「Java 集合框架之間的比較」。也是面試中面試官最喜歡提問到的,今天的內容希望每個小夥伴都要好好掌握,重中之重。

Java中級進階之集合框架比較

JAVA集合框架之間的關係

1. ArrayList 與 HashSet

1.1 是否有順序

① ArrayList: 有順序

② HashSet: 無順序

代碼:


 1ArrayList<Integer> numberList =new ArrayList<Integer>();
 2//List中的數據按照插入順序存放
 3System.out.println("----------List----------");
 4System.out.println("向 List 中插入 9 5 1");
 5numberList.add(9);
 6numberList.add(5);
 7numberList.add(1);
 8System.out.println("List 按照順序存放數據:");
 9System.out.println(numberList);
10System.out.println("----------Set----------");
11HashSet<Integer> numberSet =new HashSet<Integer>();
12System.out.println("向Set 中插入9 5 1");
13//Set中的數據不是按照插入順序存放
14numberSet.add(9);
15numberSet.add(5);
16numberSet.add(1);
17System.out.println("Set 不是按照順序存放數據:");
18System.out.println(numberSet);

1.2 能否重複

① List 中的數據可以重複

② Set 中的數據不能夠重複

重複判斷標準是 :

① 首先看 hashcode 是否相同

② 如果 hashcode 不同,則認爲是不同數據

③ 如果 hashcode 相同,再比較 equals,如果 equals 相同,則是相同數據,否則是不同數據

代碼:


 1ArrayList<Integer> numberList = newArrayList<Integer>();
 2//List中的數據可以重複
 3System.out.println("----------List----------");
 4System.out.println("向List 中插入 9 9");
 5numberList.add(9);
 6numberList.add(9);
 7System.out.println("List 中出現兩個9:");
 8System.out.println(numberList);
 9System.out.println("----------Set----------");
10HashSet<Integer> numberSet =newHashSet<Integer>();
11System.out.println("向Set 中插入9 9");
12// Set中的數據不能重複
13numberSet.add(9);
14numberSet.add(9);
15System.out.println("Set 中只會保留一個 9:");
16System.out.println(numberSet);
  1. ArrayList 與 LinkedList

2.1 ArrayList 和 LinkedList 的區別

① ArrayList 插入,刪除數據慢。

② LinkedList 插入,刪除數據快

③ ArrayList 是順序結構,所以定位很快,指哪找哪。 就像電影院位置一樣,有了電影票,一下就找到位置了。LinkedList 是鏈表結構,就像手裏的一串佛珠,要找出第 99 個佛珠,必須得一個一個的數過去,所以定位慢。

2.2 插入數據

代碼:


 1public static void main(String[] args) {
 2      List<Integer> l;
 3      l = new ArrayList<>();
 4      insertFirst(l, "ArrayList");
 5      l = new LinkedList<>();
 6      insertFirst(l, "LinkedList");
 7}
 8private static void insertFirst(List<Integer> l, String type) {
 9      int total = 1000 * 100;
10      final int number = 5;
11      //獲取當前系統時間
12      long start = System.currentTimeMillis();
13      for (int i = 0; i < total; i++) {
14      l.add(0, number);
15}
16      long end = System.currentTimeMillis();
17      System.out.printf("在%s 最前面插入%d條數據,總共耗時 %d 毫秒 %n", type, total, end - start);
18}

2.3 定位數據

代碼:


 1public static void main(String[] args) {
 2     List<Integer> l;
 3     l = new ArrayList<>();
 4     modify(l, "ArrayList");
 5     l = new LinkedList<>();
 6     modify(l, "LinkedList");
 7}
 8private static void modify(List<Integer> l, String type) {
 9    int total = 100 * 1000;
10    int index = total/2;
11    final int number = 5;
12    //初始化
13    for (int i = 0; i < total; i++) {
14       l.add(number);
15    }
16    long start = System.currentTimeMillis();
17    for (int i = 0; i < total; i++) {
18      int n = l.get(index);
19      n++;
20      l.set(index, n);
21    }
22    long end = System.currentTimeMillis();
23    System.out.printf("%s總長度是%d,定位到第%d個數據,取出來,加1,再放回去%n 重複%d遍,總共耗時 %d 毫秒 %n", type,total, index,total, end - start);
24    System.out.println();
25}
  1. HashMap 與 HashTable

3.1 HashMap和Hashtable的區別

共同點:HashMap 和 Hashtable 都實現了 Map 接口,都是鍵值對保存數據的方式

不同點:

區別 1 :

① HashMap 可以存放 null

② Hashtable 不能存放 null

區別 2 :

① HashMap 不是線程安全的類

② Hashtable 是線程安全的類

代碼:


1//HashMap和Hashtable都實現了Map接口,都是鍵值對保存數據的方式
2HashMap<String,String> hashMap = newHashMap<String,String>();
3//HashMap可以用null作key,作value
4hashMap.put(null, "123");
5hashMap.put("123", null);
6Hashtable<String,String> hashtable = newHashtable<String,String>();
7//Hashtable不能用null作key,不能用null作value
8hashtable.put(null, "123");
9hashtable.put("123", null);
  1. set

4.1 set 種類

① HashSet: 無序

② LinkedHashSet: 按照插入順序

③ TreeSet: 從小到大排序

代碼:


 1HashSet<Integer> numberSet1 =newHashSet<Integer>();
 2//HashSet中的數據不是按照插入順序存放
 3numberSet1.add(88);
 4numberSet1.add(8);
 5numberSet1.add(888);
 6System.out.println(numberSet1);
 7LinkedHashSet<Integer> numberSet2 =newLinkedHashSet<Integer>();
 8//LinkedHashSet中的數據是按照插入順序存放
 9numberSet2.add(88);
10numberSet2.add(8);
11numberSet2.add(888);
12System.out.println(numberSet2);
13TreeSet<Integer> numberSet3 =newTreeSet<Integer>();
14//TreeSet 中的數據是進行了排序的
15numberSet3.add(88);
16numberSet3.add(8);
17numberSet3.add(888);
18System.out.println(numberSet3);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章