gudii9
asked on
noX functional program
Hi,
http://codingbat.com/prob/p105967
I wrote my code as below and failing some tests
how to fix and improve my code, please advise
http://codingbat.com/prob/p105967
I wrote my code as below and failing some tests
public List<String> noX(List<String> strings) {
strings.replaceAll(x -> "");
return strings;
}
Expected Run
noX(["ax", "bb", "cx"]) → ["a", "bb", "c"] ["", "", ""] X
noX(["xxax", "xbxbx", "xxcx"]) → ["a", "bb", "c"] ["", "", ""] X
noX(["x"]) → [""] [""] OK
noX([""]) → [""] [""] OK
noX([]) → [] [] OK
noX(["the", "taxi"]) → ["the", "tai"] ["", ""] X
noX(["the", "xxtxaxixxx"]) → ["the", "tai"] ["", ""] X
noX(["this", "is", "the", "x"]) → ["this", "is", "the", ""] ["", "", "", ""] X
other tests
X
how to fix and improve my code, please advise
Same as the other thread: Sort out your tags. Java or .NET?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
how to do using lamda expression?
ASKER
something like
strings.replaceAll(x -> "");
strings.replaceAll(x -> "");
Here is one way.
import java.util.*;
import java.util.stream.*;
public class X {
public static void main(String[] args) {
List<String> list = Arrays.asList("xxax", "xbxbx", "xxcx", "zzz");
System.out.println(noX(list));
}
public static List<String> noX(List<String> strings) {
return strings.stream().map(s -> s.replaceAll("x","")).collect(Collectors.toList());
}
}
ASKER
what is Colectors what is stream and collect methods here? please advise
Discussion moved to
https://www.experts-exchange.com/questions/29041669/Collectors-stream-collect-method.html
That way we all can be on the same page.
https://www.experts-exchange.com/questions/29041669/Collectors-stream-collect-method.html
That way we all can be on the same page.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.