不重复随机数导入集合

package com.liushuaishuai;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

public class RandomDemo01 {
    public static void main(String[] args) {
        Set<Integer> ts = new HashSet<Integer>();
        Random rd = new Random();

        while (ts.size() < 10) {
            int x = rd.nextInt(20) + 1;
            ts.add(x);
        }
        for (int s : ts) {
            System.out.println(s);
        }

    }
}
原文地址:https://www.cnblogs.com/lsswudi/p/11411249.html