集合—collection、iterator遍历集合
2024-10-20 01:29:29
一、collection接口
1.collection常用方法
点击查看代码
@Test
public void test(){
//contains()
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(false);
Person p = new Person("jerry", 23);
coll.add(p);
coll.add(new Person("twq", 23));
//1.contains(Object obj):判断当前集合中是否包含obj
//我们在判断时会调用obj对象所在类的equals()方法
boolean contains = coll.contains(123);
System.out.println(contains);
System.out.println(coll.contains(new String("Tom")));//判断的是内容而不是地址
System.out.println(coll.contains(p));
System.out.println(coll.contains(new Person("twq", 23)));
System.out.println("===================");
//2.containsAll(Collection call1):判断形参coll1中的所有元素是否都存在与当前集合中
Collection coll1 = Arrays.asList(123,456);
System.out.println(coll.containsAll(coll1));//判断coll中是否包含coll1
System.out.println("===================");
//3.remove(Object obj):从当前集合中移除obj元素
coll.remove(1234);
System.out.println(coll);
coll.remove(new Person("twq", 23));
System.out.println(coll);
System.out.println("===================");
//4.removeAll(Collection coll1):从当前集合中移除coll1中的所有的元素
Collection coll2 = Arrays.asList(123,456);
coll.removeAll(coll2);
System.out.println(coll);
System.out.println("===================");
//5.retainAll(collection coll1) 交集:获取当前集合和coll1集合的交集
Collection coll3 = Arrays.asList(false,"Tom");
coll.retainAll(coll3);
System.out.println(coll);
System.out.println("===================");
//6.equals(Object obj):要想返回true需要判断当前集合和形参集合的元素都相同
Collection coll4 = new ArrayList();
coll4.add(123);
coll4.add(456);
coll4.add(new String("Tom"));
coll4.add(false);
Collection coll5 = new ArrayList();
coll5.add(123);
coll5.add(456);
coll5.add(new String("Tom"));
coll5.add(false);
System.out.println(coll4.equals(coll5));
System.out.println("==================");
//7.hashCode():返回当前对象的哈希值
System.out.println(coll.hashCode());
System.out.println("==================");
//8.集合--->数组的转换:toArray()
Object[] arr = coll.toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("==================");
//拓展:数组--->集合:调用Arrays类的静态方法asList()
List<String> list = Arrays.asList(new String[]{"AA", "DD", "CC"});
System.out.println(list);
List<int[]> ints = Arrays.asList(new int[]{123, 456});
System.out.println(ints.size());//虽然有两个元素,但是会默认为一个元素
List ints1 = Arrays.asList(new Integer[]{123, 456});
System.out.println(ints1.size());//这里可以正常输出
System.out.println("==================");
}
运行结果图
二、iterator集合元素遍历
1.遍历方法
点击查看代码
public void test1(){
//iterator():返回
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(false);
Iterator iterator = coll.iterator();
while(iterator.hasNext()){//判断集合中是否还有元素
System.out.println(iterator.next());
}
}
运行结果图:
2.iterator中remove方法的使用
点击查看代码
@Test
public void test2(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(false);
Iterator iterator = coll.iterator();
while(iterator.hasNext()){
Object obj = iterator.next();
if(obj.equals("Tom")){
iterator.remove();
}
}
//这里必须重新定义一个迭代器变量,之前的迭代器变量已经指向了集合的末尾
//新定义的迭代器变量指针默认指向集合的初始位置
Iterator iterator1 = coll.iterator();
while(iterator1.hasNext()){//判断集合中是否还有元素
System.out.println(iterator1.next());
}
}
运行结果图
最新文章
- [SQL Server] 特殊字符、上标、下标处理
- 数据结构图文解析之:哈夫曼树与哈夫曼编码详解及C++模板实现
- php 读取输出其他文件的方法
- iOS AVCaptureSession 小视频开发总结,支持设备旋转
- java 27 - 4 反射之 通过反射获取成员变量并使用
- 求sqrt()底层效率问题(二分/牛顿迭代)
- NoSQL聚合数据模型
- oracle 索引失效原因
- 创建Mysql
- xml to json
- Codeforces Round #245 (Div. 1)——Working out
- windows文件快速搜索软件推荐
- 58. Length of Last Word【leetcode】
- TCP服务端开发为例--web开发不同url请求走不同control方法
- 在";";中添加";
- bootstap初识之css
- Linux代理搭建TinyProxy
- C++_day9am
- ABP框架系列之三十五:(MVC-Controllers-MVC控制器)
- Category 的一些事
热门文章
- BootstrapBlazor实战 Menu 导航菜单使用(1)
- 从0开始基于Webpack5 搭建HTML+Less 前端工程
- 从零开始学YC-Framework之初步
- 网页数字递增——jquery.countTo.js
- ThinkPHP信息泄露
- .NET性能优化-使用ValueStringBuilder拼接字符串
- Blazor和Vue对比学习(基础1.8):Blazor中实现计算属性和数据监听
- 2022管家婆工贸版ERP T3 V22.0工厂管理软件单机网络版无限用户免狗软件可定制
- 使用python脚本+zabbix前端监控云联网底层TCP数据流所负载的链路质量,并在丢包时联动保存MTR记录
- Spring 源码(14)Spring Bean 的创建过程(6)对象的提前暴露