常用方法

Modifier and Type Method and Description
boolean add(E e) 确保此集合包含指定的元素(可选操作)。
boolean addAll(Collection c) 将指定集合中的所有元素添加到此集合(可选操作)。
void clear() 从此集合中删除所有元素(可选操作)。
boolean contains(Object o) 如果此集合包含指定的元素,则返回 true
boolean containsAll(Collection c) 如果此集合包含指定 集合中的所有元素,则返回true。
boolean equals(Object o) 将指定的对象与此集合进行比较以获得相等性。
int hashCode() 返回此集合的哈希码值。
boolean isEmpty() 如果此集合不包含元素,则返回 true
Iterator iterator() 返回此集合中的元素的迭代器。
default Stream parallelStream() 返回可能并行的 Stream与此集合作为其来源。
boolean remove(Object o) 从该集合中删除指定元素的单个实例(如果存在)(可选操作)。
boolean removeAll(Collection c) 删除指定集合中包含的所有此集合的元素(可选操作)。
default boolean removeIf(Predicate filter) 删除满足给定谓词的此集合的所有元素。
boolean retainAll(Collection c) 仅保留此集合中包含在指定集合中的元素(可选操作)。
int size() 返回此集合中的元素数。
default Spliterator spliterator() 创建一个Spliterator在这个集合中的元素。
default Stream stream() 返回以此集合作为源的顺序 Stream
Object[] toArray() 返回一个包含此集合中所有元素的数组。
T[] toArray(T[] a) 返回包含此集合中所有元素的数组; 返回的数组的运行时类型是指定数组的运行时类型。

一般方法的演示

package 集合;

import java.util.ArrayList;
import java.util.Collection; public class CollectionDemo {
public static void main(String[] args) {
Collection coll = new ArrayList();
Collection c = new ArrayList();
//添加元素
coll.add("西瓜");
coll.add("草莓");
coll.add("苹果");
c.addAll(coll);
//输出所有元素
System.out.println("======输出所有元素========");
System.out.println(coll);
System.out.println(c);
System.out.println(c);
//元素个数
System.out.println("========元素个数===========");
System.out.println(coll.size());
System.out.println(c.size());
//包含元素
System.out.println("=======判断是否包含元素=======");
System.out.println(coll.contains("西瓜"));
System.out.println(coll.isEmpty());
System.out.println(coll.containsAll(c));
c.remove("苹果");
System.out.println(coll.equals(c));
System.out.println("======删除元素===========");
coll.removeAll(c);
System.out.println(coll);
c.clear();
System.out.println(c.size());
}
}

对集合内元素迭代的演示

两种方法

  • 增强 for 循环

  • 使用迭代器方法 iterator() 进行操作

    iterator() 以迭代器形式返回集合中的全部元素,此后对迭代器的操作即为对集合的操作;

迭代器中有三个重要函数

boolean hasNext() 如果迭代具有更多元素,则返回 true
E next() 返回迭代中的下一个元素。
default void remove() 从底层集合中删除此迭代器返回的最后一个元素(可选操作)。
package 集合;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; public class CollectionDemo {
public static void main(String[] args) {
Collection coll = new ArrayList();
//添加元素
coll.add("西瓜");
coll.add("草莓");
coll.add("苹果");
System.out.println("=======增强for循环=======");
for (Object object : coll) {
String o = (String)object;
System.out.println(o);
}
System.out.println("=========使用迭代器=======");
Iterator its = coll.iterator();
while(its.hasNext()) {
String o = (String) its.next();
System.out.println(o);
// coll.remove("苹果"); //在使用迭代器时,不能对集合进行修改
its.remove();
}
System.out.println("=======使用迭代器删除集合内元素后=======");
System.out.println(coll.size());
}
}

作为对象的集合元素

package 集合;

import java.util.ArrayList;
import java.util.Collection; public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public static void main(String[] args) {
Collection coll = new ArrayList();
Student s1 = new Student("张三", 22);
Student s2 = new Student("李四", 21);
Student s3 = new Student("王五", 19);
//添加元素
coll.add(s1);
coll.add(s2);
coll.add(s3);
System.out.println(coll);
//元素迭代
System.out.println("增强for循环");
for (Object object : coll) {
Student s = (Student) object;
System.out.println(s);
}
//删除元素
System.out.println(coll.size());
coll.remove(s2);
System.out.println(coll.size());
coll.remove(new Student("张三", 22));
System.out.println("new一个相同元素删除后:" + coll.size()); //这样无法删除集合内的元素
}
}

Collection的使用的


扫描二维码,在手机上阅读!