Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

noX functional program

Hi,

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;
}

Open in new window



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	

Open in new window


how to fix and improve my code, please advise
Avatar of ste5an
ste5an
Flag of Germany image

Same as the other thread: Sort out your tags. Java or .NET?
SOLUTION
Avatar of no body
no body

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

how to do using lamda expression?
Avatar of gudii9

ASKER

something like

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());
    }	
}

Open in new window

Avatar of gudii9

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial