forked from AndrewProgramming/JavaTutorialCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamDemo.java
More file actions
51 lines (35 loc) · 1.82 KB
/
StreamDemo.java
File metadata and controls
51 lines (35 loc) · 1.82 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
50
51
package java8.stream;
import java.util.stream.Stream;
public class StreamDemo {
public static void main(String[] args) {
String[] names = {"John", "Peter", "Susan", "Kim", "Jen", "George", "Alan", "Stacy", "Michelle",
"john"};
Stream.of(names).limit(4).sorted().forEach((e) -> System.out.println(e + " "));
System.out.println();
Stream.of(names).skip(4).sorted((e1, e2) -> e1.compareToIgnoreCase(e2))
.forEach(e -> System.out.print(e + " "));
System.out.println();
Stream.of(names).skip(4).sorted(String::compareToIgnoreCase)
.forEach(e -> System.out.print(e + " "));
System.out.println(
"\nLargest string with length > 4:" + Stream.of(names).filter(e -> e.length() > 4)
.max(String::compareTo).get());
System.out.println(
"Smallest string alphabetically: " + Stream.of(names).min(String::compareTo).get());
System.out.println("Stacy is in names? " + Stream.of(names).anyMatch(e -> e.equals("Stacy")));
System.out.println("All names start with a capital letter? "
+ Stream.of(names)
.allMatch(e -> Character.isUpperCase(e.charAt(0))));
System.out.println("No name begins with Ko? "
+ Stream.of(names).noneMatch(e -> e.startsWith("Ko")));
System.out.println("Number of distinct case-insensitive strings: "
+ Stream.of(names).map(e -> e.toUpperCase())
.distinct().count());
System.out.println("First element in this stream in lowercase: "
+ Stream.of(names).map(String::toLowerCase).findFirst().get());
System.out.println("Skip 4 and get any element in this stream:"
+ Stream.of(names).skip(4).sorted().findAny().get());
Object[] namesInLowerCase = Stream.of(names).map(String::toLowerCase).toArray();
System.out.println(java.util.Arrays.toString(namesInLowerCase));
}
}