Search code examples
javajava-streamfunctional-interface

Is there a way to apply a function with multiple parameters to a stream?


I have the following generic method for transforming a page object into a Slice:

public static <T,R> Slice<R> toSlice(final Page<T> source, Function<T,R> transform) {
  return Slice.<R>builder()
      .totalPages(BigInteger.valueOf(source.getTotalPages()))
      .pageData(source.stream()
          .map(transform)
          .collect(Collectors.toList()))
      .build();
}

I would then use this as follows:

return SliceFactory.toSlice(
            someDao.findAll(
                    predicate,
                    PageRequest.of(page, PAGE_SIZE,
                        Sort.by(Sort.Direction.DESC, DEFAULT_SORTING_PROPERTY))),
            dtoFactory::toSomeDto);

However, I would like to now pass an additional argument to dtoFactory.toSomeDto(). I'm not sure how can I turn this into a generic method, similar to the existing toSlice() method?


Solution

  • Just call it with param -> dtoFactory.toSomeDto(param, additionalParam) but in that case you have to ensure that additionalParam is effectively immutable.