Collection集合概述
- Java数组的长度是固定的,为了使程序能够方便地存储和操作数目不固定的一组数据,JDK类库提供了Java集合
- 与数组不同的是,集合中不能存放基本类型数据,而只能存放对象的引用。
- 数组只能存储同种数据类型的元素 ,集合可以存储不同类型的元素
集合框架的介绍


Collection集合的常用功能
java.utiL.Collection接口
- 所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法
- 任意的单列集合都可以使用Collection接口中的方法
Collection接口的共性方法
复制 | public boolean add(E e) 把给定的对象添加到当前集合中。 |
| public void clear() 清空集合中所有的元素。 |
| public boolean remove(E e) 把给定的对象在当前集合中册除。 |
| public boolean contains(E e) 判断当前集合中是否包合给定的对象。 |
| public boolean isEmpty() 判断当前集合是否为空。 |
| public int size() 返回集合中元素的个数。 |
| public Object[] toArray() 把集合中的元素,存储到数组中。 |
add()方法
作用:把给定的对象添加到当前集合中。
复制 | import java.util.Collection; |
| import java.util.ArrayList; |
| |
| public class DemoCollectionAdd { |
| public static void main(String[] args) { |
| |
| Collection<String> collection = new ArrayList<>(); |
| |
| |
| System.out.println("没有进行任何操作的ArrayList集合对象:" + collection); |
| |
| |
| boolean addReturn = collection.add("LeeHua"); |
| System.out.println("往集合中添加一个元素后的返回值:" + addReturn); |
| System.out.println("使用add方法往集合里面添加了元素后:" + collection); |
| } |
| } |
复制 | 输出结果: |
| 没有进行任何操作的ArrayList集合对象:[] |
| 往集合中添加一个元素后的返回值:true |
| 使用add方法往集合里面添加了元素后:[LeeHua] |
remove方法
作用:把给定的对象在当前集合中册除。
复制 | import java.util.Collection; |
| import java.util.ArrayList; |
| |
| public class DemoCollectionRemove { |
| public static void main(String[] args) { |
| |
| Collection<String> collection = new ArrayList<>(); |
| System.out.println("往集合中添加元素前的集合是:" + collection); |
| |
| |
| collection.add("一号"); |
| collection.add("二号"); |
| collection.add("三号"); |
| collection.add("四号"); |
| System.out.println("往集合中添加元素后的集合是:" + collection); |
| |
| |
| |
| |
| boolean removeReturn1 = collection.remove("一号"); |
| System.out.println("删除元素\"一号\"的返回值:" + removeReturn1); |
| System.out.println("删除元素\"一号\"后的集合是:" + collection); |
| |
| boolean removeReturn2 = collection.remove("十号"); |
| System.out.println("删除元素\"十号\"的返回值:" + removeReturn2); |
| System.out.println("删除元素\"十号\"后的集合是:" + collection); |
| } |
| } |
复制 | 输出结果: |
| 往集合中添加元素前的集合是:[] |
| 往集合中添加元素后的集合是:[一号, 二号, 三号, 四号] |
| 删除元素"一号"的返回值:true |
| 删除元素"一号"后的集合是:[二号, 三号, 四号] |
| 删除元素"十号"的返回值:false |
| 删除元素"十号"后的集合是:[二号, 三号, 四号] |
contains方法
作用:判断当前集合中是否包合给定的对象。
复制 | import java.util.ArrayList; |
| import java.util.Collection; |
| |
| public class DemoCollectionContains { |
| public static void main(String[] args) { |
| Collection<String> collection = new ArrayList<>(); |
| |
| |
| collection.add("对象1"); |
| collection.add("对象2"); |
| collection.add("对象3"); |
| collection.add("对象4"); |
| System.out.println("集合:" + collection); |
| |
| |
| |
| |
| boolean containsReturn1 = collection.constains("对象100"); |
| System.out.println("是否包含\"对象100\":" + containsReturn1); |
| |
| boolean containsReturn2 = collection.constains("对象1"); |
| System.out.println("是否包含\"对象1\":" + containsReturn2); |
| } |
| } |
复制 | 输出结果: |
| 集合:[对象1, 对象2, 对象3, 对象4] |
| 是否包含"对象100":false |
| 是否包含"对象1":true |
isEmpty方法
作用:判断当前集合是否为空。
复制 | import java.util.ArrayList; |
| import java.util.Collection; |
| |
| public class DemoCollectionIsEmpty { |
| public static void main(String[] args) { |
| |
| Collection<String> collection = new ArrayList<>(); |
| |
| |
| boolean isEmptyReturn1 = collection.isEmpty(); |
| System.out.println("集合是否为空:" + isEmptyReturn1); |
| |
| |
| collection.add("一号元素"); |
| |
| boolean isEmptyReturn2 = collection.isEmpty(); |
| System.out.println("集合是否为空:" + isEmptyReturn2); |
| } |
| } |
复制 | 输出结果: |
| 集合是否为空:true |
| 集合是否为空:false |
size方法
作用:返回集合中元素的个数。
复制 | import java.util.ArrayList; |
| import java.util.Collection; |
| |
| public class DemoCollectionSize { |
| public static void main(String[] args) { |
| |
| Collection<String> collection = new ArrayList<>(); |
| |
| |
| int collectionSize1 = collection.size(); |
| System.out.println("collectionSize1 = " + collectionSize1); |
| |
| |
| collection.add("一号元素"); |
| collection.add("二号元素"); |
| collection.add("三号元素"); |
| collection.add("四号元素"); |
| collection.add("五号元素"); |
| |
| |
| int collectionSize2 = collection.size(); |
| System.out.println("collectionSize2 = " + collectionSize2); |
| } |
| } |
复制 | 输出结果: |
| collectionSize1 = 0 |
| collectionSize2 = 5 |
toArray方法
作用:把集合中的元素,存储到数组中。
复制 | public class DemoCollectionToArray { |
| public static void main(String[] args) { |
| |
| Collection<String> collection = new ArrayList<>(); |
| |
| |
| collection.add("一号元素"); |
| collection.add("二号元素"); |
| collection.add("三号元素"); |
| collection.add("四号元素"); |
| collection.add("五号元素"); |
| |
| |
| Object[] collectionToArray = collection.toArray(); |
| |
| |
| for (int i = 0; i < collectionToArray.length; i++) { |
| System.out.println(collectionToArray[i]); |
| } |
| } |
| } |
复制 | 输出结果: |
| 一号元素 |
| 二号元素 |
| 三号元素 |
| 四号元素 |
| 五号元素 |
clear方法
作用:清空集合中的所用元素
复制 | import java.util.ArrayList; |
| import java.util.Collection; |
| |
| public class DemoCollectionClear { |
| public static void main(String[] args) { |
| |
| Collection<String> collection = new ArrayList<>(); |
| |
| |
| collection.add("一号元素"); |
| collection.add("二号元素"); |
| collection.add("三号元素"); |
| collection.add("四号元素"); |
| collection.add("五号元素"); |
| System.out.println("清空集合元素之前:" + collection); |
| |
| |
| collection.clear(); |
| System.out.println("清空集合元素之后:" + collection); |
| } |
| } |
复制 | 输出结果: |
| 清空集合元素之前:[一号元素, 二号元素, 三号元素, 四号元素, 五号元素] |
| 清空集合元素之后:[] |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)