一、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());
}
}

运行结果图

最新文章

  1. [SQL Server] 特殊字符、上标、下标处理
  2. 数据结构图文解析之:哈夫曼树与哈夫曼编码详解及C++模板实现
  3. php 读取输出其他文件的方法
  4. iOS AVCaptureSession 小视频开发总结,支持设备旋转
  5. java 27 - 4 反射之 通过反射获取成员变量并使用
  6. 求sqrt()底层效率问题(二分/牛顿迭代)
  7. NoSQL聚合数据模型
  8. oracle 索引失效原因
  9. 创建Mysql
  10. xml to json
  11. Codeforces Round #245 (Div. 1)——Working out
  12. windows文件快速搜索软件推荐
  13. 58. Length of Last Word【leetcode】
  14. TCP服务端开发为例--web开发不同url请求走不同control方法
  15. 在&quot;&quot;中添加&quot;
  16. bootstap初识之css
  17. Linux代理搭建TinyProxy
  18. C++_day9am
  19. ABP框架系列之三十五:(MVC-Controllers-MVC控制器)
  20. Category 的一些事

热门文章

  1. BootstrapBlazor实战 Menu 导航菜单使用(1)
  2. 从0开始基于Webpack5 搭建HTML+Less 前端工程
  3. 从零开始学YC-Framework之初步
  4. 网页数字递增——jquery.countTo.js
  5. ThinkPHP信息泄露
  6. .NET性能优化-使用ValueStringBuilder拼接字符串
  7. Blazor和Vue对比学习(基础1.8):Blazor中实现计算属性和数据监听
  8. 2022管家婆工贸版ERP T3 V22.0工厂管理软件单机网络版无限用户免狗软件可定制
  9. 使用python脚本+zabbix前端监控云联网底层TCP数据流所负载的链路质量,并在丢包时联动保存MTR记录
  10. Spring 源码(14)Spring Bean 的创建过程(6)对象的提前暴露