可以参考前面发布的 Collection 介绍博客:网页链接
Set<String> set = new HashSet<>();
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("张三");
set.add("张三");
System.out.println(set);
}
public static void main(String[] args) {
Set<String> set = new HashSet<>();
boolean b1 = set.add("张三");
boolean b2 = set.add("张三");
System.out.println(b1);
System.out.println(b2);
}
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("张三");
set.add("李四");
set.add("王五");
System.out.println(set);
}
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("张三");
set.add("李四");
set.add("王五");
boolean b1 = set.remove("张三");
boolean b2 = set.remove("张三");
System.out.println(b1);
System.out.println(b2);
}
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("张三");
set.add("李四");
set.add("王五");
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
String data = iterator.next();
System.out.println(data);
}
}
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("张三");
set.add("李四");
set.add("王五");
for (String s : set) {
System.out.println(s);
}
}
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("张三");
set.add("李四");
set.add("王五");
set.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.print(s + "\t\t");
}
});
System.out.println("\n----------------------");
set.forEach(s ->System.out.print(s + "\t\t"));
System.out.println("\n----------------------");
set.forEach(System.out::println);
}
评论
登录后才可以进行评论哦!