Collection
接口的所有方法举例讲解public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] a);
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
void clear();
boolean equals(Object o);
int hashCode();
default Stream<E> stream() {
// Java 8 引入的 Stream API
return null;
}
default Stream<E> parallelStream() {
// Java 8 引入的 Stream API
return null;
}
}
Collection
接口是 Java 集合框架中的一个基本接口,定义了一系列与集合操作相关的方法。以下是 Collection
接口中的方法按不同功能分类展示:
int size()
: 返回集合中的元素数量。boolean isEmpty()
: 判断集合是否为空。boolean contains(Object element)
: 判断集合是否包含指定元素。boolean add(E element)
: 向集合中添加一个元素。boolean remove(Object element)
: 从集合中移除指定元素。void clear()
: 清空集合中的所有元素。Object[] toArray()
: 将集合转换为数组。<T> T[] toArray(T[] array)
: 将集合转换为指定类型的数组。Iterator<E> iterator()
: 返回一个迭代器,用于遍历集合中的元素。forEach(Consumer<? super E> action)
: 对集合中的每个元素执行指定操作。boolean containsAll(Collection<?> c)
: 判断集合是否包含指定集合中的所有元素。boolean addAll(Collection<? extends E> c)
: 将指定集合中的所有元素添加到集合中。boolean removeAll(Collection<?> c)
: 从集合中移除指定集合中的所有元素。boolean retainAll(Collection<?> c)
: 仅保留集合中包含在指定集合中的元素。boolean equals(Object o)
: 判断两个集合是否相等。int hashCode()
: 返回集合的哈希码值。下面是关于 Collection
接口中方法的例子:
基本操作方法:
size()
:
Collection<String> collection = Arrays.asList("apple", "banana", "orange");
int size = collection.size(); // 返回 3
isEmpty()
:
Collection<String> emptyCollection = new ArrayList<>();
boolean isEmpty = emptyCollection.isEmpty(); // 返回 true
contains(Object element)
:
Collection<String> fruits = Arrays.asList("apple", "banana", "orange");
boolean containsApple = fruits.contains("apple"); // 返回 true
add(E element)
:
Collection<String> colors = new ArrayList<>();
colors.add("red");
colors.add("blue");
// 现在 colors 包含 ["red", "blue"]
remove(Object element)
:
Collection<String> colors = new ArrayList<>(Arrays.asList("red", "blue"));
colors.remove("red");
// 现在 colors 包含 ["blue"]
clear()
:
Collection<String> colors = new ArrayList<>(Arrays.asList("red", "blue"));
colors.clear(); // 清空集合,colors 现在为空
集合转换方法:
toArray()
:
Collection<String> fruits = Arrays.asList("apple", "banana", "orange");
Object[] array = fruits.toArray(); // 转换为数组
toArray(T[] array)
:
Collection<String> fruits = Arrays.asList("apple", "banana", "orange");
String[] array = fruits.toArray(new String[0]); // 转换为字符串数组
集合遍历方法:
iterator()
:
Collection<String> colors = Arrays.asList("red", "blue", "green");
Iterator<String> iterator = colors.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
forEach(Consumer<? super E> action)
:
Collection<String> colors = Arrays.asList("red", "blue", "green");
colors.forEach(color -> System.out.println(color));
集合操作方法:
containsAll(Collection<?> c)
:
Collection<String> fruits = Arrays.asList("apple", "banana", "orange");
Collection<String> someFruits = Arrays.asList("apple", "banana");
boolean containsAll = fruits.containsAll(someFruits); // 返回 true
addAll(Collection<? extends E> c)
:
Collection<String> colors = new ArrayList<>(Arrays.asList("red", "blue"));
Collection<String> additionalColors = Arrays.asList("green", "yellow");
colors.addAll(additionalColors);
// 现在 colors 包含 ["red", "blue", "green", "yellow"]
removeAll(Collection<?> c)
:
Collection<String> colors = new ArrayList<>(Arrays.asList("red", "blue", "green", "yellow"));
Collection<String> unwantedColors = Arrays.asList("green", "yellow");
colors.removeAll(unwantedColors);
// 现在 colors 包含 ["red", "blue"]
retainAll(Collection<?> c)
:
Collection<String> colors = new ArrayList<>(Arrays.asList("red", "blue", "green", "yellow"));
Collection<String> wantedColors = Arrays.asList("red", "blue");
colors.retainAll(wantedColors);
// 现在 colors 包含 ["red", "blue"]
比较和哈希方法:
equals(Object o)
:
Collection<String> colors1 = Arrays.asList("red", "blue");
Collection<String> colors2 = Arrays.asList("red", "blue");
boolean areEqual = colors1.equals(colors2); // 返回 true
hashCode()
:
Collection<String> colors = Arrays.asList("red", "blue");
int hashCode = colors.hashCode();
这些例子演示了 Collection
接口中方法的基本用法。请注意,具体的使用方式可能会因集合的具体实现类而不同。