Java : Collection 示例

Java 中的 Collection (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 Collection<E> 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

集合层次结构中的根接口。集合表示一组对象,称为其元素。有些集合允许重复元素,有些则不允许。有些是有序的,有些则是无序的。 (机器翻译)

Class diagram

final Collection<String> collection = new ArrayList<>();
collection.add("aaa");
collection.add("bbb");
collection.add("ccc");

System.out.println(collection); // [aaa, bbb, ccc]

collection.remove("bbb");

System.out.println(collection); // [aaa, ccc]
final Collection<Integer> collection = Set.of(111, 222, 333);

System.out.println(collection); // [222, 111, 333]
System.out.println(collection.size()); // 3

Methods

boolean add (E e)

确保此集合包含指定元素(可选操作)。 (机器翻译)

final Collection<String> collection = new ArrayList<>();
System.out.println(collection.add("aaa")); // true
System.out.println(collection.add("bbb")); // true
System.out.println(collection.add("bbb")); // true

System.out.println(collection); // [aaa, bbb, bbb]
final Collection<String> collection = new HashSet<>();
System.out.println(collection.add("aaa")); // true
System.out.println(collection.add("bbb")); // true
System.out.println(collection.add("bbb")); // false

System.out.println(collection); // [aaa, bbb]

boolean addAll (Collection<? extends E> c)

将指定集合中的所有元素添加到此集合(可选操作)。 (机器翻译)

final Collection<String> collection = new ArrayList<>();

final var list1 = List.of("aaa", "bbb");
final var list2 = List.of("bbb", "ccc");
final var list3 = List.of("ccc");

System.out.println(collection.addAll(list1)); // true
System.out.println(collection); // [aaa, bbb]

System.out.println(collection.addAll(list2)); // true
System.out.println(collection); // [aaa, bbb, bbb, ccc]

System.out.println(collection.addAll(list3)); // true
System.out.println(collection); // [aaa, bbb, bbb, ccc, ccc]
final Collection<String> collection = new HashSet<>();

final var set1 = Set.of("aaa", "bbb");
final var set2 = Set.of("bbb", "ccc");
final var set3 = Set.of("ccc");

System.out.println(collection.addAll(set1)); // true
System.out.println(collection); // [aaa, bbb]

System.out.println(collection.addAll(set2)); // true
System.out.println(collection); // [aaa, ccc, bbb]

System.out.println(collection.addAll(set3)); // false
System.out.println(collection); // [aaa, ccc, bbb]

void clear ()

从此集合中删除所有元素(可选操作)。 (机器翻译)

final Collection<String> collection = new ArrayList<>();
collection.add("aaa");
collection.add("bbb");
collection.add("ccc");

System.out.println(collection); // [aaa, bbb, ccc]

collection.clear();

System.out.println(collection); // []
System.out.println(collection.isEmpty()); // true

boolean contains (Object o)

如果此集合包含指定元素,则返回 true。 (机器翻译)

final Collection<String> collection = List.of("aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]

System.out.println(collection.contains("aaa")); // true
System.out.println(collection.contains("bbb")); // true
System.out.println(collection.contains("XXX")); // false

boolean containsAll (Collection<?> c)

如果此集合包含指定集合中的所有元素,则返回 true。 (机器翻译)

final Collection<String> collection = List.of("aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]

final var list1 = List.of("aaa", "bbb");
System.out.println(collection.containsAll(list1)); // true

final var list2 = List.of("bbb", "XXX");
System.out.println(collection.containsAll(list2)); // false

final var list3 = List.of("XXX", "YYY");
System.out.println(collection.containsAll(list3)); // false

boolean equals (Object o)

将指定对象与此集合进行比较以确定其是否相等。 (机器翻译)

final Collection<String> collection = List.of("aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]

final var list1 = List.of("aaa", "bbb");
System.out.println(collection.equals(list1)); // false

final var list2 = List.of("aaa", "bbb", "ccc");
System.out.println(collection.equals(list2)); // true

final var list3 = List.of("XXX", "YYY");
System.out.println(collection.equals(list3)); // false
final Collection<String> collection = List.of("aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]

final var set = Set.of("aaa", "bbb", "ccc");
System.out.println(collection.equals(set)); // false

int hashCode ()

返回此集合的哈希码值。 (机器翻译)

final Collection<String> collection1 = List.of("aaa", "bbb", "ccc");
System.out.println(collection1.hashCode()); // 95709313

final Collection<Integer> collection2 = Set.of(111, 222, 333);
System.out.println(collection2.hashCode()); // 666

boolean isEmpty ()

如果此集合不包含任何元素,则返回 true。 (机器翻译)

final Collection<String> collection = new ArrayList<>();

System.out.println(collection); // []
System.out.println(collection.size()); // 0
System.out.println(collection.isEmpty()); // true

collection.add("aaa");
System.out.println(collection); // [aaa]
System.out.println(collection.size()); // 1
System.out.println(collection.isEmpty()); // false

collection.add("bbb");
System.out.println(collection); // [aaa, bbb]
System.out.println(collection.size()); // 2
System.out.println(collection.isEmpty()); // false

Iterator<E> iterator ()

返回此集合中元素的迭代器。 (机器翻译)

final Collection<String> collection = List.of("aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]

final var iterator = collection.iterator();

System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(System.out::println);

// Result
// ↓
//-- forEachRemaining --
//aaa
//bbb
//ccc

default Stream<E> parallelStream ()

返回以此集合作为源的可能并行的流。 (机器翻译)

final Collection<String> collection = List.of("aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]

final var stream = collection.parallelStream();
System.out.println(stream.isParallel()); // true

System.out.println("-- forEach --");
stream.forEach(s -> {
    final var id = Thread.currentThread().threadId();
    System.out.println(s + " : (thread id = " + id + ")");
});

// Result
// ↓
//-- forEach --
//aaa : (thread id = 32)
//ccc : (thread id = 33)
//bbb : (thread id = 1)

boolean remove (Object o)

如果存在,则从此集合中删除指定元素的单个实例(可选操作)。 (机器翻译)

final Collection<String> collection = new ArrayList<>();
collection.add("aaa");
collection.add("bbb");
collection.add("ccc");

System.out.println(collection); // [aaa, bbb, ccc]

System.out.println(collection.remove("aaa")); // true
System.out.println(collection); // [bbb, ccc]

System.out.println(collection.remove("bbb")); // true
System.out.println(collection); // [ccc]

System.out.println(collection.remove("XXX")); // false
System.out.println(collection); // [ccc]

boolean removeAll (Collection<?> c)

删除此集合中所有包含在指定集合中的元素(可选操作)。 (机器翻译)

final var src = List.of("aaa", "bbb", "ccc");

{
    final Collection<String> collection = new ArrayList<>(src);
    System.out.println(collection); // [aaa, bbb, ccc]

    final var list = List.of("aaa", "bbb");
    System.out.println(collection.removeAll(list)); // true
    System.out.println(collection); // [ccc]
}
{
    final Collection<String> collection = new ArrayList<>(src);
    System.out.println(collection); // [aaa, bbb, ccc]

    final var list = List.of("XXX", "YYY");
    System.out.println(collection.removeAll(list)); // false
    System.out.println(collection); // [aaa, bbb, ccc]
}
{
    final Collection<String> collection = new ArrayList<>(src);
    System.out.println(collection); // [aaa, bbb, ccc]

    final var list = List.of("aaa", "bbb", "ccc", "XXX");
    System.out.println(collection.removeAll(list)); // true
    System.out.println(collection); // []
}

default boolean removeIf (Predicate<? super E> filter)

删除此集合中满足给定谓词的所有元素(可选操作)。 (机器翻译)

final Collection<String> collection = new ArrayList<>();
collection.add("aaa");
collection.add("BBB");
collection.add("ccc");
collection.add("DDD");

System.out.println(collection); // [aaa, BBB, ccc, DDD]

final var ret = collection.removeIf(s -> {
    // Checks if the string is uppercase.
    return s.equals(s.toUpperCase());
});

System.out.println(ret); // true
System.out.println(collection); // [aaa, ccc]
final Collection<String> collection = new ArrayList<>();
collection.add("aaa");
collection.add("bbb");

System.out.println(collection); // [aaa, bbb]

final var ret = collection.removeIf(s -> s.equals(s.toUpperCase()));

System.out.println(ret); // false
System.out.println(collection); // [aaa, bbb]

boolean retainAll (Collection<?> c)

仅保留此集合中包含在指定集合中的元素(可选操作)。 (机器翻译)

final var src = List.of("aaa", "bbb", "ccc");

{
    final Collection<String> collection = new ArrayList<>(src);
    System.out.println(collection); // [aaa, bbb, ccc]

    final var list = List.of("aaa", "bbb");
    System.out.println(collection.retainAll(list)); // true
    System.out.println(collection); // [aaa, bbb]
}
{
    final Collection<String> collection = new ArrayList<>(src);
    System.out.println(collection); // [aaa, bbb, ccc]

    final var list = List.of("XXX", "YYY");
    System.out.println(collection.retainAll(list)); // true
    System.out.println(collection); // []
}
{
    final Collection<String> collection = new ArrayList<>(src);
    System.out.println(collection); // [aaa, bbb, ccc]

    final var list = List.of("aaa", "bbb", "ccc", "XXX");
    System.out.println(collection.retainAll(list)); // false
    System.out.println(collection); // [aaa, bbb, ccc]
}

int size ()

返回此集合中的元素数量。 (机器翻译)

final Collection<String> collection = new ArrayList<>();

System.out.println(collection); // []
System.out.println(collection.size()); // 0
System.out.println(collection.isEmpty()); // true

collection.add("aaa");
System.out.println(collection); // [aaa]
System.out.println(collection.size()); // 1
System.out.println(collection.isEmpty()); // false

collection.add("bbb");
System.out.println(collection); // [aaa, bbb]
System.out.println(collection.size()); // 2
System.out.println(collection.isEmpty()); // false

default Spliterator<E> spliterator ()

针对该集合中的元素创建一个 Spliterator。 (机器翻译)

final Collection<String> collection = List.of("aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]

final var spliterator = collection.spliterator();

System.out.println("-- forEachRemaining --");
spliterator.forEachRemaining(System.out::println);

// Result
// ↓
//-- forEachRemaining --
//aaa
//bbb
//ccc

default Stream<E> stream ()

返回以此集合为源的顺序流。 (机器翻译)

final Collection<String> collection = List.of("aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]

final var stream = collection.stream();
System.out.println(stream.isParallel()); // false

System.out.println("-- forEach --");
stream.forEach(System.out::println);

// Result
// ↓
//-- forEach --
//aaa
//bbb
//ccc

Object[] toArray ()

返回包含此集合中所有元素的数组。 (机器翻译)

final Collection<String> collection = List.of("aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]

final Object[] ret = collection.toArray();
System.out.println(Arrays.toString(ret)); // [aaa, bbb, ccc]

default <T> T[] toArray (IntFunction<T[]> generator)

返回包含此集合中所有元素的数组,使用提供的生成器函数分配返回的数组。 (机器翻译)

final Collection<String> collection = List.of("aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]

final String[] ret = collection.toArray(String[]::new);
System.out.println(Arrays.toString(ret)); // [aaa, bbb, ccc]

<T> T[] toArray (T[] a)

返回包含此集合中所有元素的数组;返回数组的运行时类型是指定数组的类型。 (机器翻译)

final Collection<String> collection = List.of("aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]

{
    final String[] ret = collection.toArray(new String[0]);
    System.out.println(Arrays.toString(ret)); // [aaa, bbb, ccc]
}
{
    final String[] array = new String[collection.size()];
    System.out.println(Arrays.toString(array)); // [null, null, null]

    final var ret = collection.toArray(array);
    System.out.println(Arrays.toString(ret)); // [aaa, bbb, ccc]

    System.out.println(Arrays.toString(array)); // [aaa, bbb, ccc]
}

Methods declared in Iterable

forEach

请参阅下面的链接。


相关文章

To top of page