Java-进阶:集合框架1

jdk中的描述:

Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。 
一些 collection 允许有重复的元素,而另一些则不允许。一些collection 是有序的,而另一些则是无序的。 
JDK 不提供此接口的任何直接,实现:它提供更具体的子接口(如 Set 和 List)实现


三、 集合 Collection 的方法

Java中三种长度表现形式
数组.length 属性 返回值 int
字符串.length() 方法,返回值 int
集合.size() 方法, 返回值 int

  • 集合 Collection 的方法是实现类必须拥有的方法

1. 第一类:针对单个元素的操作

  • boolean add(E e)将元素追加到当前元素集合的末尾,添加成功返回 true
  • Object[] toArray():将集合中的元素,转成一个数组中的元素, **集合转成数组**。返回是一个存储对象的数组

集合对象中,已经覆盖了ObjecttoString方法,所以可以直接打印集合中的元素值

  • 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 中的所有元素,则返回 true
  • boolean 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()方法 用来返回基于当前数据集合的迭代器对象。
  • 迭代器对象是 依赖当前数据集合的

Iterator 为什么不是定义成一个类,而是接口? 
因为:功能上没什么问题,但是由于一旦把针对不同具体集合的生成迭代器的方法,放在一个类中,通常,各个不同的迭代器的方法,可能会有一些公共行为,被抽成一个方法。就会因为一些公共操作的存在,产生错综复杂的联系,形成 上帝类(God Class) 啥都管,很难维护

  • 迭代器的优点

迭代器是一个接口,每一个具体的集合类都实现了 Iterator 接口,能够返回一个针对自己的存储结构的具体的 Iterator 接口的实现子类。
Iterator向上层的使用者,屏蔽了具体的底层集合类的实现细节,使用方法都一样

2. Iterator 接口的抽象方法

  • boolean hasNext():判断集合中还有没有可以被取出的元素,如果有返回 true
  • next():取出集合中的下一个元素
  • 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类对象的集合对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值