4

I have line like this↓ to get exact elements from list, but i want to add it multiple time using some kind of array like "for" with counter

list.stream().filter(x -> x.getUserID() == user.getUserID()).collect(Collectors.toList());
list.stream().map(o -> new Object[] { (Object) o }).collect(Collectors.toList();

I've had similar code but i dont want to use double for:

List<Object[]> tmp = new ArrayList<Object[]>();
for (Iterator<?> iterator = tests.getTestData().iterator(); iterator.hasNext();) {
    Object objects = iterator.next();
    //should have condition like id=id
    for (int i = 0; i < t.getInvocationCount(it); i++) {
        tmp.add(new Object[] { objects });
    }
}

It is possible to multiple elements which fulfils condition using stream?

EDIT:

*tests.getTestData() -> returns List
**t.getInvocationCount -> returns int [t is not important cause it is generic]

i only need to multiple element in arry, in notice

FOR arry TO arry=END DO:
  IF arry[i] IS statment=true DO:
    FOR 0 TO outsideCounter_i DO:
      tempArry.add(arry[i])

where * is arry and ** is outsideCounter

I want to multiple element if statment is true using stream. If it is still unclear please add comment.

i read about nCopies and it is "cool" but can i use it inside stream?

1 Answers1

8

You can use an IntStream as the indices used to duplicate the element.

Something like this should work (I'm not entirely sure how your two code snippets are related, so I may have gotten the names wrong) :

List<Object[]> tmp =
    tests.getTestData().stream()
        .filter(x -> x.getUserID() == user.getUserID())  // not sure about this
                                                         // part, since it's not
                                                         // clear if the elements 
                                                         // of the input 
                                                         // Iterable have a 
                                                         // getUserID method
        .flatMap (x -> IntStream.range(0,t.getInvocationCount(it)).mapToObj(i -> x))
        .map(o -> new Object[] {o})
        .collect (Collectors.toList());

As aioobe commented, the Collections.nCopies method can be useful here:

List<Object[]> tmp =
    tests.getTestData().stream()
        .filter(x -> x.getUserID() == user.getUserID())  // not sure about this
                                                         // part, since it's not
                                                         // clear if the elements 
                                                         // of the input 
                                                         // Iterable have a 
                                                         // getUserID method
        .flatMap (o -> Collections.nCopies(t.getInvocationCount(it),o).stream())
        .map (o -> new Object[] {o})
        .collect (Collectors.toList());
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 4
    How about something like `Collections.nCopies(new Object[] { o }).stream()`? – aioobe Mar 31 '15 at 10:12
  • 1
    @aioobe Thanks. I didn't know such a method existed. – Eran Mar 31 '15 at 10:17
  • 1
    Using `nCopies` like that will change the semantics of the code. It will make the array once and then duplicate it n times. In the original code, a new array is created every time. To match the original code, do `nCopies` on object itself and then map to `new Object[]` separately. – Misha Mar 31 '15 at 10:36
  • 1
    @Holger Thanks for the comment. I fixed it, though the nCopies solution looks better anyway. – Eran Mar 31 '15 at 10:49
  • Nope. :/ this still does not work correct, all time i'am getting 2 when t.getInvocationCount(it) is 15 :/ – underwater ranged weapon Mar 31 '15 at 11:40
  • 1
    @shutdown -h now: I have my doubts regarding `t.getInvocationCount(it)` as neither `t` nor `it` is in the scope of your question. Either, invoking the method again for each item is obsolete or you have a mutable state that depends on something that is not seen in the code here. Don’t be surprised about unpredictable results then. – Holger Mar 31 '15 at 12:38