吓了一跳哦

导航

Java中HashSet使用源码演示

如下的代码段是关于Java中HashSet使用演示的代码,希望对各位朋友有所好处。

import java.util.HashSet;

public class myHashsetExample {
public static void main(String[] args) {
HashSet<String> languages = new HashSet<String>();
languages.add("hindi");
languages.add("english");
languages.add("spanish");
System.out.println("The number of languages is " + languages.size());
for (String name : languages) {
System.out.println(name);
}
languages.remove("hindi");
languages.add("tamil");
String name = "hindi";
if (languages.contains(name)) {
System.out.println(name + " is in the collection.");
} else {
System.out.println(name + " is not in the collection.");
}
for (String n : languages) {
System.out.println(n);
}
languages.clear();
System.out.println("The number of languages is " + languages.size());
}
}




 

posted on 2021-12-06 15:08  吓了一跳哦  阅读(33)  评论(0编辑  收藏  举报