forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString1.java
More file actions
49 lines (42 loc) · 1.35 KB
/
String1.java
File metadata and controls
49 lines (42 loc) · 1.35 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
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.winterbe.java8.samples.misc;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author Benjamin Winterberg
*/
public class String1 {
public static void main(String[] args) {
testJoin();
testChars();
testPatternPredicate();
testPatternSplit();
}
private static void testChars() {
String string = "foobar:foo:bar"
.chars()
.distinct()
.mapToObj(c -> String.valueOf((char) c))
.sorted()
.collect(Collectors.joining());
System.out.println(string);
}
private static void testPatternSplit() {
String string = Pattern.compile(":")
.splitAsStream("foobar:foo:bar")
.filter(s -> s.contains("bar"))
.sorted()
.collect(Collectors.joining(":"));
System.out.println(string);
}
private static void testPatternPredicate() {
long count = Stream.of("bob@gmail.com", "alice@hotmail.com")
.filter(Pattern.compile(".*@gmail\\.com").asPredicate())
.count();
System.out.println(count);
}
private static void testJoin() {
String string = String.join(":", "foobar", "foo", "bar");
System.out.println(string);
}
}