集合的基本操作

集合:

在java当中,含有着一些不同的存储数据的相关集合。分为单列集合(Collection)和双列集合(Map)。

在这里插入图片描述

Collection

首先学习Collection来进行展示:

以框框为例子,蓝色的代表的是接口,而红色的是代表的接口的实现类!在Collection当中,又分为两种一个是List,一个是Set!

List的特点:元素可以重复,含有索引,有序(在存和取的过程当中是有序的,先存储的时候,在遍历获取时就是先获取的)。

Set的特点和List完全相反:元素不可重复,无索引,无序。

Collection接口是单列集合的祖宗接口,所有的实现类都要实现这个接口。

那么首先来学习的可以是以Collection为例子:

在这里插入图片描述

由于Collection是接口,所以在实现代码的时候,要创建一个实现类实现这个接口,才能展现出来这个接口当中的所有方法。

基本方法

add

添加元素到collection当中

        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        collection.add("he1");
        System.out.println(collection);

可以添加重复的元素

clear

删除元素在collection中:

全部进行清空操作!

Collection<String> collection = new ArrayList<>();
collection.add("he1");
collection.add("he2");
collection.add("he3");
collection.add("he4");
System.out.println(collection);

collection.clear();
System.out.println(collection);
remove

删除指定的元素:

Collection<String> collection = new ArrayList<>();
collection.add("he1");
collection.add("he2");
collection.add("he3");
collection.add("he1");
System.out.println(collection);


boolean isNo = collection.remove("he1");
System.out.println(isNo);
System.out.println(collection);

ArrayList的源码删除:

/*从此列表中删除第一个出现的指定元素(如果存在)。如果列表不包含该元素,则该元素保持不变。更正式地说,删除索引 为 i 的元素,使得 (o==null ? get(i)==null : o.equals(get(i))) (如果存在这样的元素)。如果此列表包含指定的元素(或等效地,如果此列表因调用而更改),则返回 true 。
参数:
o – 要从此列表中删除的元素(如果存在)
返回:
如果此列表包含指定的元素,则为 true*/
public boolean remove(Object o) {
    if (o == null) {
        for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
                fastRemove(index);
                //找到一个就直接返回了,直接就删除了!
                return true;
            }
    } else {
        for (int index = 0; index < size; index++)
            if (o.equals(elementData[index])) {
                fastRemove(index);
                return true;
            }
    }
    return false;
}

这个是只会删除一个的情况的。

在这里插入图片描述

contains

处理包含的情况:

        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        System.out.println(collection);


//        boolean isNo = collection.remove("he1");
//        System.out.println(isNo);
//        System.out.println(collection);


        System.out.println(collection.contains("he1"));

查看是否含有h1的元素。

在这里插入图片描述

具体的源码是怎么进行查找的呢?

public int indexOf(Object o) {
    if (o == null) {
        for (int i = 0; i < size; i++)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = 0; i < size; i++)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}

根据对象所继承Object.equals进行判断的。是判断的是对象是否为同一个对象。

public boolean equals(Object obj) {
    return (this == obj);
}

如果String缓冲池内不存在与其指定值相同的String对象,那么此时虚拟机将为此创建新的String对象,并存放在String缓冲池内。

如果String缓冲池内存在与其指定值相同的String对象,那么此时虚拟机将不为此创建新的String对象,而直接返回已存在的String对象的引用。

 //接着System.out.println(s1.equals(s2));这里的equals在String类中被重写过,用来比较两个字符串的实际内容是否相等,即每一个字符是否相等,重写方法末尾会另做说明!!!因为比较的是字符串内容,s1,s2内容都是hello当然是相等的。 
 
		String ss1 = "aaa";
		//在池子里面又创建了一个!
         String ss2 = new String("aaa");
         System.out.println(ss1 == ss2);  //false
         System.out.println(ss1.equals(ss2));  // true
 
ss2是new出来的,所以重新分配内存地址,当用==判断时,返回false,但是两个字符串的内容相同,所以用equals方法时,返回true

对于含有自己创建的引用数据类型的情况!

        Collection<Student> students = new ArrayList<>();
        students.add(new Student("yyy" , 111));
        students.add(new Student("yyy" , 112));
        students.add(new Student("yy" , 111));
        System.out.println(students);

在这里插入图片描述

Collection<Student> students = new ArrayList<>();
students.add(new Student("yyy" , 111));
students.add(new Student("yyy" , 112));
students.add(new Student("yy" , 111));
System.out.println(students);
System.out.println(students.contains(new Student("yyy" , 111)));

在这里插入图片描述

返回的没有的信息,对于自己创建Students的类是直接使用equals进行判断,判断的是地址!

想要进行数据的判断是要进行方法的重写操作的。

在Students的类当中重写其方法!

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Student student = (Student) o;
    return age == student.age && Objects.equals(name, student.name);
}

在这里插入图片描述

这样就是可以直接根据对象的属性值进行判断了!

size()和isEmpty()
Collection<Student> students = new ArrayList<>();
students.add(new Student("yyy" , 111));
students.add(new Student("yyy" , 112));
students.add(new Student("yy" , 111));
System.out.println(students);
System.out.println(students.contains(new Student("yyy" , 111)));
System.out.println(collection.size());
System.out.println(collection.isEmpty());

在这里插入图片描述

集合的遍历方式

由于使用的是Collection的集合来进行处理操作的。那么想要对集合当中的元素进行遍历操作,就不能使用普通的for循环来进行遍历的。

原因是:在使用for循环的时候要进行索引的获取操作,但是在Collection的Set下面的接口是没有索引的。故不能直接使用for循环来进行遍历操作。

含有下面的方式来进行的遍历操作:

Iterator
public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        System.out.println(collection);
        
        Iterator<String> iterator = collection.iterator();
        //含有三个元素he1   he2   he3
        while (iterator.hasNext()) {
            String string = iterator.next();
            System.out.println(string);
        }
    }
}

直接进行遍历操作。使用的是指针来进行遍历的。

首先指向的是元素的第一个位置,使用hasNext的方法判断当前位置是否含有元素,若含有元素就直接进行返回true,使用iterator.next()是来进行先获取元素的,在进行指针的移动操作。不断的进行的移动最终使得将全部的元素都进行了遍历操作。

在这里插入图片描述

在这里插入图片描述

指向一个空指针的情况:抛出
package myCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @program: 集合
 * @description
 * @author: YangTao
 * @create: 2024-04-30 11:57
 **/
public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        System.out.println(collection);

        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            String string = iterator.next();
            System.out.println(string);
        }

         System.out.println(iterator.next());
    }
}

在这里插入图片描述

当迭代器当中的指针进行移动操作之后,会使得指针指向一个空的情况。造成一个异常的抛出!

/**
返回迭代中的下一个元素。
返回:
迭代中的下一个元素
抛出:
NoSuchElementException – 如果迭代没有更多元素
 */
E next();
在指针的遍历结束的时候,指针不会进行复位
Collection<String> collection = new ArrayList<>();
collection.add("he1");
collection.add("he2");
collection.add("he3");
System.out.println(collection);

Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()) {
    String string = iterator.next();
    System.out.println(string);
}

System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
每一个循环当中是只能调用一个next的方法的。一次指针的移动,一次判断获取元素的操作。
package myCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @program: 集合
 * @description
 * @author: YangTao
 * @create: 2024-04-30 11:57
 **/
public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        System.out.println(collection);

        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            String string1 = iterator.next();
            String string2 = iterator.next();
            System.out.println(string1);
            System.out.println(string2);

        }



    }
}

两次移动好像也是没有问题的,但是当含有的元素为奇数个情况就直接报错。

public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        collection.add("he1");
        collection.add("he2");
        System.out.println(collection);

        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            String string1 = iterator.next();
            System.out.println(string1);
            String string2 = iterator.next();
            System.out.println(string2);

        }



    }
}

在这里插入图片描述

指针移动两次,但是元素是只有奇数个的情况,在指向最后一个元素的next的时候会直接找不到那一个元素的。直接就抛异常了。

不能使用集合当中的方法进行添加元素和删除元素
package myCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @program: 集合
 * @description
 * @author: YangTao
 * @create: 2024-04-30 11:57
 **/
public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        collection.add("he1");
        collection.add("he2");
        System.out.println(collection);

        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {

            String string2 = iterator.next();
            System.out.println(string2);
            if(string2.equals("he3")){
                //进行修改
                System.out.println(collection.remove(string2));
            }
        }
    }
}

在这里插入图片描述

但是可以使用Iterator当中的remove方法进行删除操作,直接删除的Collection当中的方法。

package myCollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @program: 集合
 * @description
 * @author: YangTao
 * @create: 2024-04-30 11:57
 **/
public class Collection_test2 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("he1");
        collection.add("he2");
        collection.add("he3");
        collection.add("he1");
        collection.add("he2");
        System.out.println(collection);

        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {

            String string2 = iterator.next();
            System.out.println(string2);
            if(string2.equals("he3")){
                //进行修改
                iterator.remove();
            }
        }
        System.out.println(collection);


    }
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/588152.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Linux极简教程】常见实用命令不断更新中......

【Linux极简教程】常见实用命令不断更新中...... 常见问题1.Waiting for cache lock: Could not get lock /var/lib/dpkg/lock. It is held by process xxxx(dpkg) 常见问题 1.Waiting for cache lock: Could not get lock /var/lib/dpkg/lock. It is held by process xxxx(dp…

机器学习:基于Sklearn、XGBoost,使用逻辑回归、支持向量机和XGBClassifier预测股票价格

前言 系列专栏&#xff1a;机器学习&#xff1a;高级应用与实践【项目实战100】【2024】✨︎ 在本专栏中不仅包含一些适合初学者的最新机器学习项目&#xff0c;每个项目都处理一组不同的问题&#xff0c;包括监督和无监督学习、分类、回归和聚类&#xff0c;而且涉及创建深度学…

C语言——队列的实现

队列按照先进先出&#xff08;FIFO&#xff0c;First In First Out&#xff09;的原则管理数据。这意味着最先进入队列的元素会被最先移出&#xff0c;类似于排队等候服务的情况。队列通常有两个主要操作&#xff1a;入队&#xff08;enqueue&#xff09;&#xff0c;将元素添加…

DSP实时分析平台设计方案:924-6U CPCI振动数据DSP实时分析平台

6U CPCI振动数据DSP实时分析平台 一、产品概述 基于CPCI结构完成40路AD输入&#xff0c;30路DA输出的信号处理平台&#xff0c;处理平台采用双DSPFPGA的结构&#xff0c;DSP采用TI公司新一代DSP TMS320C6678&#xff0c;FPGA采用Xilinx V5 5VLX110T-1FF1136芯片&#xff…

《QT实用小工具·五十》动态增删数据与平滑缩放移动的折线图

1、概述 源码放在文章末尾 该项目实现了带动画、带交互的折线图&#xff0c;包含如下特点&#xff1a; 动态增删数值 自适应显示坐标轴数值 鼠标悬浮显示十字对准线 鼠标靠近点自动贴附 支持直线与平滑曲线效果 自定义点的显示类型与大小 自适应点的数值显示位置 根据指定锚点…

java并发编程-AQS介绍及源码详解

介绍 AQS 的全称为 AbstractQueuedSynchronizer &#xff0c;就是抽象队列同步器。 从源码上可以看到AQS 就是一个抽象类&#xff0c;它继承了AbstractOwnableSynchronizer&#xff0c;实现了java.io.Serializable接口。 public abstract class AbstractQueuedSynchronizere…

redis缓存详情

redis安装包及图形化软件: 百度链接&#xff1a;https://pan.baidu.com/s/1wljo7JzgrSQyqldv9d5HZA?pwdht1m 提取码&#xff1a;ht1m 目录 1.redis的下载及安装 1.1redis的启动与停止 1.2Redis服务启动与停止 2.redis数据类型及常用指令 2.1redis数据类型 2.2redis常用…

读天才与算法:人脑与AI的数学思维笔记15_声响的数学之旅

1. 音乐 1.1. 巴赫的作品以严格的对位著称&#xff0c;他十分中意对称的结构 1.2. 巴托克的作品很多都以黄金比例为结构基础&#xff0c;他非常喜欢并善于使用斐波纳契数列 1.3. 有时&#xff0c;作曲家是本能地或者不自知地被数学的模式和结构所吸引&#xff0c;而他们并没…

Golang | Leetcode Golang题解之第61题旋转链表

题目&#xff1a; 题解&#xff1a; func rotateRight(head *ListNode, k int) *ListNode {if k 0 || head nil || head.Next nil {return head}n : 1iter : headfor iter.Next ! nil {iter iter.Nextn}add : n - k%nif add n {return head}iter.Next headfor add > …

【项目构建】04:动态库与静态库制作

OVERVIEW 1.编译动态链接库&#xff08;1&#xff09;编译动态库&#xff08;2&#xff09;链接动态库&#xff08;3&#xff09;运行时使用动态库 2.编译静态链接库&#xff08;1&#xff09;编译静态库&#xff08;2&#xff09;链接静态库&#xff08;3&#xff09;运行时使…

matlab学习007-已知离散时间系统的系统函数并使用matlab绘制该系统的零极点图;判断系统的稳定性;幅频和相频特性曲线

目录 题目 离散时间系统的系统函数&#xff1a;H(z)(3*z^3-5*z^210z)/(z^3-3*z^27*z-5) 1&#xff0c;绘制该系统的零极点图 1&#xff09;零极点图 2&#xff09;代码 2&#xff0c;判断系统的稳定性 1&#xff09;判断结果 2&#xff09;代码 3&#xff0c;试用MATL…

C++的未来之路:探索与突破

在计算机科学的浩瀚星空中&#xff0c;C无疑是一颗璀璨的明星。自诞生以来&#xff0c;它以其强大的性能和灵活的特性&#xff0c;赢得了无数开发者的青睐。然而&#xff0c;随着技术的不断进步和应用的日益复杂&#xff0c;C也面临着前所未有的挑战和机遇。本文将探讨C的未来之…

腾锐D2000-8 MXM VPX,全国产,可广泛应用于边缘计算网关、入侵检测、VPN、网络监控等等应用领域

腾锐D2000-8 MXM VPX 1. 概述 XMVPX-108 是一款基于飞腾 D2000/8 处理器的低功耗逻辑运算和图形处理 VPX 刀片&#xff0c; 板贴 32GB DDR4 内存&#xff0c;搭载飞腾 X100 套片&#xff0c;满足通用 IO 接口功能。GPU 采用 MXM 小型插卡形式&#xff0c; 搭配 8GB 显卡。提供…

【16-降维技术:PCA与LDA在Scikit-learn中的应用】

文章目录 前言主成分分析(PCA)原理简介Scikit-learn中的PCA实现应用示例线性判别分析(LDA)原理简介Scikit-learn中的LDA实现应用示例总结前言 降维是机器学习中一种常见的数据预处理方法,旨在减少数据集的特征数量,同时尽量保留原始数据集的重要信息。这不仅有助于减少计…

开箱子咸鱼之王H5游戏源码_内购修复优化_附带APK完美运营无bug最终版__GM总运营后台_附带安卓版本

内容目录 一、详细介绍二、效果展示2.效果图展示 三、学习资料下载 一、详细介绍 1.包括原生打包APK&#xff0c;资源全部APK本地化&#xff0c;基本上不跑服务器宽带 2.优化后端&#xff0c;基本上不再一直跑内存&#xff0c;不炸服响应快&#xff01; 3.优化前端&#xff0c…

【源码阅读】Golang中的go-sql-driver库源码探究

文章目录 前言一、go-sql-driver/mysql1、驱动注册&#xff1a;sql.Register2、驱动实现&#xff1a;MysqlDriver3、RegisterDialContext 二、总结 前言 在上篇文章中我们知道&#xff0c;database/sql只是提供了驱动相关的接口&#xff0c;并没有相关的具体实现&#xff0c;具…

NLP 笔记:TF-IDF

TF-IDF&#xff08;Term Frequency-Inverse Document Frequency&#xff0c;词频-逆文档频率&#xff09;是一种用于信息检索和文本挖掘的统计方法&#xff0c;用来评估一个词在一组文档中的重要性。TF-IDF的基本思想是&#xff0c;如果某个词在一篇文档中出现频率高&#xff0…

不坑盒子2024.0501版,Word朗读、Word表格计算、Word中代码高亮显示行号、Excel中正则提取内容……

通过“听”来审阅Word中的内容&#xff0c;能轻松找出那些容易被眼看忽视的错字。 不坑盒子2024.0501版来了&#xff0c;很多奇妙的事情&#xff0c;正在发生…… 功能一览 此版本共带来10余项变动&#xff0c;来看看有没有你感兴趣的吧~ 接入Azure的“语音”能力 接入“语…

Flutter笔记:Widgets Easier组件库(3)使用按钮组件

Flutter笔记 Widgets Easier组件库&#xff08;3&#xff09;&#xff1a;使用按钮组件 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite&#xff1a;http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddre…

C语言之详细讲解文件操作(抓住文件操作的奥秘)

什么是文件 与普通文件载体不同&#xff0c;文件是以硬盘为载体存储在计算机上的信息集合&#xff0c;文件可以是文本文档、图片、程序等等。文件通常具有点三个字母的文件扩展名&#xff0c;用于指示文件类型&#xff08;例如&#xff0c;图片文件常常以KPEG格式保存并且文件…
最新文章