HashSet详细介绍

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

最新文章

  1. Python命令 (if __name__=="__main__":)
  2. 【转】parallels desktop 11 授权许可文件删除方法
  3. XTU 1250 Super Fast Fourier Transform
  4. day01(计算机组成,进制,内存分布,操作系统)
  5. Python-Django-Djangorestframwork
  6. springboot系列五、springboot常用注解使用说明
  7. sap hana 数据库 EBS
  8. 现代OpenGL渲染管线介绍
  9. LinqProvider系列(三)如何实现自己的Linq Provider?
  10. Dingo Api 1.0在laravel5.2中的简单应用
  11. 简明python教程 --C++程序员的视角(六):输入输出IO
  12. C#集合总结
  13. (数据科学学习手札49)Scala中的模式匹配
  14. 八、网页版消息推送SDK-WebSockets
  15. (转)C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区
  16. js_一个简单的30分钟循环倒计时
  17. web 导出数据到 exls 中
  18. TFTP & commons-net-3.3.jar
  19. Parsing Netflow using Kibana via Logstash to ElasticSearch
  20. dfs序学习总结

热门文章

  1. 转:Numpy教程
  2. NIO buffer 缓冲区 API
  3. MATLAB常用方法技巧总结
  4. 记录两个python的小问题
  5. ZooKeeper与仲裁模式
  6. vue表单验证--veevalidate使用教程
  7. Django admin 使用多个数据库
  8. javascript变量,作用域和内存问题
  9. spring注解扫描组件注册
  10. Java Tutorial