『Java』List Set

观前提醒:本文内容多为入门时的学习笔记,笔记内容有些混乱!!!
| 😙 | 😎 | 😟 |
| 😙 | 😨 | 🤣 |
| 🐭 | 😯 | 😟 |

泛型只能是引用类型,不能是基本类型。

如果希望集合中存储的是基本类型数据,需要基本类型对应的“包装类”,如下:

基本类型        包装类(都位于java.lang包中)
byte           Byte
short          Short
int            Integer
long           Long
float          Float
double         Double
char           Character
boolean        Boolean

// 自动装箱:基本类型 --> 包装类型   例如:int --> Integer
// 自动拆箱:包装类型 --> 基本类型   例如:Integer --> int

List

List接口不但继承了Collection接口的全部方法,还增加了一些根据元素【索引】进行操作的特有方法,如下:

  • public void add(int index, E element):将指定元素添加到集合的指定位置
  • public E get(int index):获取指定位置的元素值
  • public E remove(int index):删除指定位置的元素值,返回被移除元素
  • public E set(int index, E element):用指定元素替换指定位置的元素,返回指定位置的修改之前的元素

ArrayList

每个 ArrayList 实例都有一个容量,随着向 ArrayList 中不断添加元素,其容量也自动增长。

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> arr = new ArrayList<>(); // 定义一个空列表
        ArrayList<Character> arr_char = new ArrayList<>(5); // 使用指定的初始容量构造一个空列表
        System.out.println(arr_char.size()); // 0 返回集合中元素的个数,虽然arr_char容量为5,但是还没有元素

        arr.add(0);
        System.out.println(arr); // [0]

        Collection<Integer> coll = new ArrayList<>();
        Collections.addAll(coll, 1,2,3,4);
        arr.addAll(coll);
        System.out.println(arr); // [0, 1, 2, 3, 4]

        arr.add(3, -1); // 在索引3的位置上插入元素10
        System.out.println(arr); // [0, 1, 2, -1, 3, 4]

        arr.addAll(0, coll); // 在0位置上插入coll中的元素
        System.out.println(arr); // [1, 2, 3, 4, 0, 1, 2, -1, 3, 4]

        System.out.println(arr.indexOf(-1)); // 7 返回-1元素的索引值
        System.out.println(arr.get(7)); // -1 获取索引7对应的元素值
        System.out.println(arr.set(7, 1)); // -1 将-1这个元素修改为1,并返回修改之前的元素
        System.out.println(arr); // [1, 2, 3, 4, 0, 1, 2, 1, 3, 4]

        System.out.println(arr.remove(7)); // 1 删除索引7对应的元素,并返回该元素
        System.out.println(arr); // [1, 2, 3, 4, 0, 1, 2, 3, 4]
        System.out.println(arr.subList(3, 6)); // [4, 0, 1] 截取索引在[3, 6)之间的元素,返回值是List<E>

        arr.sort((o1, o2) -> o1 - o2); // 升序排列 无论升序还是降序都需要传入Comparator实现类的实例
        System.out.println(arr); // [0, 1, 1, 2, 2, 3, 3, 4, 4]
        arr.sort((o1, o2) -> o2 - o1); // 降序排列
        System.out.println(arr); // [4, 4, 3, 3, 2, 2, 1, 1, 0]

        System.out.println(arr.contains(0)); // true 集合中包含元素0,返回true
        System.out.println(arr.containsAll(coll)); // true 如果该集合包含指定集合中的所有元素,则返回true
        System.out.println(arr.removeAll(coll)); // true 删除arr中所有包含在coll中的元素
        System.out.println(arr); // [0]

        System.out.println(arr.isEmpty()); // false 判空

        ArrayList arr2 = (ArrayList<Integer>)arr.clone(); // 拷贝,返回值是一个Object实例,可进行向下转型
        System.out.println(arr == arr2); // false
        arr2.clear(); // 将集合中元素清空
        System.out.println(arr); // [0]
        System.out.println(arr2); // []
    }
}

LinkedList

LinkedList集合的特点:

  1. 底层是一个【链表】结构:查询慢,增删快
  2. 里边包含了大量操作首尾元素的方法

下面代码介绍几个LinkedList特有方法,其他List和Collection接口定义的一些共性方法不在介绍:

import java.util.LinkedList;

public class Main {
    public static void main(String[] args) {
        /* LinkedList中特有的一些重用方法:
            - public void addFirst(E e):将指定元素插入此列表的开头。
            - public void addLast(E e):将指定元素添加到此列表的结尾。
            - public void push(E e):将元素推入此列表所表示的堆栈。

            - public E getFirst():返回此列表的第一个元素。
            - public E getLast():返回此列表的最后一个元素。

            - public E removeFirst():移除并返回此列表的第一个元素。
            - public E removeLast():移除并返回此列表的最后一个元素。
            - public E pop():从此列表所表示的堆栈处弹出一个元素。

            - public boolean isEmpty():如果列表不包含元素,则返回true。
         */
        //创建LinkedList集合对象
        LinkedList<String> linked = new LinkedList<>();
        //使用add方法往集合中添加元素
        linked.add("a");
        linked.add("b");
        linked.add("c");
        System.out.println(linked); //[a, b, c]

        // addFirst()等价于push()
        linked.addFirst("www1");
        System.out.println(linked); // [www1, a, b, c]
        linked.push("www2");
        System.out.println(linked); // [www2, www1, a, b, c]

        // 将指定元素添加到此列表的结尾。此方法等效于add()
        linked.addLast("com");
        System.out.println(linked); // [www2, www1, a, b, c, com]

        if(!linked.isEmpty()){ // 判空
            String first = linked.getFirst();  // 获取表头元素
            System.out.println(first); // www2
            String last = linked.getLast(); // 获取表尾元素
            System.out.println(last); // com
            System.out.println(linked); // [www2, www1, a, b, c, com]
        }

        String first = linked.pop(); // 弹出第一个元素,相当于removeFirst()
        System.out.println("被移除的第一个元素:"+first); // 被移除的第一个元素:www2
        String last = linked.removeLast();
        System.out.println("被移除的最后一个元素:"+last); // 被移除的最后一个元素:com
        System.out.println(linked); // [www1, a, b, c]
        linked.removeFirst();
        System.out.println(linked); // [a, b, c]

        linked.add(2, "bc"); // 可以使用索引
        System.out.println(linked); // [a, b, bc, c]
        System.out.println(linked.get(2)); // bc 通过索引获取元素
    }
}

Set

Set集合特点:

  1. 不允许重复的元素
  2. 没有索引,不能使用带索引的方法

HashSet和LinkedHashSet

HashSet是Set接口的一个实现类,LinkedHashSet也是Set接口的一个实现类,同时还是HashSet的子类。

HashSet的常用方法:

public boolean add(E e)
public boolean remove(Object o)
public void clear()
public boolean contains(Object o)
public boolean isEmpty()
public boolean isEmpty()
// 以上方法不再介绍
import java.util.*;

public class Main {
    public static void main(String[] args) {
        /*
            java.util.HashSet 是 Set接口 的一个实现类
            特点:
                1. 无序集合,存储顺序和取出顺序可能不一致
                2. 底层是一个哈希表,查询速度非常快
         */
        Set<Integer> set1 = new HashSet<>();
        set1.add(1); set1.add(3); set1.add(2); set1.add(1);
        System.out.println(set1); // [1, 2, 3]

        // 遍历方式:
        Iterator<Integer> it = set1.iterator(); // 返回该集合中所有元素的迭代器
        while (it.hasNext()){ // hasNext()判断是否还有下一个元素
            System.out.println(it.next()); // next()获取下一个元素
        } // 1 2 3

        // 使用foreach语句遍历集合
        for (int x : set1)
            System.out.println(x); // 1 2 3

        // ===============================================================

        /*
            java.util.LinkedHashSet是HashSet的一个子类
            底层结构是【哈希表+链表】,链表用来记录元素添加顺序,可以实现顺序存储
         */
        Set<Integer> set3 = new LinkedHashSet<>();
        set3.add(1); set3.add(3); set3.add(2); set3.add(1);
        System.out.println(set3); // [1, 3, 2]
    }
}

哈希表图解

Set集合中元素不重复的原理

posted @ 2021-02-21 18:57  世纪小小孟  阅读(136)  评论(0编辑  收藏  举报