@2023/10/15(点击更换时间)
java|:集合框架
集合体系结构
Collection
Collection是单列集合的祖宗接口,它的功能是全部单列集合都可以继承使用的。
List系列集合:添加的元素是有序、可重复、有索引
Set系列集合:添加的元素是无序、不重复、无索引
public static void main(String[] args) {
/**
* 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是一个接口,我们不能直接创建它的对象。
* 所以,现在我们学习他的方法是,只能创建它的实现类的对象。
* 实现类ArrayList
*/
//目的:为了学习Collection接口里面的方法
//自己在做练习的时候可以按
//ArrayList list = new ArrayList<>();
Collection coll = new ArrayList<>();
//1.添加元素
/*细节1:如果我们要往List系列集合中添加数据,
那么方法永远返回true,因为List系列的是允许元素重复的。*/
/*细节2:如果我们要往Set系列集合中添加数据,
如果当前要添加元素不存在,方法返回true,表示添加成功。
如果当前要添加元素已经存在,方法返回false,表示添加失败。
因为Set系列的集合不允许重复。
*/
coll.add("aaa");
coll.add("bbb");
coll.add("ccc");
System.out.println(coll);
//2.清空
//coll.clear();
//3.删除
//细节1:因为Collection里面定义的是共性的方法,所以此时不能通过索引删除(Set系列集合没有索引),只能通过元素的对象进行删除。
//细节2:方法会有一个boolean类型的返回值,产出成功返回true,删除失败返回false。
//如果要删除的元素不存在,就会删除失败。
coll.remove("aaa");
System.out.println(coll);
//4.判断元素是否包含
//细节:底层是依赖equals方法进行判断是否存在的。
//所以,如果集合中存储的是自定义对象,也想通过contains方法来判断是包含涵,那么在javabean类中,一定要重写equals方法。
boolean result = coll.contains("bbb");
System.out.println(result);
//5.判断集合是否为空
boolean result2 = coll.isEmpty();
System.out.println(result2);
//6.获取集合的长度
System.out.println(coll.size());
}
public static void main(String[] args) {
//1.创建集合的对象
Collection coll = new ArrayList<>();
//2.创建学生对象
Student s1 = new Student("zhangsan",23);
Student s2 = new Student("lisi",23);
Student s3 = new Student("wangwu",23);
//3.把学生对象放入到集合中
coll.add(s1);
coll.add(s2);
coll.add(s3);
//4.判断集合中某一个学生对象是否包含
Student s4 = new Student("zhangsan",23);
//因为contains方法在底层依赖equals方法判断对象是否一致的。
//如果存的是自定义对象,没有重写equals方法,那么默认使用Object类中的equals方法进行判断
//而Object类中的equals方法,依赖地址值进行判断。
//需求:如果同姓名、年龄就认为是同一个学生
//所以需要在自定义的Javabean类中重写equals方法就可以了
boolean result = coll.contains(s4);
System.out.println(result);
}
迭代器不依赖索引
迭代器在Java中的类是Iterator,迭代器是集合专用的遍历方式。
Collection集合获取迭代器
Iterator iterator()
返回迭代器对象,默认指向当前集合的0索引
Iterator中的常用方法
boolean hasNext()
判断当前位置是否有元素,有true,没有false
E next()
获取当前位置的元素,并将迭代器对象移向下一个位置
增强for的细节:
修改增强for中的变量,不会改变集合中原本的数据
/**
* 迭代器遍历相关的三个方法
* Iterator iterator() :获取一个迭代器对象
* boolean hasNext() :判断当前指向的位置是否有元素
* E next() :获取当前指向的元素并移动指针
*
*/
public static void main(String[] args) {
//1.创建集合并添加元素
Collection coll = new ArrayList<>();
coll.add("aaa");
coll.add("bbb");
coll.add("ccc");
coll.add("ddd");
//2.获取迭代器对象
//迭代器就好比是一个箭头,默认指向集合的0索引处
Iterator it = coll.iterator();
//3.利用循环不断地取获取集合的0索引处
while(it.hasNext()){
//4.next方法的两件事情:获取元素并移动指针
String str = it.next();
System.out.println(str);
}
}
‼️迭代器的细节注意点:
1.报错NoSuchElemeException
//当上面的循环结束之后,迭代器的指针已经指向了最后没有元素的位置
System.out.println(it.next());//NoSuchElementException
2.迭代器遍历完毕,指针不会复位
//迭代器遍历完毕,指针不会复位
System.out.println(it.hasNext());
//如果我们要继续第二次遍历集合,只能再次获取一个新的迭代器对象
Iterator it2 = coll.iterator();
while(it2.hasNext()){
String str = it2.next();
System.out.println(str);
3.循环中只能使用一次next方法
while(it.hasNext()){
//4.next方法的两件事情:获取元素并移动指针
System.out.println(it.next());//aaa ccc eee
System.out.println(it.next());//bbb ddd NoSuchElementException
}
4.迭代器遍历时,不能用集合的方法中对集合进行增加或者删除
while(it.hasNext()){
//4.next方法的两件事情:获取元素并移动指针
String str = it.next();
if("bbb".equals(str)){
coll.remove("bbb");
}
System.out.println(str);//ConcurrentModificationException
增强for的底层就是迭代器,为了简化迭代器的代码书写的。
它是JDK5之后出现的,器内部原理就是一个Iterator迭代器
所有的单列集合和数组才能用增强for进行遍历
public static void main(String[] args) {
/**
*
* 增强for格式:
* for(数据类型 变量名: 集合/数组){
*
* }
*
* 快速生成方式:
* 集合的名字 + for 回车
*/
//1.创建集合并添加元素
Collection coll = new ArrayList<>();
coll.add("aaa");
coll.add("bbb");
coll.add("ccc");
coll.add("ddd");
coll.add("eee");
//2.利用增强for进行遍历
//注意点
//s其实就是一个第三方变量,在循环的过程中依次表示集合中的每一个数据
for (String s:coll) {
System.out.println(s);
}
}
得益于JDK 8开始的新技术Lambda表达式,提供了一种更简单、更直接的遍历集合的方式
/**
* * Collection系列集合三种通用的遍历方式
* * 1.迭代器遍历
* * 2.增强for遍历
* * 3,Lambda表达式遍历
*Lambda表达式遍历:
* default void foreach(Consumer action
*
*/
public static void main(String[] args) {
//1.创建集合并添加元素
Collection coll = new ArrayList<>();
coll.add("zhangsan");
coll.add("lisi");
coll.add("wangwu");
//2.利用匿名内部类的形式
//底层原理:
//其实也会自己遍历集合,一次得到每一个元素
//把得到的每一个元素,传递给下面的accept方法
//s一次表示集合中的每一个数据
/* coll.forEach(new Consumer(){
@Override
public void accept(String s) {
System.out.println(s);
}
});*/
//lamda表达式
//()->{}
coll.forEach(s -> System.out.println(s));
}
1.Collection是单列集合的顶层接口,所有方法被List和Set系列集合共享
2.常见的成员方法:
add、clear、remove、contains、isEmpty、size
3.三种通用的遍历方式:
迭代器:在遍历的过程中需要删除元素,请使用迭代器。
增强for、Lambda:仅仅想遍历,那么使用增强for或Lambda表达式。
练习:
//1.创建对象
FileInputStream fis = new FileInputStream("D:\\myio\\movie.mp4");
FileOutputStream fos = new FileOutputStream("myio\\copy.mp4");
//2.拷贝
//核心思想:边读边写
int b;
while((b = fis.read()) !=-1){
fos.write(b);
}
//3.释放资源
//规则:先开的后关闭
fos.close();
fis.close();
//1.创建对象
FileInputStream fis = new FileInputStream("D:\\myio\\movie.mp4);
FileOutputStream fos = new FileOutStream("myio\\copy.mp4");
//2.拷贝
int len;
byte[] bytes = new byte[1024*1024*5];
while((len = fis.read(bytes)) != -1){
fos.write(bytes,0,len)
}
//3.释放资源
fos.close();
fis.close();
FileInputStream一次读多个字节
public int read()
一次读一个字节
public int read(byte[ ] buffer)
一次读多个字节
注意:一次读一个字节数组的数据,每次读取会尽可能把数组装满
//1.创建对象
FileInputStream fis = new FileInputStream("myio\\a.txt");//文本:abcde
//2.读取数据
int len;//记录读了几个
byte[] bytes = new byte[2];//读取到的数据
//一次都多个字节数据,具体读多少,跟数组的长度有关
//返回值,本次读到了多少个字节数据
len = fis.read(bytes);
System.out.println(len);//2
System.out.println(new String(bytes));//ab
len = fis.read(bytes);
System.out.println(len);//2
System.out.println(new String(bytes));//cd
len = fis.read(bytes);
System.out.println(len);
System.out.println(new String(bytes));//ed
//3.释放资源
fis.close();
//1.创建对象
FileInputStream fis = new FileInputStream("myio\\a.txt");//文本:abcde
//2.读取数据
byte[] bytes = new byte[2];//读取到的数据
//一次都多个字节数据,具体读多少,跟数组的长度有关
//返回值,本次读到了多少个字节数据
len1 = fis.read(bytes);
System.out.println(len1);//2
String str1 = new String(bytes,0,len1);
System.out.println(str1);//ab
len2 = fis.read(bytes);
System.out.println(len2);//2
String str2 = new String(bytes,0,len2);
System.out.println(str2);//cd
len3 = fis.read(bytes);
System.out.println(len2);//1
String str3 = new String(bytes,0,len3);
System.out.println(str3);//e
//3.释放资源
fis.close();
总结栏-【后期复习回顾完成】