java學習day15——List、Set、Collections、Map

list集合

list常用方法

import java.util.ArrayList;
import java.util.List;

/*
java.lang.List接口 extends Collection接口
List接口的特點:
    1. 有序的集合:存儲元素和取出元素的順序是一致的
    2. 有索引,包含了一些帶索引的方法
    3. 允許存儲重複的元素
注意:
    操作索引時一定要防止索引越界異常
 */
public class Demo01List {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("a");
        System.out.println(list);

        //public void add(int index,E element):將指定的元素,添加到該集合中的指定位置上
        list.add(3,"新加的元素");
        System.out.println(list);//[a, b, c, 新加的元素, a]

        //public E remove(int index):移除列表中指定位置的元素,返回的是被移除的元素
        String removeE = list.remove(2);
        System.out.println(removeE);//c
        System.out.println(list);//[a, b, 新加的元素, a]

        //public E set(int index,E element):用指定元素替換集合中指定位置的元素,返回的是更新前的元素
        String serE = list.set(3,"A");
        System.out.println(serE);//a
        System.out.println(list);//[a, b, 新加的元素, A]

        //public E get(int index):返回集合中指定位置的元素
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        // 或者使用迭代器或增強for
    }
}

ArrayList集合

List 接口的大小可變數組的實現。
注意,此實現不是同步的。(多線程,快!)
ArrayList增刪慢,查詢快

LinkedList集合

List 接口的鏈接列表實現。
LinkedList查詢慢,增刪快
裏面包含了大量操作首尾元素的方法
注意:使用LinkedList集合特有的方法,不能使用多態
方法

public class Demo02LinkedList {
    public static void main(String[] args) {
        LinkedList<String> linked = new LinkedList<>();
        linked.add("a");
        linked.add("b");
        linked.add("c");

//        linked.addFirst("www");
//        System.out.println(linked);//[www, a, b, c]
        linked.push("www");
        System.out.println(linked);//[www, a, b, c]

        linked.addLast("com");
        System.out.println(linked);//[www, a, b, c, com]  與add效果一樣

        System.out.println(linked.removeFirst());//www
        System.out.println(linked);//[a, b, c, com]

        System.out.println(linked.removeLast());//com
        System.out.println(linked);//[a, b, c]
        
        linked.pop();//相當於linked.removeFirst()

        linked.clear();
        if(!linked.isEmpty()){
            System.out.println(linked.getFirst());
            System.out.println(linked.getLast());
        }
    }
}

Vector集合

Vector 類可以實現可增長的對象數組。
與新 collection 實現不同,Vector 是同步的。 (被ArrayList取代)

Set接口

一個不包含重複元素的 collection。
沒有索引,沒有帶索引的方法,也不能使用普通的for循環遍歷

HashSet

HashSet常用方法

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/*
java.util.Set接口 extends Collection接口
Set接口的特點:
    1. 不允許存儲重複元素
    2. 沒有索引,也不能使用普通的for循環遍歷
java.util.HashSet集合 implements Set接口
HashSet特點:
    1. 不允許存儲重複的元素
    2. 沒有索引,也不能使用普通的for循環遍歷
    3. 是一個無序的集合,存儲元素和取出元素的順序可能不一樣
    4. 底層是一個哈希表結構(查詢的速度非常的快)
 */
public class Demo01Set {
    public static void main(String[] args) {
        Set<Integer> set = new HashSet<>();
        set.add(1);
        set.add(3);
        set.add(2);
        set.add(1);
        //遍歷,使用迭代器
        Iterator<Integer> it = set.iterator();
        while (it.hasNext()){
            Integer n = it.next();
            System.out.println(n);//1,2,3  不允許存儲重複元素
        }
        // 使用增強for遍歷
        for(Integer n : set){
            System.out.println(n);
        }
    }
}

哈希值

/*
哈希值:是一個十進制的整數,由系統隨機給出(就是對象的地址值,是一個邏輯地址,是模擬出來得到的地址,不是數據實際存儲的地址)
在Object類有一個方法,可以獲取對象的哈希值
int hashCode() 返回該對象的哈希碼值。
hashCode方法的源碼
public native int hashCode(); native:代表該方法調用的是本地操作系統的方法
 */
public class Demo02HashCode {
    public static void main(String[] args) {
        // Person類繼承了Object類,所以可以使用Object類的hashCode方法
        Person p1 = new Person();
        int h1 = p1.hashCode();
        System.out.println(h1);//284720968

        Person p2 = new Person();
        int h2 = p2.hashCode();
        System.out.println(h2);//189568618
        /*
        toString方法的源碼
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
         */
        System.out.println(p1);//demo02.Person@10f87f48
        System.out.println(p2);//demo02.Person@b4c966a

        /*
        String類的哈希值
        String類重寫了Object類的hashcode方法
         */
        String s1 = new String("abc");
        String s2 = new String("abc");
        System.out.println(s1.hashCode());//96354
        System.out.println(s2.hashCode());//96354

        System.out.println("重地".hashCode());//1179395
        System.out.println("通話".hashCode());//1179395  巧合
    }
}

HashSet集合存儲數據的結構

在這裏插入圖片描述

HashSet集合存儲元素不重複的原理

在這裏插入圖片描述

HashSet存儲自定義類型元素

給HashSet中存放自定義類型元素時,需要重寫對象中的hashCode和equals方法,建立自己的比較方式,才能保證HashSet集合中的對象唯一。
Person類

public class Person extends Object {
    private String name;
    private int age;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    public Person() {
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

main方法

import java.util.HashSet;

/*
set集合保證元素唯一  
存儲的元素(String,Integer,...,Student,...),必須重寫			hashcode方法和equals方法
要求:
    年齡和姓名一樣視爲同一個人
 */
public class Demo03HashSetSavePerson {
    public static void main(String[] args) {
        HashSet<Person> set = new HashSet<>();
        Person p1 = new Person("易烊千璽",19);
        Person p2 = new Person("易烊千璽",19);
        Person p3 = new Person("王俊凱",20);
        System.out.println(p1==p2);//false
        System.out.println(p1.equals(p2));//true
        set.add(p1);
        set.add(p2);
        set.add(p3);
        System.out.println(set);//[demo02.Person@d2f51d66, demo02.Person@35bb51a5]
    }
}

LinkedHashSet

具有可預知迭代順序的 Set 接口的哈希表和鏈接列表實現。

/*
java.util.LinkedHashSet集合 extends HashSet集合
LinkedHashSet集合特點:
底層是一個哈希表(數組+鏈表/紅黑樹)+鏈表,多了一條鏈表(記錄元素的存儲順序),保證元素有序
 */
public class Demo04LinkedHashSet {
    public static void main(String[] args) {
        HashSet<String> set = new HashSet<>();
        set.add("www");
        set.add("abc");
        set.add("abc");
        set.add("com");
        System.out.println(set);//[com, abc, www] 無序,不允許重複

        LinkedHashSet<String> linked = new LinkedHashSet<>();
        linked.add("www");
        linked.add("abc");
        linked.add("abc");
        linked.add("com");
        System.out.println(linked);//[www, abc, com] 有序,不允許重複
    }
}

可變參數

/*
可變參數:時JDK1.5之後出現的新特性
使用前提:
    當方法的參數列表數據類型已經確定,但參數的個數不確定,就可以使用可變參數
使用格式:定義方法時使用
    修飾符 返回值類型 方法名(數據類型...變量名){}
可變參數的原理:
    可變參數底層就是一個數組,根據傳遞參數不同,會創建不同長度的數組,來存儲這些參數
    傳遞的參數個數,可以是0個(不傳遞)、1、2、...多個
可變參數的注意事項:
    1. 一個方法的參數列表,只能有一個可變參數
    2. 如果方法的參數有多個,那麼可變參數必須寫在參數列表的末尾
 */
public class VarArgs {
    public static void main(String[] args) {
        int i = add("abc",1,10,12);
        System.out.println("和爲" + i);//22
    }
/*
可變參數的終極寫法
public static void method(Object...obj){}
 */
    // 計算多個整數的和i
    // add();就會創建一個長度爲0的數組,new int[0]
    public static int add(String b,int c,int... arr) {
        System.out.println(arr);//[I@723279cf底層是一個數組
        System.out.println(arr.length);//2
        int sum = 0;
        for (Integer i : arr) {
            sum += i;
        }
        return sum;
    }
}

Collections集合工具類

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/*
java.util.Collections是集合工具類,用來對集合進行操作,部分方法如下:
public static <T> boolean addAll(Collection<T> c,T... elements):往集合中添加一些元素
public static void shuffle(list<?> list) 打亂順序
public static <T> void sort(list<T> list) 將集合中的元素按照默認順序排序
注意:
    sort(list<T> list)使用前提:
    被排序的集合裏面存儲的元素,必須實現Comparable,重寫接口中的方法CompareTo定義排序規則
Comparable接口的排序規則:
    自己(this)-參數:升序
    參數-自己(this):降序
public static <T> void sort(list<T> list,Comparator<? super T>) 將集合中的元素按照指定順序排序
Comparator和Comparable的區別
Comparable:自己(this)和別人(參數)比較,自己需要實現Comparable接口,重寫比較的規則conpareTo方法
Comparator:相當於找一個第三方的裁判,來比較兩個
Comparable排序規則:
o1-o2:升序
o2-o1:降序
 */
public class Demo01Collections {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"a","b","c","d");
        System.out.println(list);//[a, b, c, d]
        Collections.shuffle(list);
        System.out.println(list);//順序變了,每次都不一樣

        ArrayList<Integer> list2 = new ArrayList<>();
        list2.add(1);
        list2.add(3);
        list2.add(2);
        Collections.sort(list2);//默認升序
        System.out.println(list2);//[1, 2, 3]

        ArrayList<Person> list3 = new ArrayList<>();
        list3.add(new Person("張三",18));
        list3.add(new Person("李四",20));
        list3.add(new Person("王五",15));
        list3.add(new Person("王五",18));
        System.out.println(list3);//[Person{name='張三', age=18}, Person{name='李四', age=20}, Person{name='王五', age=15}]
        Collections.sort(list3);
        System.out.println(list3);//[Person{name='王五', age=15}, Person{name='張三', age=18}, Person{name='李四', age=20}]

        Collections.sort(list2, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;//降序
            }
        });
        System.out.println(list2);//[3, 2, 1]

        Collections.sort(list3, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                int result = o1.getAge()-o2.getAge();// 按照年齡升序排序
                // 如果兩人年齡相同,再用姓名首字母比較
                if(result == 0){
                    result = o1.getName().charAt(0)-o2.getName().charAt(0);
                }
                return result;
            }
        });
        System.out.println(list3);//[Person{name='王五', age=15}, Person{name='張三', age=18}, Person{name='王五', age=18}, Person{name='李四', age=20}]
    }
}

Map集合

雙列集合
Collection 與 Map 的對比
在這裏插入圖片描述

Map常用子類

/*
java.util.Map<K,V>集合
Map集合的特點:
    1. Map集合是一個雙列集合,一個元素包含兩個值(一個key,一個value)
    2. Map集合中的元素,key和value的數據類型可以相同,也可以不同
    3. Map集合中的元素,key是不允許重複的,value是可以重複的
    4. Map集合中的元素,key和value是一一對應
java.util.HashMap<K,V>集合 implements Map<K,V>接口
HashMap集合的特點:
    1.底層是哈希表,查詢速度特別的快
    2.是一個無需的集合,存儲元素和取出元素的順序可能不一致
java.util.LinkedHashMap<K,V>集合 extends HashMap<K,V>集合
LinkedHashMap的特點
    1. LinkedHashMap底層是哈希表+鏈表
    2. LinkedHashMap集合是一個有序的集合,存儲元素和取出元素的順序是一致的

常用方法:
public V put(K key, V value) 將指定的值與此映射中的指定鍵關聯(可選操作)。
public V get(Object key) 返回指定鍵所映射的值;如果此映射不包含該鍵的映射關係,則返回 null。
 */
public class Demo01Map {
    public static void main(String[] args) {
        show01();
        show02();
    }


    /*
    public V remove(Object key) 如果存在一個鍵的映射關係,則將其從此映射中移除(可選操作)。
    返回值:V
        key存在,v返回被刪除的值
        key不存在,v返回null
    public V get(Object key) 返回指定鍵所映射的值;如果此映射不包含該鍵的映射關係,則返回 null。
    public boolean containsKey(Object key) 如果此映射包含指定鍵的映射關係,則返回 true。
     */
    private static void show02() {
        Map<String,Integer> map = new HashMap<>();
        map.put("趙麗穎",168);
        map.put("楊穎",165);
        map.put("林志玲",178);
        System.out.println(map);//{林志玲=178, 趙麗穎=168, 楊穎=165}
        System.out.println(map.remove("林志玲"));//178
        System.out.println(map.remove("林志玲"));//null

        System.out.println(map.get("楊穎"));//165
        System.out.println(map.get("林志玲"));//null

        System.out.println(map.containsKey("楊穎"));//true
        System.out.println(map.containsKey("林志玲"));//false
    }

    /*
    public V put(K key, V value) 將指定的值與此映射中的指定鍵關聯(可選操作)。
    返回值V:
    存儲鍵值對的時候,key不重複,返回值V是null
    存儲鍵值對的時候,key重複,會使用新的value替換map中重複的value,返回被替換的value值
     */
    private static void show01() {
        Map<String,String> map = new HashMap<>();
        String v1 = map.put("鹿晗","關曉彤1");
        System.out.println("v1:"+v1);//v1:null

        String v2 = map.put("鹿晗","關曉彤2");//新值替換舊值
        System.out.println("v2:"+v2);//v2:關曉彤1

        System.out.println(map);//{鹿晗=關曉彤2}

        map.put("小強","小紅");
        map.put("小劉","曉燕");
        map.put("小強","小麗");
        System.out.println(map);//{小強=小麗, 小劉=曉燕, 鹿晗=關曉彤2}
    }
}

Map集合的遍歷

兩種方法
在這裏插入圖片描述

/*
Map集合的第一種遍歷方式:通過鍵找值的方式
Map集合種的方法:
    Set<K> keySet() 返回此映射中包含的鍵的 Set 視圖。
實現步驟:
    1. 使用Map集合種的方法keySet(),把Map集合所有的key取出來,存儲到一個Set集合中
    2. 遍歷Set集合,獲取Map集合中的每一個key
    3. 通過Map集合中的方法get(key),通過key找到value
Map集合的第二種遍歷方式:使用Entry進行遍歷
 */
public class Demo02KeySet {
    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        map.put("趙麗穎",168);
        map.put("楊穎",165);
        map.put("林志玲",178);

        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        while(it.hasNext()){
            String key = it.next();
            Integer value = map.get(key);
            System.out.println(key+"="+value);
        }

        Set<Map.Entry<String,Integer>> set2 = map.entrySet();
        Iterator<Map.Entry<String,Integer>> it2 = set2.iterator();
        while(it2.hasNext()){
            Map.Entry<String,Integer> entry = it2.next();
            String key2 = entry.getKey();
            Integer value2 = map.get(key2);
            System.out.println(key2+"="+value2);
        }
    }
}

例題:計算一個字符串中每一個字符出現的次數

import java.util.HashMap;
import java.util.Scanner;

public class Demo01MapTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        HashMap<Character,Integer> map = new HashMap<>();
        for(char c:str.toCharArray()){
            if(map.containsKey(c)){
                Integer value = map.get(c);
                value++;
                map.put(c,value);
            }else{
                map.put(c,1);
            }
        }
        for(Character key:map.keySet()){
            Integer value = map.get(key);
            System.out.println(key+"="+value);
        }
    }
}

Debug

在這裏插入圖片描述

鬥地主案例(有序版本,雙列)

案例介紹

在這裏插入圖片描述

需求分析

在這裏插入圖片描述
這裏面map順序寫錯了

實現

public class DouDiZhu {
    public static void main(String[] args) {
        // 準備一個map集合,存儲牌的索引和組裝好的牌
        HashMap<Integer, String> poker = new HashMap<Integer, String>();
        // 創建一個index集合,存儲牌的索引
        ArrayList<Integer> index = new ArrayList<>();
        // 定義兩個集合,存儲花色和牌的序號
        List<String> colors = List.of("♠", "♥", "♣", "♦");
        List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        // 將大王小王存入牌的集合中
        poker.put(0, "大王");
        poker.put(1, "小王");
        index.add(0);
        index.add(1);

        int n = 2;
        for (int i = 0; i < numbers.size(); i++) {
            for (int i1 = 0; i1 < colors.size(); i1++) {
                poker.put(n, colors.get(i1) + numbers.get(i));
                index.add(n);
                n++;
            }
        }
        // 洗牌
        Collections.shuffle(index);
        // 定義四個集合,存儲玩家牌的索引和底牌的索引
        ArrayList<Integer> player01 = new ArrayList<>();
        ArrayList<Integer> player02 = new ArrayList<>();
        ArrayList<Integer> player03 = new ArrayList<>();
        ArrayList<Integer> dipai = new ArrayList<>();
        // 發牌
        for (int i = 0; i < index.size(); i++) {
            Integer in = index.get(i);
            if (i >= 51) {
                dipai.add(in);
            } else if (i % 3 == 0) {
                player01.add(in);
            } else if (i % 3 == 1) {
                player02.add(in);
            } else if (i % 3 == 2) {
                player03.add(in);
            }
        }
        // 排序,sort默認升序排序
        Collections.sort(player01);
        Collections.sort(player02);
        Collections.sort(player03);
        Collections.sort(dipai);

        // 看牌
        lookPoker("劉德華",poker,player01);
        lookPoker("周潤發",poker,player02);
        lookPoker("周星馳",poker,player03);
        lookPoker("底牌",poker,dipai);
    }
    /*
        定義一個看牌方法,提高代碼的複用性
        參數:
        String name :玩家名稱
        HashMap<Integer, String> poker
        ArrayList<Integer> list:存儲玩家和底牌的list集合
        查表法:
        遍歷玩家或底牌集合,獲取牌的索引,使用牌的索引,去Map集合中,找到對應的牌
         */
    public static void lookPoker(String name,HashMap<Integer, String> poker,ArrayList<Integer> list){
        // 輸出玩家名稱,不換行
        System.out.print(name+":");
        // 遍歷玩家或底牌集合,獲取牌的索引
        for (Integer key : list) {
            String value = poker.get(key);
            System.out.print(value+" ");
        }
        System.out.println();//打印完每個玩家的牌,換行
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章