定义一个Set,向Set中存储5个人名,其中有两个是重复的,输出Set集合的数据,通过for each,和Iterator两种方法。
public class TestHashSet{
public static void main(String[] args){
//Create a hash set
Set set = new HashSet();
//Add strings to the set
set.add("London");
set.add("Paris");
set.add("New York");
set.add("San Francisco");
set.add("Beijing");
set.add("New York");
System.out.println(set);
//Obtain an iterator for the hash set
Iterator<String> iterator = new set.iterator();
//Display the elements in the hash set
while(iterator.hasNext()){
System.out.print(iterator.next().toUpperCase() + " ");
}
}
}