java之集合–Collection
文章目录
一、集合是什么?
集合,是对象的容器,用于存储各种类型的对象,只能存储对象。定义了对多个对象进行操作的常用方法,可实现数组的功能。
位置:java.util.*;
二、Collection父接口
2.1 概念
代表一组任意类型的对象,由于本身是接口,不能创建对象,所以需要通过向上转型的方式来创建对象,以其实现类ArrayList
为例:
Collection c = new ArrayList();//通过向上转型的方式创建对象
集合与数组的区别:
- 数组长度固定,集合长度不固定。
- 数组可以存储基本类型和引用类型,集合只能存储引用类型。
注意:
集合中只能存储对象,每个元素代表了对象的地址。
Collection c1 = new ArrayList();
c1.add(125);
以上代码并不是存储了125这个基本类型数据,而是进行了自动装箱
后,存储的包装类对象。
2.2 分类
其中Collection是父接口,蓝色箭头的起始点都是接口,橙色箭头的起始点都是类型。
List类型的集合
2.3 常用方法
collection父接口所声明的方法,其子类都可以使用,且作用一致
2.3.1 size()
作用:返回集合的长度
返回值类型:int
代码示例:
Collection c1 = new ArrayList();
System.out.println(c1.size);
输出结果
0
2.3.2 add(Object obj)
作用:在集合中添加一个对象
返回值类型:boolean
代码示例:
Collection collection = new ArrayList();
Student stu = new Student();
collection.add(stu);
2.3.3 addAll(Collection c)
作用:将一个集合中的所有对象添加到此集合中
返回值类型:boolean
代码示例:
Collection c1 = new ArrayList();
Collection c2 = new ArrayList();
Student stu1 = new Student();
c1.add(stu1);//集合c1添加了学生对象
c2.addAll(c1);//集合c2获取了c1中的所有属性
2.3.4 clear()
作用:清空整个集合的元素
返回值类型:无
代码示例:
Collection c1 = new ArrayList();
c1.add("张三");
c1.add("李四");
c1.add("王五");
c1.clear();//清空集合中的元素
2.3.5 contains(Object obj)
作用:检查集合中是否包含对象obj
返回值类型:boolean
代码示例:
Collection c1 = new ArrayList();
c1.add("张三");
System.out.println(c1.contains("张三"));
System.out.println(c1.contains("李四"));
输出结果:
true
false
2.3.6 containsAll(Collection c)
作用:检查集合中是否包含集合c的全部元素
返回值类型:boolean
代码示例:
Collection c1 = new ArrayList();
Collection c2 = new ArrayList();
c1.add("苹果");
c1.add("香蕉");
c1.add("菠萝");
c2.add("苹果");
c2.add("香蕉");
System.out.println(c1.containsAll(c2));
c2.add("西瓜");
System.out.println(c1.containsAll(c2));
输出结果:
true
false
2.3.7 equals(Object obj)
作用:比较此集合是否与指定对象相等
返回值类型:boolean
代码示例:
2.3.8 isEmpty()
作用:判断此集合是否为空
返回值类型:boolean
代码示例:
Collection c1 = new ArrayList();
System.out.println(c1.isEmpty());
c1.add("张三");
System.out.println(c1.isEmpty());
输出结果:
true
false
2.3.9 remove(Object obj)
作用:在此集合中移除指定的对象
返回值类型:boolean
代码演示:
Collection c1 = new ArrayList();
c1.add("张三");
c1.add("王五");
c1.add("李四");
System.out.println(c1);
c1.remove("李四");
System.out.println(c1);
运行结果:
[张三, 王五, 李四]
[张三, 王五]
remove还有一种使用方法:(非必要掌握,可跳过)
Student类:
public class Student{
String name;
int age;
String gender;
public Student(String name,int age,String gender){
this.name = name;
this.age = age;
this.gender = gender;
}
@Override
public String toString() {
return "Student{" +
"name=" + name +
", age=" + age +
", gender=" + gender +
'}' + '\n';
}
@Override
public boolean equals(Object o) {//必须重写
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name) && Objects.equals(gender, student.gender);
}
@Override
public int hashCode() {//可不重写,但是是行业规范
return Objects.hash(name, age, gender);
}
}
主类:
public class Test{
public static void main(String[] args){
Student stu1 = new Student("张三",15,"男");
Student stu2 = new Student("李四",21,"女");
Student stu3 = new Student("王五",43,"男");
Collection list = new ArrayList();
list.add(stu1);
list.add(stu2);
list.add(stu3);
System.out.println(list);
list.remove(new Student("李四",21,"女"));//重点!这种方式也可以使我们精确删除数据!
//这种方法需要在类中重写equals方法(必须)和hashCode方法(行业规范)才能生效!!
System.out.println(list);
}
}
运行结果:
[Student{name=张三, age=15, gender=男}
, Student{name=李四, age=21, gender=女}
, Student{name=王五, age=43, gender=男}
]
[Student{name=张三, age=15, gender=男}
, Student{name=王五, age=43, gender=男}
]
2.3.10 removeAll(Collection c)
作用:在此集合中移除集合c中所包含的所有元素
返回值类型:boolean
代码演示:
Collection c1 = new ArrayList();
Collection c2 = new ArrayList();
c1.add("张三");
c1.add("王五");
c1.add("张三");
System.out.println("c1中的元素:");
System.out.println(c1);
c2.add("张三");
c2.add("王五");
System.out.println("c2中的元素:");
System.out.println(c2);
c1.removeAll(c2);
System.out.println("c1移除c2中包含的元素后:");
System.out.println(c1);
输出结果
c1中的元素:
[张三, 王五, 张三]
c2中的元素:
[张三, 王五]
c1移除c2中包含的元素后:
[]
总结
今天的文章重点整理了Collection接口的方法,后续会逐个介绍集合中的其他集合类型。