treeset和set产生随机数字
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Random random = new Random();
//Set<Integer> set=new HashSet<Integer>();
TreeSet<Integer> set = new TreeSet<Integer>();
while (set.size() < 10) {
int r = random.nextInt(20) + 1;
set.add(r);
}
for (int i : set) {
System.out.println(i);
}
}
}
他们的区别是,set是不存储重复的内容,而treeset既可以存储不重复还能排序
标签:知识点,set,java,Random,util,import,new From: https://www.cnblogs.com/zhaoshengfu/p/18076003