Skip to content

Commit 8460294

Browse files
committed
方法引用
1 parent f40982d commit 8460294

File tree

2 files changed

+132
-4
lines changed

2 files changed

+132
-4
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@
3838

3939
- [新的日期时间的API](java-8/src/main/java/com/diyishuai/java8/LocalDateAndTimeAndDateTime.java)
4040

41-
- [默认方法](java-8/src/main/java/com/diyishuai/java8/newinterface)
41+
- [默认/静态方法](java-8/src/main/java/com/diyishuai/java8/newinterface)
4242

43-
- 方法引用
44-
45-
- 构造器引用
43+
- [方法引用](java-8/src/main/java/com/diyishuai/java8/function/FunctionReference.java)
4644

4745
- [Optional](java-8/src/main/java/com/diyishuai/java8/optional/OptionalDemo.java)
46+
47+
- JVM的新特性
48+
49+
使用元空间`Metaspace`代替持久代(`PermGen space`),JVM参数使用`-XX:MetaSpaceSize``-XX:MaxMetaspaceSize`设置大小
50+
4851
### JDK 9
4952

5053
---
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.diyishuai.java8.function;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.function.BiFunction;
10+
import java.util.function.Function;
11+
import java.util.function.Supplier;
12+
13+
/**
14+
* @author Shea
15+
* @date 2021-01-21
16+
* @description
17+
*/
18+
public class FunctionReference {
19+
20+
public static void main(String[] args) {
21+
classConstructorFunctionReference();
22+
classStaticFunctionReference();
23+
instanceFunctionReference();
24+
}
25+
26+
27+
/**
28+
* 类构造方法引用
29+
*/
30+
private static void classConstructorFunctionReference() {
31+
System.out.println("类构造方法引用:");
32+
//构造引用
33+
// 0 无参构造
34+
Supplier<Apple> appleSupplier = Apple::new;
35+
Apple a0 = appleSupplier.get();
36+
System.out.println(a0);
37+
// 1 单参构造
38+
Function<String, Apple> appleFunction = Apple::new;
39+
Apple a1 = appleFunction.apply("1");
40+
System.out.println(a1);
41+
// 2 双参构造
42+
BiFunction<String, Double, Apple> appleBiFunction = Apple::new;
43+
Apple a2 = appleBiFunction.apply("2", 0.5);
44+
System.out.println(a2);
45+
}
46+
47+
/**
48+
* 类静态方法引用
49+
*/
50+
private static void classStaticFunctionReference() {
51+
System.out.println("类静态方法引用");
52+
List list = new ArrayList();
53+
54+
list.add(new Apple("1"));
55+
list.add(new Apple("2"));
56+
list.add(new Apple("3"));
57+
list.add(new Apple[]{new Apple("4"), new Apple("5")});
58+
59+
list.stream().forEach(Show::show);
60+
61+
}
62+
63+
64+
/**
65+
* 实例方法引用
66+
*/
67+
private static void instanceFunctionReference() {
68+
System.out.println("实例方法引用");
69+
List<Apple> list = new ArrayList();
70+
71+
list.add(new Apple("1"));
72+
list.add(new Apple("2"));
73+
list.add(new Apple("3"));
74+
75+
list.stream().map(a -> a.getId()).forEach(System.out::println);
76+
System.out.println("----");
77+
list.stream().map(Apple::getId).forEach(System.out::println);
78+
}
79+
80+
81+
}
82+
83+
@Data
84+
@AllArgsConstructor
85+
@NoArgsConstructor
86+
class Apple {
87+
88+
private String id;
89+
private double weight;
90+
private String color;
91+
92+
public Apple(String id) {
93+
this.id = id;
94+
}
95+
96+
public Apple(String id, double weight) {
97+
this.id = id;
98+
this.weight = weight;
99+
}
100+
101+
public void show(){
102+
System.out.println(this.toString());
103+
}
104+
105+
}
106+
107+
class Show {
108+
109+
public static void show(Object obj) {
110+
if (obj instanceof List) {
111+
for (Object o : (List) obj) {
112+
System.out.print(o.toString() + "\t");
113+
}
114+
System.out.println();
115+
} else if (obj instanceof Object[]) {
116+
for (Object o : (Object[]) obj) {
117+
System.out.print(o.toString() + "\t");
118+
}
119+
System.out.println();
120+
} else {
121+
System.out.println(obj.toString());
122+
}
123+
}
124+
125+
}

0 commit comments

Comments
 (0)