Skip to content

Commit f4ab4bc

Browse files
committed
Stream接口&Function
1 parent 8571c7c commit f4ab4bc

File tree

9 files changed

+188
-0
lines changed

9 files changed

+188
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.xu.java8.Function;
2+
3+
import java.util.function.BiConsumer;
4+
5+
/**
6+
* Created by xu on 2017/5/10.
7+
*/
8+
public class BiConsumerTest {
9+
public static void main(String[] args) {
10+
//return void
11+
BiConsumer<Integer,Integer> accept=(x,y)->{};
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.xu.java8.Function;
2+
3+
4+
import java.util.function.BiFunction;
5+
6+
public class BiFunctionTest {
7+
8+
public static void main(String[] args) {
9+
BiFunction<Integer, Integer, Integer> add = (x, y) -> x + y;
10+
System.out.println(add.apply(10,20));
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.xu.java8.Function;
2+
3+
import java.util.function.Consumer;
4+
5+
/**
6+
* Created by xu on 2017/5/10.
7+
*/
8+
public class ConsumerTest {
9+
public static void main(String[] args) {
10+
//return void
11+
Consumer<Integer> accept=x->{};
12+
}
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.xu.java8.Function;
2+
3+
4+
import java.util.function.Function;
5+
6+
public class FunctionTest {
7+
8+
9+
public static void main(String[] args) {
10+
11+
Function<Integer, Integer> times2 = e -> e * 2;
12+
Function<Integer, Integer> squared = e -> e * e;
13+
Function<Integer, Integer> identity = Function.identity();
14+
15+
//return 8 - > 4 * 2
16+
Integer a = times2.apply(4);
17+
System.out.println(a);
18+
//return 32 - > 4 ^ 2 * 2
19+
Integer b = times2.compose(squared).apply(4);
20+
System.out.println(b);
21+
//return 64 - > (4 * 2) ^ 2
22+
Integer c = times2.andThen(squared).apply(4);
23+
System.out.println(c);
24+
//return 4
25+
Integer d = identity.apply(4);
26+
System.out.println(d);
27+
}
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.xu.java8.Function;
2+
3+
import java.util.function.Predicate;
4+
5+
/**
6+
* Created by xu on 2017/5/10.
7+
*/
8+
public class PredicateTest {
9+
10+
public static void main(String[] args) {
11+
Predicate<String> predicate=x->x.startsWith("a");
12+
System.out.println(predicate.test("abc"));
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.xu.java8.Function;
2+
3+
import java.util.function.Supplier;
4+
5+
/**
6+
* Created by xu on 2017/5/10.
7+
*/
8+
public class SupplierTest {
9+
public static void main(String[] args) {
10+
Supplier<Integer> get= () -> 10;
11+
Integer a=get.get();
12+
System.out.println(a);
13+
}
14+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.xu.java8.Stream;
2+
3+
4+
import java.util.*;
5+
import java.util.function.*;
6+
import java.util.stream.Collectors;
7+
8+
public class Collect {
9+
10+
public static void main(String[] args) {
11+
List<String> stringCollection = new ArrayList<>();
12+
stringCollection.add("ddd2");
13+
stringCollection.add("aaa1");
14+
stringCollection.add("bbb1");
15+
stringCollection.add("aaa1");
16+
17+
System.out.println(stringCollection);
18+
19+
System.out.println(stringCollection.stream().filter(x -> x.startsWith("a")).collect(Collectors.toList()));
20+
21+
System.out.println(stringCollection.stream().filter(x -> x.startsWith("a")).collect(Collectors.toSet()));
22+
23+
List<String> list = stringCollection.stream()
24+
.filter(x -> x.startsWith("a"))
25+
.collect(Collectors.toCollection(ArrayList::new));
26+
System.out.println(list);
27+
28+
Function<String, String> xu = x -> x + "_xu";
29+
Map<String, String> map = stringCollection.stream()
30+
.filter(x -> x.startsWith("a"))
31+
.distinct()
32+
.collect(Collectors.toMap(Function.identity(), xu));
33+
System.out.println(map);
34+
35+
Predicate<String> startA = x -> x.startsWith("a");
36+
Map<Boolean, List<String>> map2 = stringCollection.stream()
37+
.collect(Collectors.partitioningBy(startA));
38+
System.out.println(map2);
39+
40+
Predicate<String> end1 = x -> x.endsWith("1");
41+
Map<Boolean, Map<Boolean, List<String>>> map3 = stringCollection.stream()
42+
.collect(Collectors.partitioningBy(startA, Collectors.partitioningBy(end1)));
43+
System.out.println("map3:" + map3);
44+
45+
Function<String, String> stringStart = x -> String.valueOf(x.charAt(0));
46+
Map<String, List<String>> map4 = stringCollection.stream()
47+
.collect(Collectors.groupingBy(stringStart));
48+
System.out.println("map4:" + map4);
49+
50+
Map<String, Long> map5 = stringCollection.stream()
51+
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
52+
System.out.println("map5:" + map5);
53+
54+
Map<String, Long> map6 = stringCollection.stream()
55+
.collect(Collectors.groupingBy(stringStart, LinkedHashMap::new, Collectors.counting()));
56+
System.out.println("map6:" + map6);
57+
58+
Supplier<List<String>> supplier = ArrayList::new;
59+
BiConsumer<List<String>, String> accumulator = List::add;
60+
BiConsumer<List<String>, List<String>> combiner = List::addAll;
61+
List<String> list1 = stringCollection.stream()
62+
.filter(x -> x.startsWith("a"))
63+
.collect(supplier, accumulator, combiner);
64+
System.out.println("list1:" + list1);
65+
66+
BinaryOperator<String> binaryOperator = (x, y) -> x + y;
67+
String reduceStr1 = stringCollection.stream().reduce(binaryOperator).orElse("");
68+
System.out.println("reduceStr1: " + reduceStr1);
69+
70+
String reduceStr2 = stringCollection.stream().reduce("start:", binaryOperator);
71+
System.out.println("reduceStr1: " + reduceStr2);
72+
73+
List<Person> personList = new ArrayList<>();
74+
personList.add(new Person(10, 20));
75+
personList.add(new Person(20, 30));
76+
personList.add(new Person(30, 50));
77+
78+
BiFunction<Person, Person, Person> biFunction = (x, y) -> new Person(x.getAge() + y.getAge(), x.getRate() + y.getRate());
79+
BinaryOperator<Person> binaryOperator1 = (x, y) -> new Person(x.getAge() + y.getAge(), x.getRate() + y.getRate());
80+
Person total = personList.parallelStream().reduce(new Person(0, 0), biFunction, binaryOperator1);
81+
System.out.println("total:"+total);
82+
}
83+
}

src/main/java/com/xu/java8/lambda/Lambda2.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public Integer convert(String from) {
6262
System.out.println(converted4); // result J
6363

6464
/**简写的lambda表达式:constructor*/
65+
Person p=new Person("xu","hua");
66+
Person p2=new Person("xu","hua","");
6567
PersonFactory<Person> personFactory = Person::new;
6668
Person person = personFactory.create("xu", "hua");//Java编译器会自动根据PersonFactory.create方法的签名来选择合适的构造函数。
6769
System.out.println(person.toString());

src/main/java/com/xu/java8/lambda/Person.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ public Person(String firstName, String lastName) {
1212
this.lastName = lastName;
1313
}
1414

15+
public Person(String firstName, String lastName,String cc) {
16+
this.firstName = firstName;
17+
this.lastName = lastName;
18+
}
19+
20+
public void add(String firstName){
21+
22+
}
23+
1524
public String toString(){
1625
return firstName+lastName;
1726
}

0 commit comments

Comments
 (0)