Set 接口的核心 API

一、Set 的核心 API

1. 添加元素

  • boolean add(E e):将指定元素添加到集合中(如果集合中尚未包含该元素)。

  • boolean addAll(Collection<? extends E> c):将指定集合中的所有元素添加到当前集合中。

示例

Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // 重复元素,不会被添加
System.out.println(set); // 输出: [Apple, Banana]

2. 删除元素

  • boolean remove(Object o):从集合中移除指定元素(如果存在)。

  • boolean removeAll(Collection<?> c):移除集合中与指定集合相同的所有元素。

  • boolean retainAll(Collection<?> c):仅保留集合中与指定集合相同的元素。

  • void clear():清空集合中的所有元素。

示例

Set<String> set = new HashSet<>(Arrays.asList("Apple", "Banana", "Cherry"));
set.remove("Banana");
System.out.println(set); // 输出: [Apple, Cherry]

set.removeAll(Arrays.asList("Apple", "Cherry"));
System.out.println(set); // 输出: []

set.addAll(Arrays.asList("Apple", "Banana", "Cherry"));
set.retainAll(Arrays.asList("Apple", "Cherry"));
System.out.println(set); // 输出: [Apple, Cherry]

set.clear();
System.out.println(set); // 输出: []

3. 查询元素

  • boolean contains(Object o):判断集合是否包含指定元素。

  • boolean containsAll(Collection<?> c):判断集合是否包含指定集合中的所有元素。

  • boolean isEmpty():判断集合是否为空。

  • int size():返回集合中的元素数量。

示例

Set<String> set = new HashSet<>(Arrays.asList("Apple", "Banana", "Cherry"));
System.out.println(set.contains("Banana")); // 输出: true
System.out.println(set.containsAll(Arrays.asList("Apple", "Cherry"))); // 输出: true
System.out.println(set.isEmpty()); // 输出: false
System.out.println(set.size()); // 输出: 3

4. 遍历元素

  • Iterator<E> iterator():返回集合的迭代器,用于遍历元素。

  • 增强 for 循环。

示例

Set<String> set = new HashSet<>(Arrays.asList("Apple", "Banana", "Cherry"));

// 使用迭代器遍历
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
    System.out.println(iterator.next());
}

// 使用增强 for 循环遍历
for (String fruit : set) {
    System.out.println(fruit);
}

5. 转换为数组

  • Object[] toArray():将集合转换为数组。

  • <T> T[] toArray(T[] a):将集合转换为指定类型的数组。

示例

Set<String> set = new HashSet<>(Arrays.asList("Apple", "Banana", "Cherry"));

// 转换为 Object 数组
Object[] array = set.toArray();
System.out.println(Arrays.toString(array)); // 输出: [Apple, Banana, Cherry]

// 转换为 String 数组
String[] stringArray = set.toArray(new String[0]);
System.out.println(Arrays.toString(stringArray)); // 输出: [Apple, Banana, Cherry]

6. 其他方法

  • boolean equals(Object o):判断集合是否与指定对象相等。

  • int hashCode():返回集合的哈希码值。

示例

Set<String> set1 = new HashSet<>(Arrays.asList("Apple", "Banana"));
Set<String> set2 = new HashSet<>(Arrays.asList("Banana", "Apple"));
System.out.println(set1.equals(set2)); // 输出: true
System.out.println(set1.hashCode() == set2.hashCode()); // 输出: true

二、Set 的实现类

1. HashSet

  • 特点

    • 基于哈希表实现,元素无序。

    • 允许 null 元素。

    • 插入、删除和查找操作的时间复杂度为 O(1)。

  • 示例

    Set<String> hashSet = new HashSet<>();
    hashSet.add("Apple");
    hashSet.add("Banana");
    System.out.println(hashSet); // 输出: [Apple, Banana]

2. LinkedHashSet

  • 特点

    • 基于哈希表和链表实现,元素按插入顺序排序。

    • 允许 null 元素。

    • 插入、删除和查找操作的时间复杂度为 O(1)。

  • 示例

    Set<String> linkedHashSet = new LinkedHashSet<>();
    linkedHashSet.add("Apple");
    linkedHashSet.add("Banana");
    linkedHashSet.add("Cherry");
    System.out.println(linkedHashSet); // 输出: [Apple, Banana, Cherry]

3. TreeSet

  • 特点

    • 基于红黑树实现,元素按自然顺序或指定比较器排序。

    • 不允许 null 元素。

    • 插入、删除和查找操作的时间复杂度为 O(log n)。

  • 示例

    Set<String> treeSet = new TreeSet<>();
    treeSet.add("Banana");
    treeSet.add("Apple");
    treeSet.add("Cherry");
    System.out.println(treeSet); // 输出: [Apple, Banana, Cherry]

三、Set 的常见问题

1. 如何保证元素不重复?

  • HashSet 和 LinkedHashSet 通过 equals() 和 hashCode() 方法判断元素是否重复。

  • TreeSet 通过 compareTo() 或 Comparator 判断元素是否重复。


2. 如何选择 Set 的实现类?

  • 如果需要快速查找且不关心顺序,使用 HashSet

  • 如果需要按插入顺序排序,使用 LinkedHashSet

  • 如果需要按自然顺序或自定义顺序排序,使用 TreeSet


四、总结

方法说明
add(E e)添加元素(不允许重复)。
remove(Object o)移除指定元素。
contains(Object o)判断是否包含指定元素。
size()返回集合中的元素数量。
iterator()返回集合的迭代器。
toArray()将集合转换为数组。
clear()清空集合中的所有元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值