Java集合-HashSet 集合
一、HashSet 集合
1.1、HashSet 集合概述和特点
1.1.1、HashSet 集合概述
- HashSet 集合是具体的类
- 该类实现了 Set 接口
1.1.2、HashSet 集合特点
- 底层数据结构是哈希表
- 对集合的迭代顺序不作任何保证,也就是说不保证存储和取出的元素顺序一致
- 没有带索引的方法,所以不能使用普通 for 循环遍历
- 由于是 Set 集合,所以是不包含重复元素的集合
1.2、使用 HashSet 集合
// 创建 Set 集合对象
Set<String> set = new HashSet<>();
// 添加元素
set.add("hello");
set.add("world");
set.add("java");
// 遍历
for (String s : set){
System.out.println(s);
}

浙公网安备 33010602011771号