[Java集合類]集合框架以及背後的數據結構

預備知識

什麼是數據結構?

組織大量的數據--增刪改查

什麼是集合類?

Java提供的一套現成的類,實現了常用的數據結構

Java集合框架

(Java Collection Framework):又被稱爲容器container是定義在java.util包下的一組接口interfaces和其實現類 classes 

爲什麼要實現集合框架?

將多個元素 element 置於一個單元中,用於對這些元素進行快速、便捷的存儲 store 、檢索
retrieve 、管理 manipulate
,即平時我們俗稱的增刪查改 CRUD 。

筆試面試題

Java中常用集合類及對應的數據結構

ArrayList:順序表

LinkedList:鏈表

TreeSet/TreeMap:二叉搜索樹

HashSet/HashMap:哈希表

PriorityQueue:堆(二叉樹)

接口interfaces

collection:用來管理和存儲一組對象(Objects),這些對象一般稱爲元素

 Set : 元素不能重複,背後隱含着查找/搜索的語義

SortedSet : 一組有序的不能重複的元素

List : 線性結構

Queue : 隊列

 Deque : 雙端隊列

Map:鍵值對(key-value-Pair),根據key來查找value

1Collection接口:

實例化時需要通過一個具體類(例如ArrayList類)來實現此接口

Collection<String> coolection=new ArrayList<>();

1.1Collection常用方法

boolean add(E e)--將元素e放入集合中

    collection.add("第一個元素");

void clear()--刪除集合中的所有元素

boolean isEmpty()--判斷集合是否有空元素

boolean remove(Object e)--如果元素e出現在集合中,刪除

int size()--返回集合中元素的個數

Object[] toArray--返回一個裝有所有集合元素的數組

 //使用toArray()--返回一個裝有集合所有元素的數組
        Object[] array=collection.toArray();
        System.out.println( "裝有集合所有元素的數組:"+Arrays.toString(array));

contains()--查詢是否包含該元素

//使用contains()方法-----是否包含某元素
        boolean ret=collection.contains("第一個元素");
        System.out.println("集合是否包含該元素?(true包含,false不包含):"+ret);

代碼實現: 

package package1127;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class TestCollection {
    public static void main(String[] args) {
        //泛型語法,集合中保存的是String類型的若干個對象
        //本身是ArrayList<String>類型
        Collection<String> collection=new ArrayList<>();//創建Collection實例,Collection接口實現的子類
        //使用isEmpty()--判斷集合是否爲空
        // 使用size()--返回集合元素的個數
        System.out.println("集合是否爲空?(true爲空,false不爲空):"+collection.isEmpty());
        System.out.println("集合中元素的個數爲:"+collection.size());
        //使用add("被添加的元素")--添加一個元素進入集合中
        collection.add("第一個元素");
        collection.add("第二個元素");
        collection.add("第三個元素");
        System.out.println("集合是否爲空?(true爲空,false不爲空):"+collection.isEmpty());
        System.out.println("集合中元素的個數爲:"+collection.size());
        //使用toArray()--返回一個裝有集合所有元素的數組
        Object[] array=collection.toArray();
        System.out.println( "裝有集合所有元素的數組:"+Arrays.toString(array));
        //foreach遍歷集合類
        System.out.println("foreach遍歷集合類:");
        for(String s:collection){
            System.out.println(s);
        }
        //使用contains()方法-----是否包含某元素
        boolean ret=collection.contains("第一個元素");
        System.out.println("集合是否包含該元素?(true包含,false不包含):"+ret);
        //remove()---刪除方法
        System.out.println("刪除第二個元素:");
        collection.remove("第二個元素");
        for(String s:collection){
            System.out.println(s);
        }
    }
}
//輸出:
/*
集合是否爲空?(true爲空,false不爲空):true
集合中元素的個數爲:0
集合是否爲空?(true爲空,false不爲空):false
集合中元素的個數爲:3
裝有集合所有元素的數組:[第一個元素, 第二個元素, 第三個元素]
foreach遍歷集合類:
第一個元素
第二個元素
第三個元素
集合是否包含該元素?(true包含,false不包含):true
刪除第二個元素:
第一個元素
第三個元素
*/

1.2遍歷集合類

foreach語法可以和很多類搭配,只要這個類實現了iterable接口,就可以使用

//foreach遍歷集合類
        System.out.println("foreach遍歷集合類:");
        for(String s:collection){
            System.out.println(s);
        }

2Map接口

存的是鍵值對

Map根據key可以映射到value但是根據value無法映射到key

2.1常用方法:

boolean isEmpty()

int size()

put(key,value)--插入鍵值對,put方法如果key已經存在,新的value就會覆蓋掉舊的value

  //使用put("key","value")--插入鍵值對
        map.put("小酒窩","林俊杰");

get(key)--返回value(根據key查找value),key不存在就返回null,無默認value

getOrdefault(key,defaultValue)--返回value,如果不存在key時,有默認value值,爲defaultValue

 //使用get("key")--根據key查找value
        System.out.println(map.get("王妃"));
        //key不存在返回defaultValue,key存在返回value
        System.out.println(map.getOrDefault("小星星","北方男人"));
package package1127;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
    public static void main(String[] args) {
        //Map的基本用法
        Map<String,String> map=new HashMap<>();//HashMap來實現Map接口,key和value都是String類型
        //使用isEnmty()和size()
        System.out.println("map是否爲空?(true爲空,false不爲空):"+map.isEmpty());
        System.out.println("map中鍵值對的個數爲:"+map.size()+"個");
        //使用put("key","value")--插入鍵值對
        map.put("小酒窩","林俊杰");
        map.put("王妃","蕭敬騰");
        map.put("告白氣球","周杰倫");
        System.out.println("map是否爲空?(true爲空,false不爲空):"+map.isEmpty());
        System.out.println("map中鍵值對的個數爲:"+map.size()+"個");
        //重點!!
        //使用get("key")--根據key查找value
        System.out.println(map.get("王妃"));
        //key不存在返回defaultValue,key存在返回value
        System.out.println(map.getOrDefault("小星星","北方男人"));
    }
}
//輸出:
/*
map是否爲空?(true爲空,false不爲空):true
map中鍵值對的個數爲:0個
map是否爲空?(true爲空,false不爲空):false
map中鍵值對的個數爲:3個
蕭敬騰
北方男人
*/

2.2遍歷Map:

2.2.1通過map.entrySet()---推薦使用,容量大

        //通過Entry操作獲取到鍵值對
        //使用Entry來進行遍歷每個Entry就是一個條目,也就是一個鍵值對
        System.out.println("通過Map.entrySet遍歷key和value");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key=" + entry.getKey() + " value=" + entry.getValue());
        }

2.2.2Map.keySet()--普遍使用,二次取值

    System.out.println("通過Map.keySet遍歷key和value:");
    for (String key : map.keySet()){
       System.out.println("key= "+ key + "  value= " + map.get(key));
    }

3實現classes

interface 順序表 鏈表 堆 紅黑樹 哈希表
Set       TreeSet HashSet
List   ArrayList LinkedList     
Queue   LinkedList PriorityQueue   
Deque   LinkedList     
Map       TreeMap HashMap

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章