jdk中的描述:
三、 集合 Collection 的方法
- 集合 Collection 的方法是实现类必须拥有的方法
1. 第一类:针对单个元素的操作
boolean add(E e)
:将元素追加到当前元素集合的末尾,添加成功返回 trueObject[] toArray()
:将集合中的元素,转成一个数组中的元素,**集合转成数组**
。返回是一个存储对象的数组
-
boolean contains(Object o)
:判断对象是否存在于集合中,对象存在返回 true -
void clear()
:清空集合中的所有元素,集合容器本身依然存在 -
boolean remove(Object o)
-
移除集合中指定的元素,删除成功返回 true,
-
当删除不存在的元素时,只会返回 false
-
当集合中有重复元素时,只会删除 第一个
-
boolean contains(Object o)
:判断当前集合中是否包含指定元素,是则返回true -
boolean isEmpty()
:判断集合是否为空
private static void function(){
//接口多态调用
Collection coll = new ArrayList();
coll.isEmpty();
coll.add(“abc”);
coll.add(“money”);
coll.add(“itcast”);
coll.add(“itheima”);
coll.add(“money”);
coll.add(“123”);
boolean b = coll.remove(“money”);
boolean b = coll.contains(“itcast”);
System.out.println(a.contains(“lisi”)); //返回fause
coll.clear();
}
2. 第二类:针对集合的操作(一次操作多个元素)
boolean addAll(Collection c)
:将指定 collection 中的所有元素都添加到此 collection 中.boolean removeAll(Collection c)
:移除此 collection 中那些也包含在指定 collection 中的所有元素boolean containsAll(Collection c)
:如果此 collection 包含指定 collection 中的所有元素,则返回 trueboolean retainAll(Collection c)
:仅保留此 collection 中那些也包含在指定 collection 的元素。如果此 collection 由于 调用 而发生更改,则返回 true,否则返回 false
public class CollectionDemo {
public static void main(String[] args) {
//创建第一个集合对象
Collection a = new ArrayList<>();
a.add(“zs”);
a.add(“lisi”);
a.add(“wangwu”);
//创建第二个集合对象
Collection b = new ArrayList<>();
b.add(“zs”);
b.add(“lisi”);
b.add(“wangwu”);
//boolean addAll(Collection c)
//boolean result = a.addAll(b);
//System.out.println(result);
//b集合中的元素,复制了一份全部添加到a集合中
//System.out.println(a);
//b集合本身没有任何变化
//System.out.println(b);
//boolean removeAll(Collection c)
// a.removeAll(b);
// System.out.println(a);
//boolean containsAll(Collection c)
//System.out.println(a.containsAll(b));
//boolean retainAll(Collection c)
System.out.println(a.retainAll(b));
System.out.println(a);
System.out.println(b); //b集合没有任何变化
}
}
三、集合元素的遍历
1. 第一种遍历方式: 将集合转化为数组
public class CollecitonDemo {
public static void main(String[] args) {
//第一步,得到一个集合对象
Collection a = new ArrayList();
Collection b = new ArrayList();
a.add(“zs”);
a.add(“lisi”);
a.add(“wangwu”);
a.add(“zhaoliu”);
//第一种遍历方式: 将集合转化为数组
Object[] objects = a.toArray();
for (int i = 0; i < objects.length; i++) {
System.out.print(objects[i] + " ");
}
System.out.println();
}
}
2. 第二种遍历方式: 通过迭代器遍历集合中的所有元素(集合专用)
- 后面详细介绍
四、Iterator 接口 (迭代器)
1. 迭代器概述
Collection
集合元素的通用获取方式:在取元素之前先要判断集合中有没有元素,如果有,就把这个元素取出来,继续在判断,如果还有就再取出出来。一直把集合中的所有元素全部取出。这种取出方式专业术语称为 迭代。- 每种集合的底层的数据结构不同,例如
ArrayList
是数组,LinkedList
底层是链表,但是无论使用那种集合,我们都会有判断是否有元素,以及取出里面的元素的动作,那么Java为我们提供一个迭代器定义了统一的****判断元素和取元素的方法 - Collection 接口定义的
iterator()方法
用来返回基于当前数据集合的迭代器对象。 - 迭代器对象是 依赖当前数据集合的
- 迭代器的优点:
2. Iterator 接口的抽象方法
boolean hasNext()
:判断集合中还有没有可以被取出的元素,如果有返回 truenext()
:取出集合中的下一个元素remove()
: 删除集合中当前访问的元素
3. 遍历的代码实现
/*
原理:
Collection接口定义方法 Iterator iterator()
ArrayList 重写方法 iterator(),返回了Iterator接口的实现类的对象
使用 ArrayList 集合的对象Iterator it = array.iterator(),运行结果就是Iterator接口的实现类的对象
it是接口的实现类对象,调用方法 hasNext 和 next 来实现集合元素迭代
*/
public class CollecitonDemo {
public static void main(String[] args) {
//第一步,得到一个集合对象
Collection a = new ArrayList();
Collection b = new ArrayList();
a.add(“zs”);
a.add(“lisi”);
a.add(“wangwu”);
a.add(“zhaoliu”);
//集合类专用的遍历方式 通过迭代器遍历集合中的所有元素
//调用集合的方法iterator()获取出 Iterator接口的实现类的对象
Iterator iterator = a.iterator();
//利用迭代器遍历
//迭代是反复内容,使用循环实现,循环的条件,集合中没元素, hasNext()返回了false
//调用方法hasNext()判断集合中是否有元素
while(iterator.hasNext()) {
//调用方法next()取出集合中的元素
System.out.println(iterator.next());
}
//for循环迭代写法:
// for (Iterator it2 = coll.iterator(); //it2.hasNext(); ) {
// System.out.println(it2.next());
// }
//利用迭代器删除集合中的元素
//注意要执行,要把前面的注释掉,因为size已经在最后了
//迭代是反复内容,使用循环实现,循环的条件是集合中没元素, hasNext()返回了false
while(iterator.hasNext()) {
String s = iterator.next();
//if(1 == i) 和 if(i == 1)
if(“wangwu”.equals(s)) {
//从Collection中删除当前访问到的元素
iterator.remove();
}
}
System.out.println(a);
Collection list = new LinkedList<>();
list.add(“stu1”);
list.add(“stu2”);
list.add(“stu3”);
list.add(“stu4”);
//利用迭代器,遍历LinkedList
Iterator iterator1 = list.iterator();
while(iterator1.hasNext()) {
System.out.println(iterator1.next());
}
}
}
4. 实现原理
/实现原理
while(it.hasNext()) {
System.out.println(it.next());
}
//cursor记录的索引值不等于集合的长度返回true,否则返回false
public boolean hasNext() {
return cursor != size; //cursor初值为0
}
//next()方法作用:
//返回cursor指向的当前元素
//cursor++
public Object next() {
int i = cursor;
cursor = i + 1;
return elementData[lastRet = i];
}
5. 一个例题:遍历对象
- 题目:存储自定义 对象 并遍历,Student(name,age)
public class Student {
//成员属性
private String name;
private int age;
//构造方法
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//get和set方法
public String getName() { 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//重写toString(),实现输出对象
@Override
public String toString() {
return “Student{” +
“name='” + name + ‘’’ +
“, age=” + age +
‘}’;
}
}
public class IteratorExercise {
public static void main(String[] args) {
//初始化一个存放Student类对象的集合对象