这个可以看我的其他的博客。hashset,hashmap底层理解总结。
@SuppressWarnings({"all"})//抑制警告。
public class Set_ {
public static void main(String[] args) {
Set hashSet = new HashSet();
hashSet.add("john");
hashSet.add("lucy");
System.out.println(hashSet.add("john"));
hashSet.add("jack");
hashSet.add("aaa");
hashSet.add(null);
System.out.println(hashSet.add(null));
System.out.println("set=" +hashSet );
hashSet.add(new Cat("tom"));
hashSet.add(new Cat("tom"));
System.out.println("set=" +hashSet );
hashSet.add(new String("我"));
hashSet.add(new String("我"));
System.out.println("set=" +hashSet );
System.out.println("只有一个我,哈哈哈");
//写一个for遍历
for (Object o : hashSet) {
System.out.println(o);
}
}
}
class Cat{
public String name;
public Cat(String name) {
this.name = name;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
'}';
}
}