4

What I'm supposed to do is create an ArrayList of random numbers via stream.generate. Code below is my attempt of trying to save it into an ArrayList, but it's of type "object". I figured I have to somehow map it into int first, but I don't know how. Code right now doesnt work.

public ArrayList<Integer> createRandomList(ArrayList<Integer> list, int amount) {
     ArrayList<Integer> a = Arrays.asList(Stream.generate(new Supplier<Integer>() {
                    @Override
                    public Integer get() {
                        Random rnd = new Random();
                        return rnd.nextInt(100000);
                    }
                }).limit(amount).mapToInt.toArray());
}
Eran
  • 387,369
  • 54
  • 702
  • 768
David Trevor
  • 794
  • 1
  • 7
  • 22

2 Answers2

7

You may use create the stream of ints directly from Random object using ints(...), box them using boxed() and use the collector to store the result:

return new Random().ints(amount, 0, 100000)
                   .boxed()
                   .collect(Collectors.toCollection(ArrayList::new));
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
3

You can use a Collector :

return Stream.generate (() -> new Random().nextInt(100000))
             .limit(amount)
             .collect (Collectors.toList());

This would produce a List<Integer> though, not necessarily an ArrayList<Integer>.

It would make more sense to create a single Random instance though :

final Random rnd = new Random();
return Stream.generate (() -> rnd.nextInt(100000))
             .limit(amount)
             .collect (Collectors.toList());
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 2
    Okay, what does the collect operation do exactly? – David Trevor Jun 29 '15 at 09:16
  • 1
    @taclight It collects the elements of the Stream using the supplied Collector. `Collectors.toList()` is a Collector that creates a List instance, adds all the Stream elements to it, and returns that List. – Eran Jun 29 '15 at 09:20
  • 1
    how does it know which type the elements in the list have to be? – David Trevor Jun 29 '15 at 09:22
  • @taclight The type is determined by the type of the elements that the Stream contains. – Eran Jun 29 '15 at 09:23
  • In my first try it told me that the created element of the stream is of type object, whats different now? – David Trevor Jun 29 '15 at 09:25
  • 2
    @taclight First of all `mapToInt` should be `mapToInt(Integer::intValue)`. Now, `toArray()` will produce an `int[]`. Then you pass it to Arrays.asList, which creates an `Arrays$ArrayList`, not an `ArrayList` as you expected. – Eran Jun 29 '15 at 09:34
  • Thanks for the insight, one last question, how do I convert an array into ArrayList then while maintaining its type? – David Trevor Jun 29 '15 at 09:44
  • @taclight If it's an array of reference types, you could use `new ArrayList(Arrays,asList(theArray))`. For primitive type arrays you have to iterate over the elements and add them to the ArrayList (though you can use some Stream operations to avoid an explicit loop). – Eran Jun 29 '15 at 09:52