JAVASE基础模块二十二(Collection集合类)
list 集合
- 为什么会用集合
- 数组作为容器的不好之处 就是对容器中的元素 进行操作时 不够方便
- 比如增删数组元素 就不方便
- 因为数组定义后 长度就不能改变了
import java.util.Arrays;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class Test {
public static void main(String[] args) {
Student x1 = new Student("x1", 21);
Student x2 = new Student("x2", 22);
Student x3 = new Student("x3", 23);
Student x4 = new Student("x4", 24);
Student[] s = new Student[]{x1, x2, x3};
for (int i = 0; i < s.length; i++) {
Student d = s[i];
System.out.println(d.getName() + "---" + d.getAge());
}
Student[] aa=new Student[4];
aa= Arrays.copyOf(s,s.length+1);
aa[aa.length-1]=x4;
System.out.println(Arrays.toString(aa));
}
}
运行结果:
x1---21
x2---22
x3---23
[Student{name='x1', age=21}, Student{name='x2', age=22}, Student{name='x3', age=23}, Student{name='x4', age=24}]
Process finished with exit code 0
-
java为了我们更加方便的去操作容器中的元素 提供了Collectiont 集合这个新的容器
-
集合与数组的区别
- 数组的长度 一单定义就固定了 集合的长度是可变的
- 数组既可以存储基本数据类型元素 也可以存储引用数据类型元素 集合只能存储引用数据类型元素
- 数组只能存储同种数据类型元素 集合可以存储不同数据类型元素
-
Collection 集合的顶层接口
- 接口 List
- ArrayList 底层数据结构是数组 查询快 增删慢 线程不安全 效率高
- LinkedList 底层数据结构是链表 查询慢 增删快 线程不安全 效率高
- Vector 底层数据结构是数组 查询快 增删慢 线程安全 效率低
- 接口 Set
- HashSet
- TreeSet
- LinkedHashSet
- 接口 List
Collection方法
-
添加元素 add
-
添加普通元素
import java.util.ArrayList; import java.util.Collection; public class Coll { public static void main(String[] args) { Collection c = new ArrayList(); //添加元素 c.add("zz"); c.add("zz"); c.add("zz"); c.add("zz"); c.add("zz"); c.add("zz"); System.out.println(c); } } 运行结果: [zz, zz, zz, zz, zz, zz] Process finished with exit code 0
-
添加数值 (可以直接添加 会进行自动装箱)
import java.util.ArrayList; import java.util.Collection; public class Cv { public static void main(String[] args) { int num=333; Collection c = new ArrayList(); //添加元素 c.add("zz"); c.add("zz"); c.add(num);//自动装箱 System.out.println(c); } } 运行结果: [zz, zz, 333] Process finished with exit code 0
-
添加对象
import java.util.ArrayList; import java.util.Collection; public class Df { public static void main(String[] args) { Collection c = new ArrayList(); Student x1 = new Student("x1", 21); Student x2 = new Student("x2", 22); Student x3 = new Student("x3", 23); Student x4 = new Student("x4", 24); //添加元素 c.add(x1); c.add(x2); c.add(x3); c.add(x4); System.out.println(c); } } class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public Student() { } 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; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } } 运行结果: [Student{name='x1', age=21}, Student{name='x2', age=22}, Student{name='x3', age=23}, Student{name='x4', age=24}] Process finished with exit code 0
-
addAll()方法 将一个collection添加到另一个collection
import java.util.ArrayList; import java.util.Collection; public class Ff { public static void main(String[] args) { Collection c = new ArrayList(); //添加元素 c.add(100); c.add(200); Collection cc = new ArrayList(); //添加元素 cc.add(333); cc.add(333); boolean d=c.addAll(cc); System.out.println(d); System.out.println(c); System.out.println(cc); } } 运行结果: true [100, 200, 333, 333] [333, 333] Process finished with exit code 0
-
-
清空 clear 清空此集合中的所有元素
import java.util.ArrayList; import java.util.Collection; public class QuanBu { public static void main(String[] args) { Collection cc = new ArrayList(); cc.add(333); cc.add(333); System.out.println(cc); //清空 cc.clear(); System.out.println(cc); } } 运行结果: [333, 333] [] Process finished with exit code 0
-
删除 remove
-
remove() 删除指定元素
import java.util.ArrayList; import java.util.Collection; public class QuanBu { public static void main(String[] args) { Collection c = new ArrayList(); c.add(100); c.add(200); c.add(333); c.add(777); c.add(999); System.out.println(c); //删除 c.remove(333); System.out.println(c); } } 运行结果: [100, 200, 333, 777, 999] [100, 200, 777, 999] Process finished with exit code 0
-
removeAll() 删除与另外一个集合内的重复元素
import java.util.ArrayList; import java.util.Collection; public class Removall { public static void main(String[] args) { Collection cc = new ArrayList(); //添加元素 cc.add(333); cc.add(123); cc.add(777); cc.add(999); Collection c = new ArrayList(); //添加元素 c.add(123); c.add(200); c.add(777); c.add(999); System.out.println(c); System.out.println(cc); c.removeAll(cc); System.out.println(c); System.out.println(cc); } } 运行结果: [123, 200, 777, 999] [333, 123, 777, 999] [200] [333, 123, 777, 999] Process finished with exit code 0
-
-
包含 contain
-
contain() 查看某个元素是否在集合内
import java.util.ArrayList; import java.util.Collection; public class Cont { public static void main(String[] args) { Collection c = new ArrayList(); c.add(123); c.add(200); c.add(777); c.add(999); System.out.println(c.contains(999)); System.out.println(c.contains(9995)); c.clear(); System.out.println(c.isEmpty()); } } 运行结果: true false true Process finished with exit code 0
-
containAll() 查看一个集合是否完全包含在另一个集合内
import java.util.ArrayList; import java.util.Collection; public class Contall { public static void main(String[] args) { Collection cc = new ArrayList(); cc.add(333); cc.add(123); cc.add(777); cc.add(999); cc.add(200); Collection c = new ArrayList(); //添加元素 c.add(123); c.add(777); c.add(999); System.out.println(c.containsAll(cc)); System.out.println(cc.containsAll(c)); } } 运行结果: false true Process finished with exit code 0
-
-
size() 集合长度
-
retainAll() 取交集
import java.util.ArrayList; import java.util.Collection; public class JiaoJi { public static void main(String[] args) { Collection cc = new ArrayList(); //添加元素 cc.add(3336); cc.add(123); cc.add(777); cc.add(9995); Collection c = new ArrayList(); //添加元素 c.add(123); c.add(200); c.add(777); c.add(999); System.out.println(c); System.out.println(cc); boolean i = c.retainAll(cc); System.out.println(i); System.out.println(c); System.out.println(cc); } } 运行结果: [123, 200, 777, 999] [3336, 123, 777, 9995] true [123, 777] [3336, 123, 777, 9995] Process finished with exit code 0
-
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; public class ShuZu { public static void main(String[] args) { Collection cc = new ArrayList(); cc.add(3336); cc.add(123); cc.add(777); cc.add(9995); Object[] ccc=new Object[cc.size()]; Iterator s=cc.iterator(); int r=0; while(s.hasNext()){ Object u=s.next(); ccc[r]=u; r++; } System.out.println(Arrays.toString(ccc)); Object[] jj=cc.toArray(); System.out.println(Arrays.toString(jj)); } } 运行结果: [3336, 123, 777, 9995] [3336, 123, 777, 9995] Process finished with exit code 0
List
- List常用方法
- 根据索引插入
- 通过索引获取元素
- 根据索引来删除元素
- 根据元素值删除数值 包装一下 一次只删除一个
- 替换集合中的元素 返回的是被替换的旧元素
- 查找数值索引
import java.util.ArrayList;
public class List {
public static void main(String[] args) {
ArrayList v = new ArrayList();
v.add(1001);
v.add(1002);
v.add(1003);
v.add(1004);
v.add(1005);
v.add(1006);
//根据索引插入
v.add(6,5555);
System.out.println(v);
//通过索引获取元素
System.out.println(v.get(v.size() - 1));
//根据索引来删除元素
v.remove(4);
System.out.println(v);
//根据元素值删除数值 包装一下 一次只删除一个
v.remove(Integer.valueOf(1002));
System.out.println(v);
//替换集合中的元素 返回的是被替换的旧元素
Object u=v.set(v.size()-1,7777);
System.out.println(u);
System.out.println(v);
//查找数值索引
int y=v.indexOf(1003);
System.out.println(y);
}
}
运行结果:
[1001, 1002, 1003, 1004, 1005, 1006, 5555]
5555
[1001, 1002, 1003, 1004, 1006, 5555]
[1001, 1003, 1004, 1006, 5555]
5555
[1001, 1003, 1004, 1006, 7777]
1
Process finished with exit code 0
- 迭代
- 正向迭代
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class BianLi {
public static void main(String[] args) {
ArrayList v = new ArrayList();
v.add(1001);
v.add(1002);
v.add(1003);
v.add(1004);
v.add(1005);
v.add(1006);
for (int i = 0; i < v.size(); i++) {
Object u = v.get(i);
System.out.print(u + " ");
}
System.out.println("--------->");
Iterator ff = v.iterator();
while (ff.hasNext()) {
Object kk = ff.next();
System.out.print(kk + " ");
}
System.out.println("--------->");
//java.util.ArrayList$ListItr@10f87f48 内部类
ListIterator yy = v.listIterator();
System.out.println(yy);
while (yy.hasNext()) {
Object kkk = yy.next();
System.out.print(kkk + " ");
}
}
}
运行结果:
1001 1002 1003 1004 1005 1006 --------->
1001 1002 1003 1004 1005 1006 --------->
java.util.ArrayList$ListItr@7291c18f
1001 1002 1003 1004 1005 1006
Process finished with exit code 0
- 反向迭代 (前提是必须进行正向迭代)
import java.util.ArrayList;
import java.util.ListIterator;
public class gg {
public static void main(String[] args) {
ArrayList v = new ArrayList();
v.add(1001);
v.add(1002);
v.add(1003);
v.add(1004);
v.add(1005);
v.add(1006);
ListIterator yy = v.listIterator();
System.out.println(yy);
while (yy.hasNext()) {
Object kkk = yy.next();
System.out.print(kkk + " ");
}
System.out.println();
while (yy.hasPrevious()) {
Object kkk = yy.previous();
System.out.print(kkk + " ");
}
}
}
运行结果:
java.util.ArrayList$ListItr@10f87f48
1001 1002 1003 1004 1005 1006
1006 1005 1004 1003 1002 1001
Process finished with exit code 0
- ConcurrentModificationException 异常分析
import java.util.ArrayList;
import java.util.ListIterator;
public class LianXi {
public static void main(String[] args) {
ArrayList v = new ArrayList();
v.add("world");
v.add("333333");
v.add("333333");
v.add("333333");
v.add("333333");
ListIterator yy = v.listIterator();
System.out.println(yy);
while (yy.hasNext()) {
if (yy.next().equals("world")) {
v.add("Javaee");
}
}
System.out.println(v);
}
}
迭代器预先拿到了集合的长度 在迭代过程中再加入 会打乱迭代器取到的集合长度就会出现并发修改异常
解决方案一:yy.add("Javaee"); 使用list自己的迭代器
[world, Javaee, 333333, 333333, 333333, 333333]
Process finished with exit code 0
解决方案二: if (v.contains("world")) {
System.out.println("222");
v.add(v.size(),"JAVAEE");}
[world, 333333, 333333, 333333, 333333, JAVAEE]
Process finished with exit code 0
- sublist() 含头不含尾
public class iiii {
public static void main(String[] args) {
ArrayList v = new ArrayList();
v.add("world");
v.add("335633");
v.add("334533");
v.add("333133");
v.add("333883");
System.out.println(v.subList(1, 3));
}
}
运行结果:
[335633, 334533]
Process finished with exit code 0