HashSet的学习
2016-03-28 18:06
204 查看
HashSet:是通过散列的机制将信息存储到哈希表。默认长度是16
通过散列获取数组下标,然后存在数组中。
特点:不能保证集合的顺序,存取速度非常的快,set里不能有重复的值。
相同的元素hash码值相同
hashset操作demo:
public static void main(String[] args) {
HashSet<String> hs=new HashSet<String>();
hs.add("a");
hs.add("b");
hs.add("c");
hs.add("d");
System.out.println(hs);
//HashSets与数组的交互
String[] str=new String[hs.size()];
hs.toArray(str);
for(String st:str){
System.out.println(st);
}
//第二种方法
HashSet<String> hs2=new HashSet<String>();
hs2.addAll(hs);
System.out.println(hs2);
}
通过散列获取数组下标,然后存在数组中。
特点:不能保证集合的顺序,存取速度非常的快,set里不能有重复的值。
相同的元素hash码值相同
hashset操作demo:
public static void main(String[] args) {
HashSet<String> hs=new HashSet<String>();
hs.add("a");
hs.add("b");
hs.add("c");
hs.add("d");
System.out.println(hs);
//HashSets与数组的交互
String[] str=new String[hs.size()];
hs.toArray(str);
for(String st:str){
System.out.println(st);
}
//第二种方法
HashSet<String> hs2=new HashSet<String>();
hs2.addAll(hs);
System.out.println(hs2);
}
相关文章推荐
- Android Intent的作用,哪些类型的数据可以被传递
- android studio 使用checkstyle全攻略
- 浅谈类型萃取
- Ansible6:Playbook简单使用
- Void的使用场景
- 分布式与集群的区别
- EventBus3.0学习小结
- 【机房个人重构】导出Excel
- 文件下载
- 设备管理
- 面试题之实现3^100次方和大整数的相乘
- 安卓软件远程连接ConnectBot v1.8.6
- unreal3窗口锁定鼠标开关
- SQL Case when 的使用方法(转自博客园:影子网络科技有限公司)
- Android UI基础——Button控件
- caffe调参经验资料文章
- php trait 简明教程,多重继承的实现方案
- Debian Chroot on Synology NAS
- CORBA, C++ and Linux:
- Swift -- !和?的使用