Any filter-like lambda operation which does not discard?

前端 未结 2 1394
南方客
南方客 2020-12-10 10:12

I basically would like to do something like:

  assertEquals(Arrays.asList(1,2,3).stream()
                                   .noDiscardingFilter(x -> x!=1         


        
2条回答
  •  甜味超标
    2020-12-10 10:38

    If you want to only change the entries which match the filter but retain the rest.

    assertEquals(Arrays.asList(-1, 1, 20, 30),
                 Stream.of(-1, 1, 2, 3)
                       .map(i -> i <= 1 ? i /* retain */ : 10 * i /* transform */)
                       .collect(Collectors.toList()));
    

提交回复
热议问题