package com.heima.set;
import java.util.HashSet;
import com.heiam.bean.Person;
public class Demo1_HashSet {
public static void main(String[] args) {
demo1();
}
public static void demo1() {
HashSet<String> hs = new HashSet<>(); //创建HashSet对象
boolean b1 = hs.add("a");
boolean b2 = hs.add("a"); //当向set集合中存储重复元素的时候返回为false
hs.add("b");
hs.add("c");
hs.add("d");
System.out.println(hs);
System.out.println(b1);
System.out.println(b2);
for (String string : hs) { //增强for循环遍历
System.out.println(string);
}
}
} |
|