1. 程式人生 > 實用技巧 >Java集合List、Set以及Map詳解

Java集合List、Set以及Map詳解

一、集合

1.1、概念:是物件的容器,實現了對於物件常用的操作,可實現陣列的功能

1.2、集合與陣列的區別

  • 陣列長度固定,集合長度不固定
  • 陣列可以儲存基本型別和引用型別,而集合只能儲存引用型別

1.3、概述

  • List、Set、Map都是介面,其中List和Set繼承Collection介面,Map為獨立介面
  • List的實現類有ArrayList(重要)、LinkedList(重要)、Vector
  • Set的實現類有HashSet、TreeSet(實現了Set的子介面SortedSet)
  • Map的實現類有HashMap、TreeMap(實現了Map的子介面SortedMap)
  • Collection介面下還有Queue介面,其實現類包含LinkedList、PoriorityQueue

注:LinkedList既實現了List介面,也實現了Queue介面。不過Queue介面窄化了LinkedList的方法,即只能使用Queue介面中定義的方法,不可以使用其他方法

二、Collection集合

1、List介面

  1.1、特點:有序、有下標、元素可以重複

  1.2、常用操作方法:add(E e)、remove(int index)、get(int index)、iterator()

  1.3、實現類特點:

資料結構 查詢速度 增刪速度 執行效率 執行緒安全
ArrayList 陣列 快   
Vector 陣列 快   
LinkedList 雙向連結串列

  1.4、ArrayList與LinkedList區別

    ArrayList是陣列結構,需要開闢連續空間,查詢比較快,增刪比較慢;

    LinkedList是雙向連結串列結構,所以無需開闢連續空間,查詢比較慢,增刪比較快

2、Set集合

  2.1、特點:無序、無下標、元素不可以重複,

  2.2、常用操作方法:add(E e)、remove(Object o)、iterator()

  2.3、實現類特點:

資料結構 是否排序 元素可否為null 元素可否重複 執行緒安全 插入速度
HashSet 雜湊表
TreeSet 紅黑樹

  3.4、HashSet儲存方式:

     HashSet的儲存結構是雜湊表,當存入元素時,首先基於HashCode計算元素存放位置,如果此位置為空,則直接儲存,如果不為空則會呼叫equals進行確認,如果結構為true,則認為重複,否則,形成連結串列。

  3.5、TreeSet的排序方法:

    1、對於系統定義的引用型別(如Integer、Double、String等)

    

public class Demo0 {
    public static void main(String[] args) {
        //建立集合
        TreeSet<Integer> treeset = new TreeSet<>();

        //新增元素
        treeset.add(4);
        treeset.add(5);
        treeset.add(51);
        treeset.add(15);
        treeset.add(26);

        for(Integer i:treeset)
            System.out.println(i);
        
    }
}

  可以看見,遍歷的結果是有序的。

  

  2、**對於儲存的元素為自己定義的類

public class Demo04 {
    public static void main(String[] args) {
        //建立集合
        TreeSet<Person> treeset = new TreeSet<>();

        //生成物件
        Person p1 = new Person("Bce",10);
        Person p2 = new Person("Ace",12);
        Person p3 = new Person("Fce",8);
        Person p4 = new Person("Cce",6);

        //新增元素
        treeset.add(p1);
        treeset.add(p2);
        treeset.add(p3);
        treeset.add(p4);

        System.out.println(treeset.toString());

    }
}

  我們發現此時系統會丟擲異常,大致意思是我們的Person類不可以進行比較,因為排序需要元素之間能比較大小才能進行,我們有兩種解決方法

  (1)元素物件實現Comparable介面,重寫其compareTo方法

public class Person implements Comparable<Person> {
    public String name;
    public int age;
    public Person(){};

    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }


    //這裡重寫Comparable
    public int compareTo(Person p){
        //這裡我們簡單的進行名字的比較
        int n1 = this.name.compareTo(p.name);
        return n1;
    }

    @Override
    public String toString(){
        return this.name+":"+(Integer)this.age;
    }


}

  可以看到執行結果按照我們定義的比較方法進行排序

  

  (2)為TreeSet指定比較器進行排序

public class Demo04 {
    public static void main(String[] args) {
        //建立集合
        TreeSet<Person> treeset = new TreeSet<>(new Comparator<Person>(){

            @Override
            public int compare(Person o1, Person o2) {
                int n = o1.name.compareTo(o2.name);
                return n;
            }
        });

        //生成物件
        Person p1 = new Person("Bce",10);
        Person p2 = new Person("Ace",12);
        Person p3 = new Person("Fce",8);
        Person p4 = new Person("Cce",6);

        //新增元素
        treeset.add(p1);
        treeset.add(p2);
        treeset.add(p3);
        treeset.add(p4);

        System.out.println("執行結果:");
        for(Person p:treeset)
            System.out.println(p.toString());

    }
}

三、Map集合

1、特點:

  • 用於儲存任意鍵值對(key-value)
  • 鍵:無序、無小標、不允許重複(唯一)
  • 值:無序、無下標、允許重複

2、常用方法:put(K key, V value)、get(Object key)、keySet()、remove(K key)、entrySet()等

3、實現類的特點:

資料結構 是否排序 執行效率 Key可否為null 執行緒安全
HashMap 雜湊表+紅黑樹
TreeMap 紅黑樹 是(對key排序)
HashTable 雜湊表 否  

4、關於Map的遍歷

package com.collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Demo06 {
    public static void main(String[] args) {
        //建立集合
        HashMap<Person,String> person = new HashMap<>();

        //1.新增元素
        Person p1 = new Person("xxx",1);
        Person p2 = new Person("yyy",2);
        Person p3 = new Person("zzz",3);
        person.put(p1,"北京");
        person.put(p2,"上海");
        person.put(p3,"杭州");
        //System.out.println(person);


        //2.遍歷
        //2.1keySet方法
        Set<Person> pkey = person.keySet();
        for(Person p:pkey)
            System.out.println(person.get(p));

        System.out.println("分割線--------------------");

        //2.2entrySet方法
        Set<Map.Entry<Person,String>> entry = person.entrySet();
        for(Map.Entry<Person,String> mmp:entry){
            System.out.println(mmp.getKey()+"+"+mmp.getValue());
        }


    }
}