forked from sumeet-automation/selenium-java-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion35.java
More file actions
36 lines (28 loc) · 1.03 KB
/
Copy pathQuestion35.java
File metadata and controls
36 lines (28 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package string;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Integer.parseInt;
public class Question35 {
public static void main(String[] args) {
String word = "abds323a47b5a";
System.out.println(sumAllNumbersWithoutLambda(word));
System.out.println(sumAllNumbersLambda(word));
String word2 = "abds323a47b5apop";
Matcher matcher = Pattern.compile("(\\d+)|(pop)").matcher(word2);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
private static int sumAllNumbersWithoutLambda(String word) {
Matcher matcher = Pattern.compile("(\\d+)").matcher(word);
int sum = 0;
while(matcher.find()){
sum += parseInt(matcher.group(0));
}
return sum;
}
private static int sumAllNumbersLambda(String word) {
return Pattern.compile("(\\d+)").matcher(word).results()
.mapToInt(matchResult -> parseInt(matchResult.group(0))).sum();
}
}