一、集合
1、集合的由来:
面向对象语言对事物的体现就是以对象这个形式,所以为了方便对这个对象进行操作,Java提出了集合类这个概念,可以将数据以集合的形式存储访问。
2、集合和数组的区别:
* 数组定义时会设置固定的长度,而集合的长度不固定;
* 数组既可以存储基本数据类型,也可以存储引用数据类型,而集合只能存储引用数据类型,
注意:
当集合存储基本数据类型时,会进行自动装箱操作,将基本数据类型转换为相对性的引用数据类型(如int对应的引用类型是integer)。
* 数组只能存储同种类型的数据,一个集合的对象可以存储各种类型的数据。
3、集合体系继承图(仅仅是常用的一些接口、类)
二、Collection接口
1、Collection接口概述:它是所有集合的根接口。
2、Collection集合的功能:
1) 添加功能
* boolean add(Object obj):添加一个元素
例:
import java.util.ArrayList;
import java.util.Collection;
public class Test {
public static void main(String[] args) {
Collection con = new ArrayList();
con.add(1);
System.out.println(con);
con.add("arr");
System.out.println(con);
}
}
//运行结果
[1]
[1,arr]
* 添加一个集合 boolean addAll(Collection c);
例:
import java.util.ArrayList;
import java.util.Collection;
public class Test {
public static void main(String[] args) {
Collection con = new ArrayList();
con.add(1);
con.add("arr");
System.out.println(con);
Collection con1 = new ArrayList();
con1.add(2);
con1.add(23);
con1.add("str");
System.out.println(con1);
//添加功能
con.add(con1);
System.out.println(con);
}
}
//运行结果
[1, arr]
[2, 23, str]
[1, arr, [2, 23, str]]
Process finished with exit code 0
2) 删除功能
* void clear():移除所有元素
* boolean remove(Object o):移除一个元素
* boolean removeAll(Collection c):移除一个集合的元素,(例:con.removeAll(con1),即就是删除con集合中与con2集合交集的元素, 如果没有交集元素,则返回值为false,反之,返回值为true。
案例:定义一个学生Student类,再创建该类的集合对象,并对上述的三种删除方法进行测试:
//学生类
public class Student {
private String name;
private String sex;
private int age;
public Student() {
}
public Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override //toString()方法的重写,打印对象的成员变量的值
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
}
package myDemol1;
import java.util.ArrayList;
import java.util.Collection;
public class Test1 {
public static void main(String[] args) {
Student stu1 = new Student("Bob", "男", 19);
Student stu2 = new Student("Lily", "女", 18);
Student stu3 = new Student("Jack", "男", 18);
Student stu4 = new Student("LiMing", "男", 20);
Collection con = new ArrayList();
con.add(stu1);
con.add(stu2);
con.add(stu3);
//boolean remove(Object o):移除一个元素
con.remove(stu2);
System.out.println("删除一个元素后的con:"+con);
//boolean removeAll(Collection c):移除一个集合的元素
Collection con1 = new ArrayList();
con1.add(stu4);
con1.add(stu1);
con1.add(stu3);
System.out.println("移除前con1:"+con1);
boolean b = con1.removeAll(con);
System.out.println("返回值:"+b);
System.out.println("移除后con1:"+con1);
//void clear():移除所有元素
con.clear();
con1.clear();
System.out.println("clear后的con:"+con);
System.out.println("clear后的con1:"+con1);
}}
//运行结果:
删除一个元素后的con:[Student{name='Bob', sex='男', age=19}, Student{name='Jack', sex='男', age=18}]
移除前con1:[Student{name='LiMing', sex='男', age=20}, Student{name='Bob', sex='男', age=19}, Student{name='Jack', sex='男', age=18}]
返回值:true
移除后con1:[Student{name='LiMing', sex='男', age=20}]
clear后的con:[]
clear后的con1:[]
Process finished with exit code 0
3) 判断功能
* boolean contains(Object o):判断集合中是否包含指定的元素,如果包含该元素,则返回true,否则返回false;
* boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(这个集合 包含 另一个集合中所有的元素才返回true)
比如:{1,2,3 }containsAll {1,2}=true {1,2,3} containsAll {2,3,4}=false
* boolean isEmpty():判断集合是否为空
案例:
import java.util.ArrayList;
import java.util.Collection;
public class Test1 {
public static void main(String[] args) {
Collection con = new ArrayList();
con.add(1);
con.add(2);
con.add(3);
con.add(4);
Collection con1=new ArrayList();
con1.add(4);
con1.add(5);
con1.add(6);
con1.add(7);
Collection con2=new ArrayList();
con2.add(4);
// boolean contains(Object o):判断集合中是否包含指定的元素
boolean b = con.contains(2);
System.out.println(b);
//boolean containsAll(Collection c):判断集合中是否包含指定的集合元素
boolean b1 = con1.containsAll(con);
System.out.println(b1);
boolean b2 = con1.containsAll(con2);
System.out.println(b2);
//boolean isEmpty():判断集合是否为空
System.out.println(con.isEmpty());
System.out.println(con1.isEmpty());
}
}
//运行结果
4) 获取功能
Iterator<E> iterator()(重点),Iterator是迭代器的意思,可以根据迭代器获取集合中的数据元素。
案例:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Test2 {
public static void main(String[] args) {
Collection con = new ArrayList();
con.add(1);
con.add(2);
con.add(3);
con.add(4);
//定义一个迭代器
Iterator iterator = con.iterator();
//iterator.hasNext()判断下一个元素是否为空
while (iterator.hasNext()){
//iterator.next()打印下一个元素
System.out.println(iterator.next());
}
}
}
//运行结果
5) 长度功能
int size():元素的个数
面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?
数组没有length()方法,但是有length属性,直接 数组名.length 调用即可;
字符串有length()方法;
集合没有length()方法,但是他有size()方法,起到的是同一个作用。
6) 交集功能
例如:A集合对B集合取交集,获取到的交集元素放于A集合中,会覆盖A集合中原有的元素。返回的布尔值表示的是A集合是否发生变化
boolean retainAll(Collection c):获取两个集合的交集元素(交集:两个集合都有的元素)
案例:
import java.util.ArrayList;
import java.util.Collection;
public class Test3 {
public static void main(String[] args) {
Collection con = new ArrayList();
con.add(1);
con.add(2);
con.add(3);
con.add(5);
Collection con1=new ArrayList();
con1.add(2);
con1.add(3);
con1.add(6);
con1.add(7);
boolean b = con.retainAll(con1);
System.out.println(b);
System.out.println("操作后的con:"+con);
System.out.println("操作后的con1:"+con1);
}
}
//运行结果
7) 把集合转换为数组
Object[] toArray()
案例:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class Test4 {
public static void main(String[] args) {
Collection con = new ArrayList();
con.add(1);
con.add(2);
con.add(3);
con.add(5);
//集合转为数组
Object[] array = con.toArray();
for(int i=0;i<array.length;i++){
System.out.print(array[i]+",");
}
System.out.println();
//数组转为集合,
List list = Arrays.asList(array);
System.out.println("集合:"+list);
//注意该集合不能再进行添加、移除操作,否则会编译错误
//list.add(19);
}
}
//运行结果
//注意:该集合不能再进行添加、移除操作,否则会编译错误,如下:
三、List接口
1、List概述及特点:
元素有序,并且每一个元素都存在一个索引.
元素可以重复。
list集合完全可以调用上述介绍的Collection接口的各种功能,在此基础上,他还有自己独特的功能。
2、List集合的特有功能概述:
1) 添加功能
void add(int index,E element): 在指定索引处添加元素
2) 移除功能
E remove(int index):移除指定索引处的元素 返回的是移除的元素
3) 获取元素
E get(int index):获取指定索引处的元素
4) 更改元素
E set(int index,E element):更改指定索引处的元素 返回的而是被替换的元素
//案例:
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
Student stu1 = new Student("Bob", "男", 19);
Student stu2 = new Student("Lily", "女", 18);
Student stu3 = new Student("Jack", "男", 18);
Student stu4 = new Student("LiMing", "男", 20);
List list=new ArrayList();
//添加功能
list.add(stu1);
list.add(stu2);
list.add(stu3);
System.out.println("原有list集合的元素:"+list);
//在指定的索引处添加
list.add(3,stu4);
System.out.println("添加指定位置后的集合:"+list);
//移除指定索引处的元素
list.remove(2);
System.out.println("移除2位置后的集合:"+list);
//获取指定索引处的元素
System.out.println("获取1位置的元素:"+list.get(1));
//更改制定索引处的元素
System.out.println("更改前的:"+list.get(0));
list.set(0,stu4);
System.out.println("更改后的:"+list.get(0));
}
}
//运行结果
原有list集合的元素:[Student{name='Bob', sex='男', age=19}, Student{name='Lily', sex='女', age=18},
Student {name='Jack', sex='男', age=18}]
添加指定位置后的集合:[Student{name='Bob', sex='男', age=19}, Student{name='Lily', sex='女', age=18},
Student{name='Jack', sex='男', age=18}, Student{name='LiMing', sex='男', age=20}]
移除2位置后的集合:[Student{name='Bob', sex='男', age=19}, Student{name='Lily', sex='女', age=18},
Student{name='LiMing', sex='男', age=20}]
获取1位置的元素:Student{name='Lily', sex='女', age=18}
更改前的:Student{name='Bob', sex='男', age=19}
更改后的:Student{name='LiMing', sex='男', age=20}
3、List集合的遍历方法
1) 方法1:使用Collection接口的迭代器Iterator
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test3 {
public static void main(String[] args) {
//方法1
List list=new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
//定义一个迭代器
Iterator iterator = list.iterator();
//iterator.hasNext()判断下一个元素是否为空
while (iterator.hasNext()) {
//iterator.next()打印下一个元素
System.out.println(iterator.next());
}
}
}
2) 方法2:使用List集合特有的ListIterator迭代器
* 从前往后获取(案例仍为上述的List集合)
//方法2
ListIterator listIterator=list.listIterator();
while (listIterator.hasNext()){
System.out.println(listIterator.next());
}
* 从后往前获取
//方法2,从后往前获取
//先将指针移到最后的索引位置
ListIterator listIterator=list.listIterator();
while (listIterator.hasNext()){
listIterator.next();
}
//调用List转悠方法从后往前获取元素
while (listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
3) 方法3:使用for循环
//方法3
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
4、并发修改异常产生的原因及解决方案
* 什么是并发修改异常?
先看一个小案例了解一下:
需求:我有一个集合,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素。
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Test4 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
ListIterator<String> iterator = list.listIterator();
while (iterator.hasNext()){
String ele = iterator.next();
if(ele.equals("world")){
list.add("javaee");
}
}
System.out.println(list);
}
}
//分析:这种做法逻辑上是没有问题的,但是在编译上会出现上述说的问题,原因是我们用了Iterator这个迭代
器遍历(采用hasNext方法和next方法)集合,并且修改集合。我们的迭代依赖于集合,当我们往集合中
添加好了元素之后,获取迭代器,那么迭代器已经知道了集合的元素个数,这个时候你在遍历的时候又突
然想给集合里面加一个元素(用的是集合的add方法),那迭代器不同意 就报错了。
//解决方法:
方法1:我们用ListIterator迭代器遍历,用迭代器自带的add方法添加元素,就不会报错了
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Test4 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
ListIterator<String> iterator = list.listIterator();
while (iterator.hasNext()){
String ele = iterator.next();
if(ele.equals("world")){
iterator.add("javaee"); //此处改正
}
}
System.out.println(list);
}
}
//方法2:使用for循环遍历集合 添加元素 不会报错
import java.util.ArrayList;
import java.util.List;
public class Test4 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
//采用for 循环去遍历集合,就没有这个并发修改异常
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("world")) {
list.add("haah");
}
}
System.out.println(list);
}
}