mercredi 7 novembre 2018

Random without duplicate using stream and questions about sort Java

I am trying to get 8 random int using stream but the problem with the code below is that distinct() removes duplicate which will not give me 8 int if there was any duplicate.

Goal:

1.Get 8 random int (no duplicate)

2.Add to List

3.Sort the first 7 int.

I know Collections.sort(winlist.subList(0, 6)); works for sorting but im trying to see if it can be done with just stream.

    new Random()
            .ints (8, 0, 64)
            .distinct()
            .sorted()
            .forEach (Integer -> System.out.print (Integer + "\n"));




Aucun commentaire:

Enregistrer un commentaire