集合框架(1):Collection | Iterator | 增强for

2023-05-16

文章目录

    • Collection接口
      • 一、集合框架的概述
        • Collection接口继承树
        • Collection接口中的方法的使用
        • 使用迭代器Iterator遍历Collection集合
        • 增强for循环遍历集合


文章链接
Java语法https://blog.csdn.net/weixin_45606067/article/details/107049186
一维数组与二维数组、内存解析https://blog.csdn.net/weixin_45606067/article/details/107049178
面向对象(1/3)类和对象https://blog.csdn.net/weixin_45606067/article/details/108234276
面向对象(2/3)封装性、继承性、多态性https://blog.csdn.net/weixin_45606067/article/details/108234328
面向对象(3/3)抽象类、接口、内部类、代码块https://blog.csdn.net/weixin_45606067/article/details/108258152
异常处理待更新
多线程(1/2)https://blog.csdn.net/weixin_45606067/article/details/107067785
多线程(2/2)https://blog.csdn.net/weixin_45606067/article/details/107067857
常用类https://blog.csdn.net/weixin_45606067/article/details/108283203
枚举与注解待更新
集合(1/5)Collection、Iterator、增强forhttps://blog.csdn.net/weixin_45606067/article/details/107046876
集合(2/5)List、ArrayList、LinkedList、Vector的底层源码https://blog.csdn.net/weixin_45606067/article/details/107069742
集合(3/5)set、HashSet、LinkedHashSet、TreeSet的底层源码
集合(4/5)Map、HashMap底层原理分析https://blog.csdn.net/weixin_45606067/article/details/107042949
集合(5/5)LinkHashMap、TreeMap、Properties、Collections工具类https://blog.csdn.net/weixin_45606067/article/details/107069691
泛型与Filehttps://blog.csdn.net/weixin_45606067/article/details/107124099
IO流与网络编程https://blog.csdn.net/weixin_45606067/article/details/107143670
反射机制待更新
Java8新特性https://blog.csdn.net/weixin_45606067/article/details/107280823
Java9/10/11新特性待更新

Collection接口

一、集合框架的概述

1.集合、数组都是对多个数据进行存储操作的结构,简称Java容器。
说明:此时的存储,主要指的是内存层面的存储,不涉及持久化存储(.txt,.jpg,.avi,数据库中)

2.数组在存储多个数据方面的特点:

  • 一旦初始化以后,其长度就确定了。
  • 数组一旦定义好,其元素类型就确定了,我们也就只能操作指定类型的数据。
    比如:String[] arr; int[] arr1;Object[] arr2;

数组在存取数据方面的缺点:

  • 一旦初始化以后,其长度不可修改。
  • 数组中提供的方法非常有限,对于删除、插入数据非常不方便,效率也不高。
  • 获取数组中实际元素的个数,数组并没有提供现成的方法。
  • 数组存储数据的特点:有序,可重复。

Collection接口继承树

在这里插入图片描述

Collection接口中的方法的使用

  • add(Object e):将元素e添加到集合coll中
  • size():获取添加的元素个数
  • addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中
  • clear():清空集合元素
  • isEmpty():判断当前集合是否为空

代码实现:

@Test
public void test1(){
  Collection coll = new ArrayList();
  //1.add(Object e)
  coll.add("AA");
  coll.add("BB");
  coll.add(123);//自动装箱
  coll.add(new Date());

  //2.size()
  System.out.println(coll.size());//4

  //3.addAll(Collection coll1)
  Collection coll1 = new ArrayList();
  coll1.add(456);
  coll1.add("CC");
  coll.addAll(coll1);
  System.out.println(coll.size());//6
  System.out.println(coll);//显示集合中元素,调用toString()方法

  //4.clear()
  coll.clear();
  
  //5.isEmpty()
  System.out.println(coll.isEmpty());
}
  • contains(Object obj):判断当前集合是否包含obj,在判断时会调用obj对象所在类的equal()。
  • containAll(Collection coll1):判断形参coll1中的元素是否都存在于当前集合中。
  • remove(Object obj):从当前集合中移除obj元素。返回true/false。
  • removeAll(Collection coll2):从当前集合中移除coll2中所有的元素(公共的部分)。
  • retainAll(Collection coll3):获取当前集合和coll3集合的交集,并返回给当前集合。
  • equals(Object obj):要返回true,需要当前集合与形参集合的元素都相同。
/*
    结论:
    向Collection接口的实现类的对象中添加数据obj时,要求obj所在类重写equals()方法
     */
    @Test
    public void test2(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(false);
//        Person p = new Person("Jerry",20);
//        coll.add(p);
        coll.add(new Person("Jerry",20));

        //6.contains(Object obj)
        boolean contains = coll.contains(123);
        System.out.println(contains);//true
        System.out.println(coll.contains(new String("Tom")));//true
//        System.out.println(coll.contains(p));//true
        System.out.println(coll.contains(new Person("Jerry",20)));//false-->true重写Person的equals方法

        //7.containAll(Collection coll1)
        Collection coll1 = Arrays.asList(123,456);
        System.out.println(coll.containsAll(coll1));

        //8.remove(Object obj)
        coll.remove(123);
        
        //9.removeAll(Collection coll2)
        Collection coll2 = Arrays.asList(123,4567);
        coll.removeAll(coll2);//[123]
        
        //10.retainAll(Collection coll3)
        Collection coll3 = Arrays.asList(123,456,789);
        coll.retainAll(coll3);//[123,456]
        
        //11.equals(Object obj)
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("Tom"));
        coll4.add(false);
        coll4.add(new Person("Jerry",20));
        coll.equals(coll4);
    }
  • hashCode():返回当前对象的哈希值
  • 集合 —> 数组:toArray()
  • 数组 —> 集合:调用Arrays类的静态方法asList()
@Test
public void test3(){
  Collection coll = new ArrayList();
  coll.add(123);
  coll.add(456);
  coll.add(new Person("Jerry",20));
  coll.add(new String("Tom"));
  coll.add(false);

  //hashCode()
  System.out.println(coll.hashCode());

  //集合 ---> 数组
  Object[] arr = coll.toArray();
  for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i]);
  }

  //数组 ---> 集合
  List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
  System.out.println(list);

  List<int[]> list1 = Arrays.asList(new int[]{123, 456});
  System.out.println(list1.size());//1

  List<Integer> list2 = Arrays.asList(123, 456);
  System.out.println(list2.size());//2
}

使用迭代器Iterator遍历Collection集合

1.内部的方法:hasNext() 和 next()
2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。
3.内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove()

注意:如果还未调用next()或在上一次调用next方法之后已经调用了remove 方法,再调用remove都会报ILLegalStateException。

代码实现:

public class IteratorTest {

    @Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);
        Iterator iterator = coll.iterator();
        //方式二:
//        for (int i = 0; i < coll.size(); i++) {
//            System.out.println(iterator.next());
//        }
        //方式三:
        //hashNext():判断是否还有下一个元素
        while (iterator.hasNext()){
            //next():①指针下移 ② 将下移以后集合位置上的元素返回
            System.out.println(iterator.next());
        }

        //测试iterator的remove()方法
        while (iterator.hasNext()){
            Object obj = iterator.next();
            if ("Tom".equals(obj)){
                iterator.remove();
            }
        }
    }
}

迭代器的执行原理
在这里插入图片描述

增强for循环遍历集合

JDK5.0 新增了foreach循环,用于遍历集合、数组

定义格式:
for(集合中元素的类型 局部变量 : 集合对象){
sout(局部变量);
}

说明:内部仍然调用了迭代器
把集合中的每个值一次一次赋值给Object类型的变量然后输出

代码实现:

public class forTest {
    @Test
    public void test1() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry", 20));
        coll.add(new String("Tom"));
        coll.add(false);

        for (Object o : coll) {
            System.out.println(o);
        }
    }
}


如果有收获!!! 希望老铁们来个三连,点赞、收藏、转发
创作不易,别忘点个赞,可以让更多的人看到这篇文章,顺便鼓励我写出更好的博客
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

集合框架(1):Collection | Iterator | 增强for 的相关文章

随机推荐

  • Ubuntu22.04网卡丢失

    首先确保网卡设备名称能看到 xff0c 看不到 xff0c 以下步骤无意义 xff1a sudo lshw c network 2 命令行执行以下命令 xff1a sudo service NetworkManager stop sudo
  • Ubuntu22.04中ROS2的安装教程

    ROS2入门教程 在ubuntu22 04下apt安装ros2版本humble 创客智造 ncnynl com ROS Humble Ubuntu 22 04 Apt Install Issue ROS 答案 xff1a 开源问答论坛
  • git 使用

    一 安装 Git 1 从 Git 官网下载对应版本 xff0c 进行默认安装即可 2 安装完成后 xff0c 桌面右键点击 Git gt Git Bash xff0c 弹出命令行窗口 3 在命令行窗口输入 34 git config glo
  • Darknet中.cfg文件参数详解

    转载自 xff1a https blog csdn net phinoo article details 83022101 感谢博主分享 net xxx 开始的行表示网络的一层 xff0c 其后的内容为该层的参数配置 xff0c net 为
  • VS的路径变量[转]

    版权声明 xff1a 本文为博主原创文章 xff0c 未经博主允许不得转载 https blog csdn net peter teng article details 9716421 RemoteMachine 设置为 调试 属性页上 远
  • dockerfile定制jenkins+python+pytest+allure镜像步骤

    一 创建dockerfile文件 创建目录 mkdir jenkinsci dockerfile 进入目录 cd jenkinsci dockerfile 创建dockerfile文件 touch dockerfile 二 将依赖的文件或目
  • Mac上用Chrome,全屏后 关闭、缩小、最小化图标不见了,多了一条空白条,解决方法

    问题 xff1a Mac上用Chrome xff0c 全屏后移动鼠标到上方 xff0c 本来会有的关闭 缩小 最小化图标不见了 xff0c 多了一条空白条 解决方法 xff1a 快捷关闭全屏模式 xff1a command 43 contr
  • Linux-----信号量

    信号量 信号量原理信号量概念信号量函数基于环形队列的生产消费模型空间和数据资源生产者和消费者申请 释放信号量模拟实现基于环形队列的生产者消费者模型 信号量原理 之前我们知道被多个执行流同时访问的公共资源叫做临界资源 xff0c 而临界资源不
  • Linux操作系统 §3:基本命令Ⅱ(Bash常用功能,文件查询)

    本篇你将掌握的内容 xff08 文末有总结全图 xff09 xff1a 文章目录 3 0 引入3 1 补全命令 文件名 xff1a Tab键 3 2 查看文件 cat more3 2 1 cat concatenate 3 2 1 1 概念
  • 无人机自主导航 (realsense D430 vins 的安装与调试)

    realsense SDK的安装 https github com IntelRealSense librealsense blob master doc distribution linux md https github com Int
  • 无人机自主导航(ARM架构的vins-fusion-GPU部署)

    本文参考 GitHub arjunskumar vins fusion gpu tx2 nano Installation step of vins fusion gpu version on Nvidia Jetson TX2 amp N
  • TI电赛无人机

    一 材料准备 1 机架 xff08 F330机架便宜耐摔 xff0c 初期调试时使用 xff09 2 电调 xff08 好盈40A电调 xff09 3 电机 xff08 新西达 朗宇 xff09 4 桨叶 xff08 乾丰8045 xff0
  • 【Maven项目如何转换为Gradle项目】

    Idea中Maven工程如何转换为Gradle工程 打开Maven项目 修改settings中Maven的本地仓库 修改settings中项目的编码格式 4 刷新Maven的相应插件 5 在Idea中TerMinal输入 gradle in
  • Git版本控制的使用

    文章目录 一 Git的介绍1 版本控制2 Git与svn对比3 聊聊Git历史 二 Git的使用1 Git环境配置2 Git配置3 Git基本理论 xff08 核心 xff09 4 Git项目搭建5 Git文件操作 三 使用GitHub1
  • eclipse解决中文乱码问题

    eclipse运行页面显示中文乱码 页面源码 lt 64 page language 61 34 java 34 contentType 61 34 text html charset 61 ISO 8859 1 34 pageEncodi
  • YOLOv4:ubuntu18下使用darknet训练自己的模型

    首先 xff0c 如果使用GPU xff0c 确认你电脑的有关环境是否符合以下要求 xff1a CMake gt 61 3 12CUDA gt 61 10 0OpenCV gt 61 2 4cuDNN gt 61 7 0GPU with C
  • Java 基础常见面试题大全

    原因 焦虑 每次去面试更高的职位时候 xff0c 内心总是担忧着那些面试题怎么解答 很多问题在实际工作中并不会遇到 xff0c 没有实际的解决问题经验 xff0c 看过也记不住 让你的Java知识功底更加强悍 xff0c 后面的框架 xff
  • 集合框架(4):HashMap底层原理分析

    文章目录 一 Map接口中常用的方法二 Map接口继承树三 Map底层源码分析1 Map xff1a 2 Map中key value的理解 xff1a 3 面试题 四 HashMap底层源码分析1 在jdk7中2 在jdk8中3 JDK1
  • HashMap中对红黑树、CAS等知识的补充

    目录 一 红黑树1 概念2 图示3 红黑树的特性 二 解决哈希冲突常见方法1 开放定址法2 链接地址3 再哈希法4 建立公共溢出区 三 CAS1 定义2 操作3 Java中CAS操作4 存在的问题5 实际应用 一 红黑树 1 概念 是一种自
  • 集合框架(1):Collection | Iterator | 增强for

    文章目录 Collection接口一 集合框架的概述Collection接口继承树Collection接口中的方法的使用使用迭代器Iterator遍历Collection集合增强for循环遍历集合 文章链接Java语法https blog