dimanche 6 décembre 2015

Assign random integer numbers to an ArrayList using stream

I came up with this:

ArrayList<Integer> randomIntegers = new ArrayList<>();
randomIntegers = Stream.generate(Math::random).map(n -> n * 10000)
                                              .mapToInt(Double::intValue)
                                              .limit(10)
                                              .boxed()
                                              .collect(Collectors.toCollection(ArrayList::new));

Since I am pretty new to streams: Is there something more elegant i.e. shorter while at least equally readable (still using streams)?




Aucun commentaire:

Enregistrer un commentaire