Java集合框架(一)Collection体系
Java集合框架(一)
目录
什么是集合
集合:对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能。
集合和数组的区别
- 数组长度固定,集合长度不固定
- 数组可以存储基本类型和引用类型,集合只能存储引用类型
集合的位置
java.util.*
Collection体系
- Collection:是根接口,代表一组对象,成为”集合“
- Collection子接口:List和Set
List
- 特点:有序、有下标、元素可以重复
- ArrayList、LinkedList、Vector继承List
- ArrayList、LinkedList、Vector这些类实现了Collection接口的方法(实现类)
Set
- 特点:无序、无下标、元素不能重复
- HashSet、SortedSet<---TreeSet
- HashSet、TreeSet这些类实现了Collection接口的方法(实现类)
Collection接口使用
-
Collection特点:
-
代表一组任意类型的对象
-
整体可以看作无序、无下标、不能重复(但实际上有部分有序、有下标、元素可以重复)
-
-
Collection的方法:
- boolean add(0bject obj)//添加一个对象。
- boolean addAll(Collection c)//将一个集合中的所有对象添加到此集合中。
- void clear()//清空此集合中的所有对象。
- boolean contains(Object o)//检查此集合中是否包含o对象
- boolean equals(Object o) //比较此集合是否与指定对象相等。(比较的是地址,看举例3)
- boolean isEmpty()//判断此集合是否为空
- boolean remove(Object o)//在此集合中移除o对象
- int size()//返回此集合中的元素个数。
- Object[] toArray()//将此集合转换成数组。
-
输出元素的方式:foreach和迭代器两种(举例1)
-
通过实现类去实现接口的这些方法,比如实现类ArrayList可以实现Collection接口的方法
-
举例1:
public class Demo01 { public static void main(String[] args) { Collection collection = new ArrayList();//ArrayList实现类实现的是接口Collection的方法 //添加 collection.add("苹果"); collection.add("西瓜"); collection.add("榴莲"); collection.add("葡萄"); //打印 System.out.println("元素个数:"+collection.size()); System.out.println(collection); //删除 collection.remove("榴莲"); System.out.println("删除之后元素个数:"+collection.size()); System.out.println(collection); //遍历 //方式一,for System.out.println("------遍历1-------"); for (Object o : collection) { System.out.println(o); } //方式二:迭代器(专门用来遍历集合的一种方式) //用到Iterator接口 System.out.println("---------遍历2-----"); Iterator it = collection.iterator(); //hasNext()有没有下一个元素 //next()获取下一个元素 //remove()删除当前元素 //在迭代的过程当中是不允许使用collection.remove()的删除方法的 //在迭代的过程当中可以使用it.remove()迭代器的删除方法的 while(it.hasNext()){ String s = (String)it.next(); System.out.println(s); } System.out.println("-------判断--------"); //判断 System.out.println("判断有没有西瓜:"); System.out.println(collection.contains("西瓜")); System.out.println("判断是否为空:"); System.out.println(collection.isEmpty()); } } //输出: 元素个数:4 [苹果, 西瓜, 榴莲, 葡萄] 删除之后元素个数:3 [苹果, 西瓜, 葡萄] ------遍历1------- 苹果 西瓜 葡萄 ---------遍历2----- 苹果 西瓜 葡萄 -------判断-------- 判断有没有西瓜: true 判断是否为空: false
-
举例2:
学生类:
public class Student { private String name; private int age; public Student() { } 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; } //重写toString方法 public String toString(){ return this.name+" "+this.age; } }
主函数:
public class Demo02 { public static void main(String[] args) { //新建Collection对象 Collection collection = new ArrayList(); Collection collection2 = new ArrayList(); Collection collection3 = new ArrayList(); Student s1 = new Student("张三",18); Student s2 = new Student("李四",16); Student s3 = new Student("王五",23); collection.add(s1); collection.add(s2); collection.add(s3); collection2.addAll(collection);//将collection所有元素赋值到collection2上 collection3.addAll(collection); //在collection里删除s1元素 collection.remove(s1); //在collection3里删除s2元素 collection3.remove(s2); System.out.println("collection元素个数:"+collection.size()); System.out.println(collection.toString()); System.out.println("collection2元素个数:"+collection2.size()); System.out.println(collection2.toString()); System.out.println("collection3元素个数:"+collection3.size()); System.out.println(collection3.toString()); //在collection里删除和collection3里的元素重合的元素 System.out.println("在collection里删除和collection3里的元素重合的元素:"); collection.removeAll(collection3); System.out.println("collection元素个数:"+collection.size()); System.out.println(collection.toString()); } } //输出: collection元素个数:2 [李四 16, 王五 23] collection2元素个数:3 [张三 18, 李四 16, 王五 23] collection3元素个数:2 [张三 18, 王五 23] 在collection里删除和collection3里的元素重合的元素: collection元素个数:1 [李四 16]
-
举例3:
学生类:
public class Student { private String name; private int age; public Student() { } 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; } //重写toString方法 public String toString(){ return this.name+" "+this.age; } }
主函数:
public class Demo03 { public static void main(String[] args) { //新建Collection对象 Collection collection = new ArrayList(); Collection collection2 = new ArrayList(); Collection collection3 = new ArrayList(); Student s1 = new Student("张三",18); Student s2 = new Student("李四",16); Student s3 = new Student("王五",23); Student s4 = new Student("张三",18); Student s5 = new Student("李四",16); Student s6 = new Student("王五",23); collection.add(s1); collection.add(s2); collection.add(s3); // collection2.add(s4); collection2.add(s5); collection2.add(s6); // collection3.add(s1); collection3.add(s2); collection3.add(s3); //查看各个元素 System.out.println("collection元素个数:"+collection.size()); System.out.println(collection.toString()); System.out.println("collection2元素个数:"+collection2.size()); System.out.println(collection2.toString()); System.out.println("collection3元素个数:"+collection3.size()); System.out.println(collection3.toString()); //比较1 System.out.println("collection和collection3比较:"); System.out.println(collection.equals(collection3)); //比较2 System.out.println("collection和collection2比较:"); System.out.println(collection.equals(collection2)); } }
List子接口
-
特点:有序、有下标、元素可以重复
-
因为有序所以有下标所以List子接口中的方法有很多需要”下标“这个参数
-
方法:
-
包含Collection的方法,但有的方法有些不同
-
与Collection不同或独有的方法:
- void add(int index,Object o) //在index(下标)位置插入对象o
- boolean addAll(int index,Collection c) //将一个集合中的元素添加到此集合中的index位置
- Object get(int index) //返回集合中指定位置的元素
- indexOf(元素内容) //获取元素内容的下标位置,显示-1表示没有
- List subList(int fromIndex,int toIndex) //返回下标fromIndex和下标toIndex之间的集合元素(左闭右开区间)
-
输出方法:
-
for循环
-
foreach循环
-
迭代器(Iterator)
-
列表迭代器(ListIterator):列表迭代器支持任意方向进行遍历
-
逆向遍历:hasPrevious()判断,previous()输出元素
(注:由于默认指针指向第一个元素所以如果想要实现倒序要先将指针指向最后一个元素)
-
正向遍历:hasNext()判断,next()输出元素
-
替换元素:set(E,e)用指定元素替换next或previous返回的最后一个(或指定元素)元素,或指定下标进行替换
-
删除元素:remove()用删除next或previous返回的最后一个(或指定元素)元素,或指定下标进行删除
-
-
-
-
举例:
学生类:
public class Student { private String name; private int age; public Student() { } 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; } //重写toString方法 public String toString(){ return this.name+" "+this.age; } }
主函数:
public class Demo04 { public static void main(String[] args) { List list = new ArrayList();//指向实现类ArrayList List list2 = new ArrayList(); Student s1 = new Student("张三",18); Student s2 = new Student("李四",16); Student s3 = new Student("王五",23); Student s4 = new Student("赵六",15); list.add(s1); list.add(s2); list.add(s3); list.add(s4); //输出全部元素 System.out.println("--------输出全部元素--------"); System.out.println(list); //输出指定下标的元素 System.out.println("--------输出指定下标为1的元素--------"); System.out.println(list.get(1)); System.out.println("-----使用subList方法(左闭右开)-------"); //使用subList方法(左闭右开) System.out.println(list.subList(1,3)); System.out.println("--------使用foreach输出全部元素--------"); for (Object o : list) { Student l1 = new Student(); l1 = (Student) o; System.out.println(l1); } //使用叠加器输出全部元素 System.out.println("--------使用叠加器输出全部元素--------"); Iterator iterator = list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } //使用列表叠加器输出全部元素 System.out.println("--------使用列表叠加器输出全部元素--------"); ListIterator listIterator = list.listIterator(); //逆向输出 //由于指针默认在下标为0,所以通过next运行一遍将指针指向最后 while (listIterator.hasNext()){ listIterator.next(); } System.out.println("---------逆向输出---------"); while (listIterator.hasPrevious()){ System.out.println("下标:"+listIterator.previousIndex()+" : 内容:"+listIterator.previous()); } //逆向输出完之后指针又回到了下标0 System.out.println("---------正向输出---------"); while (listIterator.hasNext()){ System.out.println("下标:"+listIterator.nextIndex()+" : 内容:"+listIterator.next()); } //正向输出完之后指针又指向了最后一个元素 //输出s3所在下标 System.out.println("---------输出s3所在下标---------"); System.out.println(list.indexOf(s3)); //删除指定元素 System.out.println("----删除下标为2的元素----"); list.remove(2); System.out.println("------普通输出------"); System.out.println(list); System.out.println("------正向输出------"); ListIterator listIterator2 = list.listIterator(); while (listIterator2.hasNext()){ System.out.println("下标:"+listIterator2.nextIndex()+" : 内容:"+listIterator2.next()); } } } //输出: --------输出全部元素-------- [张三 18, 李四 16, 王五 23, 赵六 15] --------输出指定下标为1的元素-------- 李四 16 -----使用subList方法(左闭右开)------- [李四 16, 王五 23] --------使用foreach输出全部元素-------- 张三 18 李四 16 王五 23 赵六 15 --------使用叠加器输出全部元素-------- 张三 18 李四 16 王五 23 赵六 15 --------使用列表叠加器输出全部元素-------- ---------逆向输出--------- 下标:3 : 内容:赵六 15 下标:2 : 内容:王五 23 下标:1 : 内容:李四 16 下标:0 : 内容:张三 18 ---------正向输出--------- 下标:0 : 内容:张三 18 下标:1 : 内容:李四 16 下标:2 : 内容:王五 23 下标:3 : 内容:赵六 15 ---------输出s3所在下标--------- 2 ----删除下标为2的元素---- ------普通输出------ [张三 18, 李四 16, 赵六 15] ------正向输出------ 下标:0 : 内容:张三 18 下标:1 : 内容:李四 16 下标:2 : 内容:赵六 15
List的实现类
ArrayList
-
特点:
- 数据结构实现,查询快、增删慢
- JDK1.2版本,运行效率快、线程不安全
-
举例:
学生类:
public class Student { private String name; private int age; public Student() { } 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; } //重写toString方法 public String toString(){ return this.name+" "+this.age; } }
主函数:
public class Demo05 { public static void main(String[] args) { ArrayList arrayList = new ArrayList();//初始容量为0 Student s1 = new Student("张三",18);//每增加数据,使容量加一 Student s2 = new Student("李四",19); Student s3 = new Student("王五",20); Student s4 = new Student("赵六",21); //添加元素 arrayList.add(s1); arrayList.add(s2); arrayList.add(s3); arrayList.add(s4); //输出 System.out.println("-----输出当前元素-------"); System.out.println(arrayList.toString()); //删除s3 arrayList.remove(s3); System.out.println("-----输出当前元素-------"); System.out.println(arrayList.toString()); System.out.println("-----判断是否有s3-------"); System.out.println(arrayList.contains(s3)); System.out.println("-----判断是否有s2-------"); System.out.println(arrayList.contains(s2)); System.out.println("-----查找s2的下标-------"); System.out.println(arrayList.indexOf(s2)); } } //输出: -----输出当前元素------- [张三 18, 李四 19, 王五 20, 赵六 21] -----输出当前元素------- [张三 18, 李四 19, 赵六 21] -----判断是否有s3------- false -----判断是否有s2------- true -----查找s2的下标------- 1
Vector
- 特点:
- 数据结构实现,查询快、增删慢
- JDK1.0版本,运行效率慢、线程安全
- 部分方法:
- firstElement() //获取第一个元素
- lastElement() //获取最后一个元素
- elementAt(下标) //获取指定下标的元素
LinkedList
-
特点:
- 链表结构实现,增删快,查询慢
- 内部有双向链表
-
举例:
学生类:
public class Student { private String name; private int age; public Student() { } 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; } //重写toString方法 public String toString(){ return this.name+" "+this.age; } }
主函数:
public class Demo06 { public static void main(String[] args) { LinkedList linkedList= new LinkedList(); Student s1 = new Student("张三",18); Student s2 = new Student("李四",19); Student s3 = new Student("王五",20); Student s4 = new Student("赵六",21); //添加元素 linkedList.add(s1); linkedList.add(s2); linkedList.add(s3); linkedList.add(s4); System.out.println("-----输出当前元素个数-------"); System.out.println(linkedList.size()); System.out.println("-----输出当前元素-------"); System.out.println(linkedList.toString()); System.out.println("-----删除s3元素-------"); linkedList.remove(s3); System.out.println("-----foreach输出元素-------"); for (Object o : linkedList) { Student l1 = (Student) o; System.out.println(l1); } System.out.println("-----迭代器输出元素-------"); Iterator iterator = linkedList.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } //判断 System.out.println("-----是否存在s1-------"); System.out.println(linkedList.contains(s1)); System.out.println("-----是否为空-------"); System.out.println(linkedList.isEmpty()); System.out.println("-----获取s1的位置-------"); System.out.println(linkedList.indexOf(s1)); } } //输出: -----输出当前元素个数------- 4 -----输出当前元素------- [张三 18, 李四 19, 王五 20, 赵六 21] -----删除s3元素------- -----foreach输出元素------- 张三 18 李四 19 赵六 21 -----迭代器输出元素------- 张三 18 李四 19 赵六 21 -----是否存在s1------- true -----是否为空------- false -----获取s1的位置------- 0
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现