【 java 集合】Collection 接口中的常用方法
创始人
2024-03-18 03:45:48
0

📋 个人简介

  • 💖 作者简介:大家好,我是阿牛,全栈领域优质创作者。😜
  • 📝 个人主页:馆主阿牛🔥
  • 🎉 支持我:点赞👍+收藏⭐️+留言📝
  • 📣 系列专栏:java 小白到高手的蜕变🍁
  • 💬格言:要成为光,因为有怕黑的人!🔥
    请添加图片描述

目录

    • 📋 个人简介
  • 前言
    • Collection 接口中的常用方法
      • 添加
      • 求长度
      • 判断当前集合是否为空
      • 清空集合元素
      • 判断当前对象是否在集合中
      • 移除
      • 判断相等
      • 集合转换为数组
      • 数组转换为集合
  • 结语

前言

本节将大概用代码案例简单总结一下 Collection 接口中的一些方法,我们会以他的实现类 Arraylist 为例创建对象。一起来看看吧!

Collection 接口中的常用方法

添加

import java.util.ArrayList;
import java.util.Collection;/*** @Author:Aniu* @Date:2022/12/4 16:19* @description TODO*/
public class Demo {public static void main(String[] args) {Collection coll = new ArrayList();// add(Object e) 增加coll.add("aniu");coll.add(123); //自动装箱coll.add(new String("miao"));System.out.println(coll);System.out.println("------------------");// addAll() 将另一个集合中的元素添加到当前集合中Collection coll1 = new ArrayList();coll1.add(123);coll1.add("bb");coll.addAll(coll1);System.out.println(coll);}
}

在这里插入图片描述

求长度

// size() 求添加的元素个数Collection coll = new ArrayList();coll.add("aniu");coll.add(123); //自动装箱coll.add(new String("miao"));System.out.println(coll.size());

判断当前集合是否为空

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自动装箱
coll.add(new String("miao"));
//isEmpty() 判断当前集合是否为空
System.out.println(coll.isEmpty());

清空集合元素

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自动装箱
coll.add(new String("miao"));
//clear() 清空集合元素
System.out.println(coll.clear());

判断当前对象是否在集合中

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自动装箱
coll.add(new String("miao"));
// contains() 判断对象是否在当前集合中
System.out.println(coll.contains(new String("aniu")));

这里要注意的是,contains本质上是用equals比较的,因此,对于自定义对象,要记得重写equals方法!

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自动装箱
coll.add(new String("miao"));
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add("aniu");
// containsAll() 判断形参集合中的元素是否在当前集合中
System.out.println(coll.containsAll(coll1));

本质上依旧是用equals一个个比较

移除

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自动装箱
coll.add(456);
coll.add(new String("miao"));
// remove() 移除
coll.remove(123);
System.out.println(coll);
System.out.println("------------");Collection coll1 = new ArrayList();
coll1.add(456);
coll1.add(new String("miao"));
// removeAll() 从当前集合中移除形参集合中的所有元素,即差集
coll.removeAll(coll1);
System.out.println(coll);

在这里插入图片描述

removeAll() 相当于求差集,那么也有对应求交集的!

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自动装箱
coll.add(new String("miao"));
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add(new String("miao"));
// retainAll() 即求交集
coll.retainAll(coll1);
System.out.println(coll);

在这里插入图片描述

判断相等

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自动装箱
Collection coll1 = new ArrayList();
coll1.add(123);
coll.add("aniu");
// equals() 判断两个集合是否相等,因为这里使用ArrayList()实现,因此要考虑顺序
System.out.println(coll.equals(coll1));

在这里插入图片描述

集合转换为数组

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自动装箱
// toArray() 集合转数组
Object[] arr = coll.toArray();
System.out.println(Arrays.toString(arr));

数组转换为集合

既然说到了集合转数组,这里就说一下数组转集合!

 List list = Arrays.asList(new String[]{"aniu", "tom"});System.out.println(list);

结语

本来关于这些api是不想总结的,像String中的一些api,和其他语言中的差不多,我就没总结!集合中的方法名与其他语言稍微有不同,这里快速过一下。

如果你觉得博主写的还不错的话,可以关注一下当前专栏,博主会更完这个系列的哦!也欢迎订阅博主的其他好的专栏。

🏰系列专栏
👉软磨 css
👉硬泡 javascript
👉flask框架快速入门

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...