How to include elements from unmodified list in modified list with Java Streams?

我与影子孤独终老i 提交于 2019-12-06 04:08:58

You could stream the original list and limit the stream to exclude the last element, do the mapping and collect to a new ArrayList, then add the last element of the original list to the last position of the new, mutable list:

int size = list.size();

List<Integer> result = list.stream()
    .limit(size - 1)
    .map(i -> i - 2)
    .collect(Collectors.toCollection(() -> new ArrayList(size)));

result.add(list.get(size - 1));

Another way would be to do the mapping and collect all the elements of the original list into the new list and then simply set the last element of the original list in the last position of the new list, overwriting the previous element:

int size = list.size();

List<Integer> result = list.stream()
    .map(i -> i - 2)
    .collect(Collectors.toCollection(() -> new ArrayList(size)));

result.set(size - 1, list.get(size - 1));
List<Integer> result = 
    list.subList(0, list.size() - 1)
        .stream().map(i -> i - 2)
        .collect(Collectors.toCollection(ArrayList::new));
result.add(list.get(list.size() - 1));
holi-java

You can compose Stream#toList with Stream#collectingAndThen to copy a List, then use List#replaceAll to replace all items except the last one.

List<Integer> result = list.stream().collect(collectingAndThen(toList(), it -> {
                         // v--- replacing all items in list except the last one 
    it.subList(0, it.size() - 1).replaceAll(item -> item - 2);
    return it;
}));

AND you can see the stream api just copy the original List into another List created by stream, so your code can simplify as below:

List<Integer> result = new ArrayList<>(list);
result.subList(0, result.size() - 1).replaceAll(item -> item - 2);
//^--- if you don't want to copy the list, you can just operates on list instead   

You'll need to alter your solution a little bit and use IntStream#range to index into the list until list.size()-1. Then simply add the last element from the initial list to the new list.

List<Integer> list = Arrays.asList(1, 2, 3, 4);
List<Integer> resultSet = IntStream.range(0,list.size()-1)
                          .map(i -> list.get(i) - 2)
                          .boxed().collect(Collectors.toCollection(ArrayList::new));
resultSet.add(list.get(list.size()-1));

Try this.

List<Integer> list = Arrays.asList(1, 2, 3, 4);
int last = list.size() - 1;
List<Integer> result = IntStream.range(0, list.size())
    .mapToObj(i -> i < last ? list.get(i) - 2 : list.get(i))
    .collect(Collectors.toList());
System.out.println(result);

If you don’t want to deal with mutable lists or mutable data structures in general, one way to solve this task, is

List<Integer> result = Stream.concat(
        list.subList(0, list.size()-1).stream().map(i -> i - 2),
        Stream.of(list.get(list.size()-1)))
    .collect(Collectors.toList());

You have to do that manually:

List<Integer> list = Arrays.asList(1, 2, 3, 4);
List<Integer> mapped = list.stream().map(i -> i - 2).collect(Collectors.toCollection(ArrayList::new));
// Do this only if not empty...
mapped.remove(mapped.size()-1);
mapped.add(list.get(list.size()-1));

Another perhaps more elegant solution:

List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
Integer last = list.remove(list.size() - 1);
list.replaceAll(i -> i - 2);
list.add(last);

Another variant, just for fun:

List<Integer> list = Arrays.asList(1, 2, 3, 4);
final int[] count = {0};
List<Integer> collect2 = list.stream()
    .map(i -> (++count[0] < list.size()) ? i - 2 : i)
    .collect(Collectors.toCollection(ArrayList::new));

StreamEx provides the extract API for this question:

StreamEx.of(1, 2, 3, 4).mapLastOrElse(i -> i - 2, Function.identity()).toList();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!