How to do substring in some elements of string list using lambda
Asked Answered
F

4

8

Below is my list of String.

["sunday", "monday", "tuesday", "wednesday", "thurs", "fri", "satur"]

I want to do remove "day" from the elements, if it is ending with "day".

Expected Output in the list:

["sun", "mon", "tues", "wednes", "thurs", "fri", "satur"]

How to do this using Lambda?

I have tried the below code, but was unable to assign the value to the list:

daysList.stream().forEach(s -> {
    if (s.endsWith("day")) {
        s = s.substring(0, s.indexOf("day"));
    }
});
Fluidize answered 29/10, 2015 at 13:5 Comment(4)
How about replacing day at the end with nothing, i.e. apply String#replaceAll("day$", "") to all elements. Just an idea how to get rid of the condition. If you can't write the changed elements back to the list (I don't know that API that well yet but "forEach" indicates it is not possible) you could collect them into a new list.Charmer
why don't you simply iterate over the list with a for loop?Jammie
@Thanigaiarasu I have done the same thing, you can see that in question. my question is after this operation my list should have elements without "day"Fluidize
@Jammie Because he wants to know how to do this using a lambda?Nuncia
S
16

Most of the answers here make use of a Stream, but you should not be using a Stream at all:

daysList.replaceAll(s -> s.replaceFirst("day$", ""));
Scarification answered 29/10, 2015 at 13:23 Comment(0)
S
8

Iterating over the stream using forEach won't work; you need to map each of the elements you modified to a new stream using map. Then you can collect the results back to your list.

daysList = daysList.stream()
        .map(s -> s.endsWith("day") ? s.substring(0, s.length()-3) : s)
        .collect(Collectors.toList());
Sipper answered 29/10, 2015 at 13:14 Comment(5)
Funny, my solution looked like yours :D. So I don't need to post it.Rejoin
I think your s.substring(0, s.indexOf("day")) would be better written as s.substring(0, s.length()-3); that way you don't run the risk of hitting an earlier occurrence of "day" in the string.Effective
@Effective Or better yet lastIndexOf. But thanks for the heads up.Sipper
@Sipper better yet why? you're just performing an extra search that you already know the result of.Effective
@Effective You have a point. I was thinking more in terms of readability as if it would be easier to note that the range of the substring ends at the index of the last occurrence of "day". But like you said, that's already known, and that seems like a better solution as well.Sipper
S
1
List<String> daysList = Arrays.asList("sunday", "monday", 
                                      "tuesday", "wednesday",
                                       "fri", "satur" 
                                      );


List<String> res = daysList.stream()
                            .map(s -> s.replace("day",""))
                            .collect(Collectors.toList());
Stripy answered 29/10, 2015 at 13:23 Comment(0)
S
1

Sub String with decimal points sorting .

String arr[]= {"2.4","4.2","3.1","5.7","3.0","2.01","7.06","6.003"};

List newList =Arrays.asList(arr); newList.stream().map(n -> n.substring(n.indexOf('.')) ).collect(Collectors.toList()).stream().sorted().forEach(n -> System.out.println(n));;

Swerve answered 12/7, 2018 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.