HashSet小试牛刀
2024-08-26 09:06:28
import java.util.HashSet;
import java.util.Iterator; public class Main { public static void main(String[] args) {
HashSet set=new HashSet();
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("e");
Iterator ite=set.iterator();
System.out.print("通过Iterator遍历HashSet: ");
while(ite.hasNext()){
System.out.print(ite.next()+" ");
}
System.out.println();
System.out.print("HashSet的大小: ");
System.out.println(set.size());
System.out.print("HashSet是否包含元素1: ");
System.out.println(set.contains(1));
System.out.print("HashSet是否包含元素a: ");
System.out.println(set.contains("a"));
set.remove("b");
String array[]=(String [])set.toArray(new String[0]); //将HashSet变换为数组
System.out.print("将元素b从set中移除后: ");
for(String str:array){
System.out.print(str);
}
System.out.println();
HashSet otherset=new HashSet();
otherset.add("a");
otherset.add("c");
otherset.add("e"); HashSet removeset=(HashSet)set.clone(); //克隆
removeset.removeAll(otherset);
System.out.println(removeset); HashSet retainset=(HashSet)set.clone();
retainset.retainAll(otherset);
System.out.println(retainset);
} }
输出
通过Iterator遍历HashSet: a b c d e
HashSet的大小: 5
HashSet是否包含元素1: false
HashSet是否包含元素a: true
将元素b从set中移除后: acde
[d]
[a, c, e]
21:54:31
2019-02-28
最新文章
- Python命令 (if __name__==";__main__";:)
- 【转】parallels desktop 11 授权许可文件删除方法
- XTU 1250 Super Fast Fourier Transform
- day01(计算机组成,进制,内存分布,操作系统)
- Python-Django-Djangorestframwork
- springboot系列五、springboot常用注解使用说明
- sap hana 数据库 EBS
- 现代OpenGL渲染管线介绍
- LinqProvider系列(三)如何实现自己的Linq Provider?
- Dingo Api 1.0在laravel5.2中的简单应用
- 简明python教程 --C++程序员的视角(六):输入输出IO
- C#集合总结
- (数据科学学习手札49)Scala中的模式匹配
- 八、网页版消息推送SDK-WebSockets
- (转)C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区
- js_一个简单的30分钟循环倒计时
- web 导出数据到 exls 中
- TFTP &; commons-net-3.3.jar
- Parsing Netflow using Kibana via Logstash to ElasticSearch
- dfs序学习总结