4.集合

集合

〇、集合

集合

一、Collection

Collection 是 Java 集合框架的根接口,位于 java.util 包中。它表示一组对象(称为元素),并且定义了对集合进行基本操作的方法

1. Collection接口核心方法

方法名 描述
boolean add(E e) 向集合中添加一个元素。
boolean remove(Object o) 从集合中移除指定的元素。
boolean contains(Object o) 判断集合是否包含指定的元素。
int size() 返回集合中的元素数量。
boolean isEmpty() 判断集合是否为空。
void clear() 清空集合中的所有元素。
Iterator<E> iterator() 返回一个迭代器,用于遍历集合中的元素。

2. 例子

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

/**
 * 该类演示了 Collection 接口的基本操作。
 */
public class CollectionExample {

    public static void main(String[] args) {
        // 1. 创建一个 Collection 实例(使用 ArrayList)
        Collection<String> collection = new ArrayList<>();

        // 2. 添加元素到集合
        collection.add("Apple");
        collection.add("Banana");
        collection.add("Cherry");

        System.out.println("初始的 Collection: " + collection);

        // 3. 检查集合是否为空
        boolean isEmpty = collection.isEmpty();
        System.out.println("集合是否为空: " + isEmpty);

        // 4. 获取集合的大小
        int size = collection.size();
        System.out.println("集合的大小: " + size);

        // 5. 检查集合是否包含某个元素
        boolean containsBanana = collection.contains("Banana");
        System.out.println("集合是否包含 'Banana': " + containsBanana);

        // 6. 删除集合中的元素
        collection.remove("Banana");
        System.out.println("删除 'Banana' 后的 Collection: " + collection);

        // 7. 遍历集合(使用增强型 for 循环)
        System.out.println("使用增强型 for 循环遍历集合:");
        for (String item : collection) {
            System.out.println(item);
        }

        // 8. 使用 Iterator 遍历集合
        System.out.println("使用 Iterator 遍历集合:");
        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            String item = iterator.next();
            System.out.println(item);
        }

        // 9. 清空集合
        collection.clear();
        System.out.println("清空后的 Collection: " + collection);

        // 10. 使用 HashSet 演示 Collection 的另一个实现
        Collection<String> hashSetCollection = new HashSet<>();
        hashSetCollection.add("Orange");
        hashSetCollection.add("Apple");
        hashSetCollection.add("Banana");

        System.out.println("HashSet Collection: " + hashSetCollection);
    }
}

二、List

表示有序集合的接口

1. List特有的核心方法

方法名 描述
void add(int index, E element) 在指定索引位置插入一个元素。
E get(int index) 返回指定索引位置的元素。
E set(int index, E element) 替换指定索引位置的元素,并返回被替换的旧元素。
E remove(int index) 移除指定索引位置的元素,并返回被移除的元素。
int indexOf(Object o) 返回指定元素在列表中首次出现的索引,如果不存在则返回-1
int lastIndexOf(Object o) 返回指定元素在列表中最后一次出现的索引,如果不存在则返回-1
List<E> subList(int fromIndex, int toIndex) 返回从fromIndex(包含)到toIndex(不包含)范围内的子列表。

2. 例子

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

/**
 * 该类演示了 List 接口的基本操作。
 */
public class ListExample {

    public static void main(String[] args) {
        // 1. 创建一个 ArrayList 实例
        List<String> arrayList = new ArrayList<>();

        // 2. 添加元素到 List
        arrayList.add("Apple");
        arrayList.add("Banana");
        arrayList.add("Cherry");

        System.out.println("初始的 ArrayList: " + arrayList);

        // 3. 在指定位置插入元素
        arrayList.add(1, "Blueberry"); // 在索引 1 的位置插入 "Blueberry"
        System.out.println("在索引 1 插入元素后的 ArrayList: " + arrayList);

        // 4. 访问 List 中的元素
        String element = arrayList.get(2); // 获取索引为 2 的元素
        System.out.println("索引 2 处的元素: " + element);

        // 5. 修改 List 中的元素
        arrayList.set(3, "Dragonfruit"); // 将索引 3 的元素替换为 "Dragonfruit"
        System.out.println("修改索引 3 元素后的 ArrayList: " + arrayList);

        // 6. 删除 List 中的元素
        arrayList.remove("Banana"); // 删除值为 "Banana" 的元素
        System.out.println("删除 'Banana' 后的 ArrayList: " + arrayList);

        // 7. 遍历 List(使用增强型 for 循环)
        System.out.println("使用增强型 for 循环遍历 ArrayList:");
        for (String item : arrayList) {
            System.out.println(item);
        }

        // 8. 使用 LinkedList 演示 List 的另一个实现
        List<String> linkedList = new LinkedList<>();
        linkedList.add("Orange");
        linkedList.add("Apple");
        linkedList.add("Banana");

        System.out.println("LinkedList: " + linkedList);

        // 9. 在 LinkedList 中插入和删除元素
        linkedList.add(1, "Mango"); // 在索引 1 的位置插入 "Mango"
        linkedList.remove(2);       // 删除索引为 2 的元素
        System.out.println("修改后的 LinkedList: " + linkedList);
    }
}

三、ArrayList

通过特殊的设计实现了List接口,底层为数组结构

1. ArrayList底层原理

  • 1.利用空参构造创建一个空数组
  • 2.初始添加元素时将数组大小扩容为10
  • 3.后续添加元素时超出了原数组大小,就自动扩容为1.5倍或者(原数组size+后续添加元素个数(如果大于1.5))

2. ArrayList源码

ArrayList源码

在扩容时重新取了一块储存空间,所以相对数组它的大小可以变,不怕周围没空间

四、LinkedList

底层数据结构为双链表,增删快,查询慢

1. LinkedList原理

  • 基于双向链表,每个节点包含数据和前后指针

2. LinkedList源码

LinkedList源码

五、数据结构

  1. 二叉树,每个节点的度<=2
  2. 二叉查找树:任意节点左子树上的值都小于当前节点,右子树树上的节点都大于当前节点
  3. 平衡二叉树:任意节点左右子树高度差不超过1
  4. 红黑树:增删查改性能都很好的数结构

三、Set集合

1. Set接口

方法名称 说明
public boolean add(E e) 把给定的对象添加到当前集合中
public void clear() 清空集合中所有的元素
public boolean remove(E e) 把给定的对象在当前集合中删除
public boolean contains(Object obj) 判断当前集合中是否包含给定的对象
public boolean isEmpty() 判断当前集合是否为空
public int size() 返回集合中元素的个数/集合的长度

2. 3个实现类

set实现类
import java.util.*;

public class SetExample {

    public static void main(String[] args) {
        // 1. 使用 HashSet 演示无序且不允许重复的集合
        System.out.println("==== HashSet 示例 ====");
        Set<String> hashSet = new HashSet<>();
        hashSet.add("Apple");
        hashSet.add("Banana");
        hashSet.add("Orange");
        hashSet.add("Apple"); // 重复元素不会被添加

        System.out.println("HashSet 内容: " + hashSet); // 输出顺序可能不同
        System.out.println("是否包含 'Banana': " + hashSet.contains("Banana")); // true
        System.out.println("HashSet 大小: " + hashSet.size()); // 3

        // 2. 使用 LinkedHashSet 演示按插入顺序存储的集合
        System.out.println("\n==== LinkedHashSet 示例 ====");
        Set<String> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add("One");
        linkedHashSet.add("Two");
        linkedHashSet.add("Three");

        System.out.println("LinkedHashSet 内容: " + linkedHashSet); // 保持插入顺序
        linkedHashSet.remove("Two");
        System.out.println("移除 'Two' 后的内容: " + linkedHashSet);

        // 3. 使用 TreeSet 演示自动排序的集合
        System.out.println("\n==== TreeSet 示例 ====");
        Set<Integer> treeSet = new TreeSet<>();
        treeSet.add(10);
        treeSet.add(5);
        treeSet.add(20);
        treeSet.add(5); // 重复元素不会被添加

        System.out.println("TreeSet 内容: " + treeSet); // 自动按升序排列
        System.out.println("第一个元素: " + ((TreeSet<Integer>) treeSet).first()); // 5
        System.out.println("最后一个元素: " + ((TreeSet<Integer>) treeSet).last()); // 20

        // 4. 数学集合运算:交集、并集、差集
        System.out.println("\n==== 数学集合运算示例 ====");
        Set<Integer> setA = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));
        Set<Integer> setB = new HashSet<>(Arrays.asList(4, 5, 6, 7));

        // 并集
        Set<Integer> union = new HashSet<>(setA);
        union.addAll(setB);
        System.out.println("并集: " + union);

        // 交集
        Set<Integer> intersection = new HashSet<>(setA);
        intersection.retainAll(setB);
        System.out.println("交集: " + intersection);

        // 差集
        Set<Integer> difference = new HashSet<>(setA);
        difference.removeAll(setB);
        System.out.println("差集 (setA - setB): " + difference);
    }
}
posted @ 2025-12-15 15:06  Godjian  阅读(4)  评论(0)    收藏  举报