package 集合架构;
import java.util.*;
//散列集HashSet
public class TestHashSet {
public static void main(String[] args){
// Create a hash set
Set<String> set = new HashSet<String>();
//Add string 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); //"New York"添加了两次,只有一个被储存,因为规划集合set不允许有重复的元素
//集合中的字符串没有按照插入时的顺序存储
//Obtain an iterator for the hash set
Iterator iterator = set.iterator();
//Display the elements in the hash set
while(iterator.hasNext()){
System.out.println(iterator.next() + " ");
}
}
}
Java集合架构 散列表HashSet
最新推荐文章于 2023-12-13 09:26:46 发布