Skip to content

首先,Collection是整个集合框架的核心接口之一。它提供了很多方法来操作数据,比如添加、删除和遍历元素等。下面是一个简单的例子:

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

public class CollectionExample {
    public static void main(String[] args) {
        // 创建一个ArrayList对象
        ArrayList<String> list = new ArrayList<>();
        
        // 添加元素到列表中
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        
        // 遍历集合中的每个元素
        for (String fruit : list) {
            System.out.println(fruit);
        }
        
        // 检查是否包含特定的元素
        if (list.contains("Banana")) {
            System.out.println("\n列表中包含香蕉。");
        } else {
            System.out.println("\n列表中不包含香蕉。");
        }
        
        // 移除一个元素
        list.remove("Cherry");
        
        // 遍历集合再次确认结果
        System.out.println("\n移除樱桃后的列表:");
        for (String fruit : list) {
            System.out.println(fruit);
        }
    }
}

这个例子展示了如何使用ArrayList来操作一个简单的水果列表。我添加了三个水果,然后遍历它们,并检查是否包含“香蕉”。接着,我还演示了如何从集合中移除一个元素。

接下来,我想了解一下关于不同类型的集合容器的区别和应用场景。比如:

  • List:允许重复元素,按顺序存储。

    • ArrayList:基于动态数组实现,默认容量为10。
    • LinkedList:基于双向链表实现,插入删除效率高但随机访问慢。
  • Set:不允许重复元素。

    • HashSet:无序且无法保证元素的唯一性,内部使用哈希表存储。
    • LinkedHashSet:保持了插入顺序的同时去重。
    • TreeSet:按自然顺序或定制排序存储元素。
  • Map:保存键值对,不允许重复键。

    • HashMap:基于哈希表实现,默认负载因子0.75,提供快速查找和访问。
    • LinkedHashMap:保持插入顺序的同时记录键值对。
    • TreeMap:按自然顺序或定制排序存储键。

为了更直观地理解这些容器的区别,我打算编写一个比较不同集合类型特性的示例代码。比如:

java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;

public class DifferentCollections {
    public static void main(String[] args) {
        // ArrayList 示例
        ArrayList<String> list = new ArrayList<>();
        addElements(list);
        
        System.out.println("\nArrayList中的元素:");
        printCollection(list);

        // HashSet 示例
        HashSet<String> set = new HashSet<>();
        addElements(set);

        System.out.println("\nHashSet中的元素:");
        printCollection(set);

        // LinkedHashSet 示例
        LinkedHashSet<String> linkedSet = new LinkedHashSet<>();
        addElements(linkedSet);

        System.out.println("\nLinkedHashSet中的元素:");
        printCollection(linkedSet);

        // TreeSet 示例
        TreeSet<String> treeSet = new TreeSet<>();
        addElements(treeSet);

        System.out.println("\nTreeSet中的元素:");
        printCollection(treeSet);
    }

    private static void addElements(Collection<String> collection) {
        collection.add("Apple");
        collection.add("Banana");
        collection.add("Cherry");
        collection.add("Apple"); // 测试重复添加
    }

    private static void printCollection(Collection<String> c) {
        for (String s : c) {
            System.out.println(s);
        }
    }
}

这个示例展示了不同集合类型在存储和遍历元素时的不同表现。通过运行这段代码,我可以直观地看到:

  1. ArrayList:允许重复元素,并且按顺序显示添加的元素。
  2. HashSet:不保留插入顺序,并且去重后只保留一个“Apple”。
  3. LinkedHashSet:保持了插入顺序的同时去除了多余的“Apple”。
  4. TreeSet:按照字母顺序排列存储的元素。