Java 实例 - 删除集合中指定元素

Java 教程 · 2019-02-11 17:12:55

以下实例演示了如何使用 Collection 类的 collection.remove() 方法来删除集合中的指定的元素:

Main.java 文件

import java.util.*; public class Main { public static void main(String [] args) { System.out.println( "集合实例!\n" ); int size; HashSet collection = new HashSet (); String str1 = "Yellow", str2 = "White", str3 = "Green", str4 = "Blue"; Iterator iterator; collection.add(str1); collection.add(str2); collection.add(str3); collection.add(str4); System.out.print("集合数据: "); iterator = collection.iterator(); while (iterator.hasNext()){ System.out.print(iterator.next() + " "); } System.out.println(); collection.remove(str2); System.out.println("删除之后 [" + str2 + "]\n"); System.out.print("现在集合的数据是: "); iterator = collection.iterator(); while (iterator.hasNext()){ System.out.print(iterator.next() + " "); } System.out.println(); size = collection.size(); System.out.println("集合大小: " + size + "\n"); } }

以上代码运行输出结果为:

集合实例!

集合数据: White Yellow Blue Green 
删除之后 [White]

现在集合的数据是: Yellow Blue Green 
集合大小: 3

点击查看所有 Java 教程 文章: https://www.codercto.com/courses/l/12.html

查看所有标签

逆向工程权威指南

逆向工程权威指南

Dennis Yurichev(丹尼斯) / 安天安全研究与应急处理中心 / 人民邮电出版社 / 2017-3-1 / 168

逆向工程是一种分析目标系统的过程,旨在于识别系统的各组件以及组件间关系,以便于通过其它形式、或在较高的抽象层次上,重建系统的表征。 本书专注于软件的逆向工程,是写给初学者的一本经典指南。全书共分为12个部分,共102章,涉及X86/X64、ARM/ARM-64、MIPS、Java/JVM等重要话题,详细解析了Oracle RDBMS、Itanium、软件狗、LD_PRELOAD、栈溢出、EL......一起来看看 《逆向工程权威指南》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具