import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class S11 {
public static void main(String[] args){
Set<Integer> set = new HashSet<>();
set.add(10);
set.add(10);
set.addAll(List.of(11,22,33,44,45,66,55,11,22));
System.out.println(set.size());
System.out.println(set);
//删除
//set.clear();
System.out.println(set.remove(1));
System.out.println(set.removeAll(List.of(11,22)));
System.out.println(set);
//判断
System.out.println(set.contains(55));
System.out.println(set.containsAll(List.of(55,11,44)));
System.out.println(set);
//遍历
for (Integer i : set){
System.out.println(i);
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/0922b2871baa4567aecc397c7533c456.png)输出结果
HashSet的使用
最新推荐文章于 2024-09-08 11:09:43 发布