What is the most efficent way to generate unique double values in Java

后端 未结 3 1197
温柔的废话
温柔的废话 2021-01-29 14:47

There are methods such as searching for duplicates but I wonder if there is a better solution for this task.

3条回答
  •  温柔的废话
    2021-01-29 15:12

    You can use Set collection. It won't allow insertion of unique values. Below is an example:

    Set doubles = new HashSet();
    Random r = new Random();
    for(int i=0 ; i<100 ; i++){
        doubles.add(r.nextDouble() * 100);
    }
    

提交回复
热议问题