I'm looking for efficient way to replace specific char from a list of email addresses if the char matches in any index of the array. But it should return the full array list.
my current code:
List<String> emailList = new ArrayList<>();
emailList.addAll(Arrays.asList("[email protected]","[email protected]","[email protected]"));
List<String> updatedEmail = emailList.stream().filter(a->a.contains("+")).
map(s -> s.replaceAll("\\+", ""))
.collect(Collectors.toList());
emailList.stream().filter(a->!a.contains("+")).forEach(b->updatedEmail.add(b));
updatedEmail.stream().forEach(b->System.out.println("email: "+b));
Console:
email: [email protected]
email: [email protected]
email: [email protected]
s.replace("+", "")is better than replaceAll as it avoids regex each call so it is justemailList.stream().map(s -> s.replace("+", "")).forEach(System.out::println)oremailList.stream().map(s -> s.replace("+", "")).toList()[email protected]refers to just[email protected], so the suffix behind the+can be used as label. However, they're not the same as[email protected], which would be the result of your string replacement.