import java.util.HashSet;标签:Set,System,接口,华为,set,使用,println,out From: https://www.cnblogs.com/123456dh/p/17136664.html
import java.util.Iterator;
import java.util.Set;
public class Demo01 {
public static void main(String[] args) {
//Set接口:无序,无下标,不可重复
//创建集合
Set<String> set = new HashSet<>();
//1.添加数据
set.add("小米");
set.add("苹果");
set.add("华为");
//set.add("华为");
System.out.println("数据个数:"+set.size());//数据个数:3
System.out.println(set);//[苹果, 华为, 小米]
//2.删除数据
set.remove("小米");
System.out.println("数据个数:"+set.size());//数据个数:2
System.out.println(set);//[苹果, 华为]
//3.遍历(重点)
//1.增强for
for (String s:
set) {
System.out.println(s);
}
//2.使用迭代器
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//4.判断
System.out.println(set.contains("华为"));//true
System.out.println(set.isEmpty());//false
}
}