-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamAPI.java
56 lines (39 loc) · 1.45 KB
/
StreamAPI.java
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
52
53
54
55
56
package test;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamAPI {
public static void main(String[] args) {
String []words= {"ab", "bc", "cd", "ef"};
int []numbers= {3,2,4,7,6};
List<String> al=Arrays.asList(words);
String str="helXZlo";
// 1-String reverse sort
// 1-
char[] arr=str.toCharArray();
Arrays.sort(arr);
String ans1=new StringBuilder(new String(arr)).reverse().toString();
// 2-
String ans2=Stream.of(str.split(""))
.sorted(Comparator.reverseOrder())
.collect(Collectors.joining());
// String count
long count = Stream.of(words).count();
long count2 = Arrays.asList(words).stream().count();
// max, min
Optional<String> max = Stream.of(words).max(String::compareToIgnoreCase);
System.out.println("max: "+max.get());
// filter
List<String> filter1 = al.stream().filter(f -> f.equals("ab")).collect(Collectors.toList());
String []filter2 = al.stream().filter(f -> f.contains("b")).toArray(String[]::new);
for(String a: filter1) System.out.println("filter: "+a);
// unique value
Object[]unique = al.stream().distinct().toArray();
System.out.println("unique");
for(Object a: unique) System.out.print(a+",");
System.out.println();
// join
String join1= al.stream().collect(Collectors.joining("#"));
System.out.println("j: "+join1);
}
}