根接口的方法
Collection<String> collection = new ArrayList<>();
/**
* 返回集合中元素的数量
*/
int size = collection.size();
/**
* 判断集合是否为空
*/
boolean isEmpty = collection.isEmpty();
/**
* 判断集合是否包含指定元素
*/
boolean isContain = collection.contains("jack");
/**
* 返回集合的迭代器,用于遍历集合
*/
Iterator<String> iterator = collection.iterator();
/**
* 集合转数组
*/
Object[] array = collection.toArray();
/**
* 集合转数组,并指定类型
* 1、当数组参数长度 < collection的长度,数组会自动扩容,大小和collection的元素个数一致
* 2、当数组参数长度 = collection的长度,直接将元素放入参数数组中
* 3、当数组参数长度 > collection的长度,直接将元素放入参数数组中,多余的空位用null填充
*/
String[] strArray = collection.toArray(new String[0]);
/**
* 新增元素
*/
boolean add = collection.add("amy");
/**
* 移除指定元素
*/
boolean remove = collection.remove("jack");
/**
* 判断collection是否包含参数集合中的所有元素
*/
List<String> list = Arrays.asList("amy");
boolean containsAll = collection.containsAll(list);
/**
* 将指定集合中的元素添加到collection中
*/
boolean addAll = collection.addAll(list);
/**
* 删除collection中包含指定集合中的元素
*/
boolean removeAll = collection.removeAll(list);
/**
* 按照一定规则过滤集合中的元素
*/
Collection<Person> persons = new ArrayList<>();
persons.add(new Person("张三", 18, "F"));
persons.add(new Person("李四", 38, "F"));
persons.add(new Person("王五", 28, "F"));
boolean removeIf = persons.removeIf(person -> person.getAge()>=30);
/**
* 删除collection中包含但指定集合list中不包含的所有元素。
*/
boolean retainAll = collection.retainAll(list);
/**
* 清空集合
*/
collection.clear();
/**
* 比较指定对象与集合是否相等
*/
boolean equals = collection.equals(list);
/**
* 返回集合的hashCode值
*/
int hashCode = collection.hashCode();
/**
* 创建一个分割迭代器
*/
Spliterator<String> spliterator = collection.spliterator();
/**
* 创建stream流
*/
Stream<String> stream = collection.stream();
/**
* 创建一个并行stream流
*/
Stream<String> parallelStream = collection.parallelStream();