Collection接口
Collection接口
特点:代表一组任意类型的对象,无序、无下标、不能重复
方法
boolean add(Object obj) // 添加一个对象
boolean addAll(Collection c) // 将一个集合中的所有对象添加到此集合中
void clear() //清空此集合中所有对象
boolean contains(Object o) //检查集合中是否包含此对象
boolean containsAll(Collection c) //检查集合中是否包含c集合中所有对象
boolean equals(Object o) //比较此集合与指定对象是否相等
boolean isEmpty() //判断刺激和是否为空
boolean remove(Object c) //在此集合中移除对象o
boolean removeAll(Collection c) //在此集合中移除c集合中也包含的元素
boolean retainAll(Collection c) //仅保留集合与c集合都包含的元素
Iterator<E> iterator() //返回此集合中的元素的迭代器
int size() //返回此集合中的元素个数
Object[] toArray() //将此集合转换成数组
例子
Demo1 String类型集合
package com.qianfeng;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* 测试集合添加删除遍历和判断
*
* @since 2022-05-06
*/
public class CollectionDemo {
public static void main(String[] args) {
Collection collection = new ArrayList<>();
// 添加
collection.add("cat");
collection.add("dog");
collection.add("cat");
collection.add("dog");
System.out.println(collection);
// 移除
collection.remove("dog");
System.out.println(collection);
System.out.println("====================================");
// 增强for循环遍历
for (Object c : collection) {
System.out.println(c);
}
System.out.println("====================================");
// 迭代器循环
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
// 使用迭代器的过程中不允许使用集合的添加和删除操作,可以使用迭代器的删除
iterator.remove();
}
System.out.println(collection);
// 判断是否删除
System.out.println(collection.contains("dog"));
System.out.println(collection.contains("kitte"));
}
}
Demo2 自定义类型集合
package com.qianfeng;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* 自定义类型集合添加、删除、遍历和判断
*
* @since 2022-05-06
*/
public class CollectionDemo2 {
public static void main(String[] args) {
Collection collection = new ArrayList<>();
Student s1 = new Student("ZhangSan", 1);
Student s2 = new Student("LiSi", 2);
Student s3 = new Student("WangWu", 3);
collection.add(s1);
collection.add(s2);
collection.add(s3);
collection.add(s3);
System.out.println(collection.size());
System.out.println(collection);
// 刪除是指从集合里面删除,堆中该数据没有删除
collection.remove(s3);
System.out.println(collection.size());
System.out.println(collection);
// 增强for循环遍历
for (Object c : collection) {
System.out.println(c);
}
// 迭代器遍历
Iterator it = collection.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s);
}
System.out.println(collection.contains(s2));
}
}
class Student {
String name;
Integer age;
public Student() {
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现