如下的代码段是关于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());
}
}
原文地址:https://www.cnblogs.com/anemone/p/11452914.html