一.集合的概述
- Collection:java是一种面向对象的语言,面向对象对事物的描述是通过对象体现出来的,要存储对象,要使用容器存储
先阶段学习的容器有:数组和字符串缓冲区
数组:长度固定
字符串缓冲区:始终返回一个字符串,并不是想要的结果
因此:java提供了集合:Collection - 集合和数组的区别
- 长度的区别:
数组:长度固定
集合:可变 - 存储数据类型的区别:
数组:可以存储基本数据类型和引用数据类型
集合:只能存储引用数据类型 - 存储类型元素的区别:
数组:存储同一种数据类型的元素
集合:可以存储多种数据类型的元素
- 长度的区别:
- Collection:层次结构中的根接口.
Collection:表示一组对象,这些对象称为Collection的元素.一些Collection允许有重复的元素,一些不允许.一些是有序的,一些是无须的 - Collection接口的子接口set和list
- 集合的体系结构
二.Collection接口的常见方法
添加功能和转换功能
boolean add(E e)
:添加功能boolean addAll(Collection c)
:添加一个集合中的元素Object[] toArray()
:把集合转成数组,可以实现集合的遍历
import java.util.ArrayList;
import java.util.Collection;
class Student{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
public class Demo1 {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//创建学生对象
Student s1 = new Student("王一",21);
Student s2 = new Student("王二",22);
Student s3 = new Student("王三",23);
Student s4 = new Student("王四",24);
//将对象添加到集合中
c.add(s1);
c.add(s2);
c.add(s3);
Collection c2 = new ArrayList();
c2.add(s4);
c2.add(s3);
//添加一个集合中的元素
c.addAll(c2);
Object[] obj = c.toArray();
//遍历输出
for(int x = 0 ; x < obj.length ; x ++){
Student s = (Student)obj[x];
System.out.println(s.getName()+"---"+s.getAge());
}
System.out.println("--------------");
}
}
结果:
王一---21
王二---22
王三---23
王四---24
王三---23
--------------
删除功能
boolean remove(Object o)
:删除指定的元素boolean removeAll(Collection c)
:删除一个集合中的元素(思考:删除该集合中的一个元素算是删除呢还是删除所有算是删除———删除一个算是删除)void clear()
:删除一个集合中的所有元素
import java.util.ArrayList;
import java.util.Collection;
class Student{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
public class Demo1 {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//创建学生对象
Student s1 = new Student("王一",21);
Student s2 = new Student("王二",22);
Student s3 = new Student("王三",23);
Student s4 = new Student("王四",24);
//将对象添加到集合中
c.add(s1);
c.add(s2);
//删除指定的元素
c.remove(s2);
Collection c2 = new ArrayList();
c2.add(s1);
c2.add(s2);
c2.add(s3);
c2.add(s4);
//删除一个集合中的元素
//c1中只有一个元素,删除这一个元素就算删除
c.removeAll(c2);
Object[] obj = c.toArray();
//遍历输出
for(int x = 0 ; x < obj.length ; x ++){
Student s = (Student)obj[x];
System.out.println(s.getName()+"---"+s.getAge());
}
System.out.println("--------------");
Object[] obj2 = c2.toArray();
//遍历输出
for(int x = 0 ; x < obj2.length ; x ++){
Student s = (Student)obj2[x];
System.out.println(s.getName()+"---"+s.getAge());
}
System.out.println("--------------");
}
}
结果:
--------------
王一---21
王二---22
王三---23
王四---24
--------------
判断功能
boolean contains(Object o)
:判断该集合中是否包含指定的元素boolean containsAll(Collection c)
:判断是否包含一个集合中的元素(包含一个算是包含,还是包含所有元素算术包含?—–该集合中包含c集合的所有元素才算包含)boolean isEmpty()
:判断集合是否为空boolean retainAll(Collection c)
:对一个集合取交集(A集合对B集合取交集.交集的元素要去A集合中,返回值类型表达的意思是:A集合的元素是否发生变化)
import java.util.ArrayList;
import java.util.Collection;
class Student1{
private String name;
private int age;
public Student1() {
}
public Student1(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
public class Demo2{
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//创建学生对象
Student1 s1 = new Student1("王一",21);
Student1 s2 = new Student1("王二",22);
Student1 s3 = new Student1("王三",23);
Student1 s4 = new Student1("王四",24);
//将对象添加到集合中
c.add(s1);
c.add(s2);
c.add(s3);
//指定的元素
System.out.println(c.contains(s1));
Collection c2 = new ArrayList();
c2.add(s3);
c2.add(s4);
//一个集合中的元素
//包含c2中的所有元素才算包含
System.out.println(c.containsAll(c2));
}
}
结果:
true
false
长度功能
int size()
:获取长度
import java.util.ArrayList;
import java.util.Collection;
class Student3{
private String name;
private int age;
public Student3() {
}
public Student3(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
public class Demo3{
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//创建学生对象
Student3 s1 = new Student3("王一",21);
Student3 s2 = new Student3("王二",22);
Student3 s3 = new Student3("王三",23);
Student3 s4 = new Student3("王四",24);
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
//求集合的长度
System.out.println(c.size());
}
}
结果:
4
三.Iterator接口
Iterator iterator()
:表示对集合中的元素进行迭代(遍历)Object next()
:返回迭代的下一个元素(获取元素)
it.next()不要多次使用!只使用一次即可(自定义引用类型),多次使用会创建多个对象boolean hasNext()
:如果仍有元素可以迭代,则返回 true
学生类:
public class Student{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
测试类:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo1 {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//创建学生对象
Student s1 = new Student("王一",21);
Student s2 = new Student("王二",22);
Student s3 = new Student("王三",23);
Student s4 = new Student("王四",24);
//将学生对象加入集合中
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
//迭代器遍历
//获取迭代器对象,多态
Iterator i = c.iterator();
while(i.hasNext()){
//创建了一个对象s
Student s = (Student)i.next();
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
结果:
王一---21
王二---22
王三---23
王四---24