import java.util.*;
public class SetOfInteger
{
public static void main(String args[])
{
Random rand = new Random(47);
Set<Integer> intset = new HashSet<Integer>();
for(int i =0; i<10000; i ++)
intset.add(rand.nextInt(30));
System.out.println(intset);
System.out.println(intset.size());
}
}
set只添加不重复的对象
10000个0-29的随机数中 可以看到结果也只是29个数字
ava SetOfInteger
, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20, 2
, 26, 29, 28]