Java 8 Stream, add one element to list many time by condition;

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?

Upvotes: 4

Views: 9706

Answers (1)

Eran
Eran

Reputation: 393781

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());

Upvotes: 8

Related Questions