- Set容器添加元素时,如果该元素已存在,则返回false,如果不存在,则返回true。
Set<Integer> set = new HashSet<>();
set.add(1);
if(set.add(1)){
System.out.println("添加成功");
}else{
System.out.println("添加失败!原因是1已存在,set容器中不能存入相同元素");
}
Set<Integer> set = new HashSet<>();
set.add(1);
if(set.add(1)){
System.out.println("添加成功");
}else{
System.out.println("添加失败!原因是1已存在,set容器中不能存入相同元素");
}