开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第31天,点击查看活动详情
- List系列集合:添加的元素是有序的、可重复、有索引的
- Set系列集合:添加的元素是无序的、不重复、无索引的
Collection
Collection是单列集合的祖宗接口,它的功能是全部单列集合都可以继承使用的
方法名称 | 说明 |
---|---|
public boolean add(E e) | 把给定的对象添加到当前集合中 |
public void clear() | 清空集合中所有的元素 |
public boolean remove(E e) | 把给定的对象在当前集合中删除 |
public boolean contains(Object obj) | 判断当前集合中是否包含给定的对象 |
public boolean isEmpty() | 判断当前集合是否为空 |
public int size() | 返回集合中元素的个数/集合的长度 |
注意:
- Collection是一个接口,我们不能直接创建他的对象
- 所以我们只能创建他的实现类
1.初始化
Collection<String> collection = new ArrayList<>();
2.添加元素
collection.add("XYG");
System.out.println(collection);
结果:
注意:
- 如果我们要往List系列集合中添加数据,那么方法永远返回true,因为List系列是允许元素重复的
- 如果我们要往Set系列集合中添加数据
- 如果当前要添加的元素不存在,方法返回true,表示添加成功
- 如果当前要添加的元素已存在,方法返回false,表示添加失败
3.清空元素
collection.clear();
System.out.println(collection);
结果:
4.删除元素
collection.remove("XYG");
System.out.println(collection);
注意:因为Collection里面定义的是共性的方法,所以此时不能通过索引进行删除,只能通过元素的对象进行删除
5.判断元素是否存在
boolean result = collection.contains("XYG");
System.out.println(result);
注意:底层是依赖equals方法进行判断是否存在的
创建学生类
package CollectionText;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
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;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
学生类测试
package CollectionText;
import java.util.ArrayList;
import java.util.Collection;
public class CollectionDome02 {
public static void main(String[] args) {
//1.创建集合对象
Collection<Student> collection = new ArrayList<>();
//2.创建三个学生对象
Student student1 = new Student("aaa", 18);
Student student2 = new Student("bbb", 19);
Student student3 = new Student("ccc", 20);
//3.把学生对象加入到集合中
collection.add(student1);
collection.add(student2);
collection.add(student3);
//4.判断集合中某一个学生对象是否包含
Student student4 = new Student("aaa", 18);
//如果同名同姓就认为是同一个学生
System.out.println(collection.contains(student4));
}
}
注意:
- 底层是依赖equals方法进行判断是否存在的
- 如果集合中存储的是自定义对象,也想通过contains方法来判断是否包含,那么在javabean类中,一定要重写equals方法
Collection遍历
迭代器遍历
迭代器在Java中的类是Iterator,迭代器是集合专用的遍历方式
Collection集合获取迭代器
方法名称 | 说明 |
---|---|
Iterator<E> iterator() | 返回迭代器对象,默认指向当前集合的0索引 |
Iterator中的常用方法
方法名称 | 说明 |
---|---|
boolean hasNext() | 判断当前位置是否有元素,有元素返回true,没元素返回false |
E next() | 获取的当前位置的元素,并将迭代器对象移向下一个位置 |
1.创建集合并添加元素
Collection<String> collection = new ArrayList<>();
collection.add("aaa");
collection.add("bbb");
collection.add("ccc");
collection.add("ddd");
2.获取迭代器对象
- 迭代器好比是一个箭头,默认指向集合0索引处
- 利用循环不算的去获取集合中的每一个元素
Iterator<String> it = collection.iterator();
while(it.hasNext()) System.out.println(it.next());
注意:
- 迭代器遍历完毕,指针不会复位
- 循环中只能用一次next方法
- 迭代器遍历时,不能用集合的方法进行增加或删除
增强for遍历
- 增强for的底层就是迭代器,为了简化迭代器的代码书写的
- 它是JDK5之后出现的,其内部原理就是一个Iterator迭代器
- 所有的单列集合和数组才能用增强for进行遍历
for (String s : collection) {
System.out.println(s);
}
注意:
- 修改增强for中的变量,不会改变集合中原本的数据
Lambda表达式遍历
方法名称 | 说明 |
---|---|
default void forEach(Consumer<? super ?>action): | 结合lambda遍历集合 |
利用匿名内部类的形式
collection.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
Lambda表达式形式
collection.forEach(s -> System.out.println(s));