Collection常用方法集合

/*
方法摘要 
 boolean add(E e) 向集合中添加元素  
 void clear() 清空集合    
 boolean contains(Object o) 如果此 collection 包含指定的元素,则返回 true。 
 boolean containsAll(Collection<?> c) 
          如果此 collection 包含指定 collection 中的所有元素,则返回 true。 
 boolean equals(Object o) 
          比较此 collection 与指定对象是否相等。 
 int hashCode() 
          返回此 collection 的哈希码值。 
 boolean isEmpty() 
          如果此 collection 不包含元素,则返回 true。 
 Iterator<E> iterator() 
          返回在此 collection 的元素上进行迭代的迭代器。 
 boolean remove(Object o) 
          从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。 
 boolean removeAll(Collection<?> c) 
          移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。 
 boolean retainAll(Collection<?> c) 
          仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。 
 int size() 
          返回此 collection 中的元素数。 
 Object[] toArray() 
          返回包含此 collection 中所有元素的数组。 
<T> T[] 
 toArray(T[] a) 
          返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同 


*/
import java.util.*;
public class fuck2{

public static void main(String[] args){
//1.创建集合
Collection c=new ArrayList();//多态,子类指向父类

//2.添加元素
//Collection只能存储引用类型,而且只能单个存储
c.add(1);
c.add(new Object());

//3。常用方法
System.out.println(c.size());//获取元素的个数,2
System.out.println(c.isEmpty());//是否为空集合,false

c.clear();
System.out.println(c.size());//0
System.out.println(c.isEmpty());//true

}

}
### Collection接口常用方法详解 Java 中的 `Collection` 接口是集合框架的顶级接口之一,它定义了所有集合类共有的基本操作。这些方法不仅适用于 `List` 和 `Set` 接口的实现类(如 `ArrayList`、`LinkedList`、`HashSet` 和 `TreeSet`),而且为集合类提供了一致的操作方式。以下是 `Collection` 接口中一些常用方法及其详细说明。 #### 添加元素 - `boolean add(E e)`:将指定的元素添加到集合中。如果集合因为此操作而发生变化,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(); boolean result = collection.add("Hello"); System.out.println(result); // 输出 true ``` - `boolean addAll(Collection<? extends E> c)`:将指定集合中的所有元素添加到当前集合中。如果当前集合因为此操作而发生变化,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(); Collection<String> anotherCollection = Arrays.asList("World", "!"); boolean result = collection.addAll(anotherCollection); System.out.println(result); // 输出 true ``` #### 删除元素 - `boolean remove(Object o)`:从集合中移除一个与指定对象相等的元素(如果存在)。如果集合包含该元素并成功移除,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); boolean result = collection.remove("banana"); System.out.println(result); // 输出 true ``` - `boolean removeAll(Collection<?> c)`:从当前集合中移除所有与指定集合中的元素相等的元素。如果当前集合因为此操作而发生变化,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Collection<String> toRemove = Arrays.asList("banana", "grape"); boolean result = collection.removeAll(toRemove); System.out.println(result); // 输出 true ``` - `boolean retainAll(Collection<?> c)`:仅保留当前集合中那些也包含在指定集合中的元素。换句话说,移除当前集合中不在指定集合中的所有元素。如果当前集合因为此操作而发生变化,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Collection<String> toRetain = Arrays.asList("banana", "cherry", "date"); boolean result = collection.retainAll(toRetain); System.out.println(result); // 输出 true ``` #### 查询操作 - `int size()`:返回集合中的元素数量。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); int size = collection.size(); System.out.println(size); // 输出 3 ``` - `boolean isEmpty()`:判断集合是否为空。如果集合中没有元素,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(); boolean empty = collection.isEmpty(); System.out.println(empty); // 输出 true ``` - `boolean contains(Object o)`:检查集合是否包含指定的元素。如果包含,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); boolean contains = collection.contains("banana"); System.out.println(contains); // 输出 true ``` - `boolean containsAll(Collection<?> c)`:检查当前集合是否包含指定集合中的所有元素。如果当前集合包含指定集合中的所有元素,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Collection<String> toCheck = Arrays.asList("banana", "cherry"); boolean containsAll = collection.containsAll(toCheck); System.out.println(containsAll); // 输出 true ``` #### 其他操作 - `Object[] toArray()`:返回一个包含集合中所有元素的数组。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Object[] array = collection.toArray(); System.out.println(Arrays.toString(array)); // 输出 [apple, banana, cherry] ``` - `<T> T[] toArray(T[] a)`:返回一个包含集合中所有元素的数组,数组的类型与指定数组相同。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); String[] array = collection.toArray(new String[0]); System.out.println(Arrays.toString(array)); // 输出 [apple, banana, cherry] ``` - `Iterator<E> iterator()`:返回一个迭代器,用于遍历集合中的元素。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Iterator<String> iterator = collection.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } // 输出: // apple // banana // cherry ``` #### 集合比较 - `boolean equals(Object o)`:比较当前集合与另一个对象是否相等。两个集合相等当且仅当它们包含相同的元素。 ```java Collection<String> collection1 = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Collection<String> collection2 = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); boolean equals = collection1.equals(collection2); System.out.println(equals); // 输出 true ``` - `int hashCode()`:返回集合的哈希码值。集合的哈希码是其所有元素的哈希码之和。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); int hashCode = collection.hashCode(); System.out.println(hashCode); // 输出某个整数值 ``` 这些方法构成了 `Collection` 接口的核心功能,为集合类提供了统一的操作接口。通过这些方法,可以方便地进行元素的添加、删除、查询和其他操作。[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值