1、Java集合框架之Collection
一、集合
(1)概述
- 是对象的容器,定义了多个对象进行操作采用的方法。可实现数组的功能
(2)集合与数组的区别
- 长度方面:数组长度固定,集合长度不固定
- 存储类型:数组可以存储基本类型和引用类型;集合只能存储引用类型(对于基本类型我们可以通过装箱来实现)
(3)位置
- 存放在
java.util.*;
二、Collection体系集合
首先我们先来看一下大体的结构:
由上图可见Collection是list、set集合最基本的接口
(1)Collection父接口
- 特点:代表一组任意类型的对象,无序、无下标、不能重复
- 方法集合:
boolean add(Object obj) //添加一个对象。
boolean addAll(Collection c) //将一个集合中的所有对象添加到此集合中。
void clear() //清空此集合中的所有对象。
boolean contains(Object o) //检查此集合中是否包含o对象。
boolean equals(Object o) //比较此集合是否与指定对象相等。
boolean isEmpty() //判断此集合是否为空。
boolean remove(Object o) //在此集合中移除o对象。
int size() //返回此集合中的元素个数。
Object[] toArray() //将此集合转换成数组。
(2)Collection接口的使用(一)
- 添加元素测试
//首先我们需要创建一个集合
Collection collection=new ArrayList ();
//添加元素测试
collection.add ("javaEE程序设计");
collection.add ("操作系统");
collection.add ("python数据可视化");
System.out.println ("元素个数:"+collection.size ());
System.out.println (collection);
运行测试:
- 删除元素测试
//删除元素测试
collection.remove ("操作系统");
System.out.println ("删除【操作系统】之后集合元素的个数:"+collection.size ());
运行测试:
- 遍历元素测试(增强for)
//1、使用增强for遍历
System.out.println ("使用增强for进行遍历~~~");
for(Object object:collection){
System.out.println (object);
}
运行测试:
- 遍历元素测试(迭代器)
System.out.println ("使用迭代器iterator遍历~~~");
Iterator iterator=collection.iterator ();
while (iterator.hasNext ()){
String object=(String)iterator.next ();
System.out.println (object);
}
运行测试:
- iterator的删除操作
//删除操作:
iterator.remove ();
这里不能用Collection里面的方法,会引发异常,并发修改异常
(3)代码汇总
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Collection接口使用(一)
* 1、添加元素
* 2、删除元素
* 3、遍历元素
* 4、判断
* @author LYM
* @date 2021/3/9 23:21
*/
public class Demo1 {
public static void main(String[] args) {
//首先我们需要创建一个集合
Collection collection=new ArrayList ();
//添加元素测试
collection.add ("javaEE程序设计");
collection.add ("操作系统");
collection.add ("python数据可视化");
System.out.println ("元素个数:"+collection.size ());
System.out.println (collection);
System.out.println ("------------------");
//删除元素测试
// collection.remove ("操作系统");
// System.out.println ("删除【操作系统】之后集合元素的个数:"+collection.size ());
//遍历元素
//1、使用增强for遍历
System.out.println ("使用增强for进行遍历~~~");
for(Object object:collection){
System.out.println (object);
}
System.out.println ("------------------");
//2、使用迭代器(迭代器是专门用来遍历集合的一种方式
//iterator有以下三个方法:hasnext();判断是否有下一个元素、next();获取下一个元素、remove();删除当前元素
System.out.println ("使用迭代器iterator遍历~~~");
Iterator iterator=collection.iterator ();
while (iterator.hasNext ()){
String object=(String)iterator.next ();
//删除操作:
// iterator.remove ();
System.out.println (object);
}
System.out.println (collection.contains ("操作系统"));
System.out.println (collection.isEmpty ());
}
}
(4)Collection使用二
- 新建一个学生类Student
/**
* @author LYM
* @date 2021/3/10 12:47
*/
public class Student {
private String name;
private int age;
public Student(String name, int age) {
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
- 添加元素测试
Collection collection=new ArrayList ();
Student s1=new Student ("张三",19);
Student s2=new Student ("李四",20);
Student s3=new Student ("李白",18);
//1、添加数据
collection.add (s1);
collection.add (s2);
collection.add (s3);
System.out.println ("元素个数:"+collection.size ());
System.out.println (collection.toString ());
运行测试:
- 删除测试
System.out.println ("------删除测试-------");
collection.remove (s1);
System.out.println ("删除之后元素个数:"+collection.size ());
运行测试:
- 遍历数据
- 增强for
System.out.println ("------遍历数据-------");
//增强for测试
for(Object o:collection){
Student result=(Student) o;
System.out.println (result.toString ());
}
运行测试:
- 迭代器遍历集合
System.out.println ("迭代器iterator~~~");
Iterator iterator=collection.iterator ();
while (iterator.hasNext ()){
Student result=(Student) iterator.next ();
System.out.println (result.toString ());
}
- 判断
//判断
System.out.println (collection.contains (s2));
System.out.println (collection.isEmpty ());
- 代码整合
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Collection接口的使用(二)
* 1、添加元素
* 2、删除元素
* 3、遍历元素
* 4、判断
* @author LYM
* @date 2021/3/10 12:47
*/
public class Demo2 {
public static void main(String[] args) {
Collection collection=new ArrayList ();
Student s1=new Student ("张三",19);
Student s2=new Student ("李四",20);
Student s3=new Student ("李白",18);
//1、添加数据
collection.add (s1);
collection.add (s2);
collection.add (s3);
System.out.println ("元素个数:"+collection.size ());
System.out.println (collection.toString ());
System.out.println ("------删除测试-------");
collection.remove (s1);
System.out.println ("删除之后元素个数:"+collection.size ());
System.out.println ("------遍历数据-------");
//增强for测试
System.out.println ("增强for测试~~~");
for(Object o:collection){
Student result=(Student) o;
System.out.println (result.toString ());
}
System.out.println ("迭代器iterator~~~");
Iterator iterator=collection.iterator ();
while (iterator.hasNext ()){
Student result=(Student) iterator.next ();
System.out.println (result.toString ());
}
//判断
System.out.println (collection.contains (s2));
System.out.println (collection.isEmpty ());
}
}
最后就是个人感觉iterator这个迭代器真的很重要,在项目中或者算法题目中应该也会经常看到♥