Skip to content

Commit f295318

Browse files
committed
Lambda表达式
1 parent c93d746 commit f295318

File tree

9 files changed

+268
-0
lines changed

9 files changed

+268
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
.idea/
3+
java-java8-learning.iml

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
<groupId>com.xu</groupId>
88
<artifactId>java-java8-learning</artifactId>
99
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>1.8</source>
17+
<target>1.8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
1022

1123

1224
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.xu.java8.interface_new;
2+
3+
/**
4+
* 接口的默认方法
5+
*/
6+
interface Formula {
7+
double calculate(int a);
8+
9+
default double sqrt(int a) {
10+
return Math.sqrt(a);
11+
}
12+
}
13+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.xu.java8.lambda;
2+
3+
/**
4+
* Class 接口的默认方法
5+
*
6+
* @author hua xu
7+
* @version 1.0.0
8+
* @date 16/08/30
9+
*/
10+
public class InterfaceT {
11+
12+
/**
13+
* Java 8允许我们给接口添加一个非抽象的方法实现,只需要使用 default关键字即可,这个特征又叫做扩展方法
14+
*/
15+
interface Formula {
16+
double calculate(int a);
17+
18+
default double sqrt(int a) {
19+
return Math.sqrt(positive(a));
20+
}
21+
22+
static int positive(int a) {
23+
return a > 0 ? a : 0;
24+
}
25+
}
26+
27+
public static void main(String[] args) {
28+
Formula formula1 = new Formula() {
29+
@Override
30+
public double calculate(int a) {
31+
return sqrt(a * 100); //默认方法sqrt将在子类上可以直接使用
32+
}
33+
};
34+
System.out.println(formula1.calculate(100));
35+
System.out.println(formula1.sqrt(-23));
36+
System.out.println(Formula.positive(-4));
37+
38+
}
39+
}
40+
41+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.xu.java8.lambda;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Class lambda表达式
7+
*
8+
* @author hua xu
9+
* @version 1.0.0
10+
* @date 16/08/30
11+
*/
12+
public class Lambda1 {
13+
public static void main(String args[]){
14+
List<String> names= Arrays.asList("peter", "anna", "mike", "xenia");
15+
16+
/**java8以前的字符串排列,创建一个匿名的比较器对象Comparator然后将其传递给sort方法*/
17+
Collections.sort(names, new Comparator<String>() {
18+
@Override
19+
public int compare(String a, String b) {
20+
return b.compareTo(a);
21+
}
22+
});
23+
/**java8使用lambda表达式就不需要匿名对象了*/
24+
Collections.sort(names,(String a,String b)->{return b.compareTo(a);});
25+
26+
/**对于函数体只有一行代码的,你可以去掉大括号{}以及return关键字*/
27+
Collections.sort(names,(String a,String b)->b.compareTo(a));
28+
29+
/**Java编译器可以自动推导出参数类型,所以你可以不用再写一次类型*/
30+
Collections.sort(names, (a, b) -> b.compareTo(a));
31+
32+
System.out.println(names);
33+
34+
names.sort(Collections.reverseOrder());
35+
36+
System.out.println(names);
37+
38+
List<String> names2 = Arrays.asList("peter", null, "anna", "mike", "xenia");
39+
names2.sort(Comparator.nullsLast(String::compareTo));
40+
System.out.println(names2);
41+
42+
List<String> names3 = null;
43+
44+
Optional.ofNullable(names3).ifPresent(list -> list.sort(Comparator.naturalOrder()));
45+
46+
System.out.println(names3);
47+
}
48+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.xu.java8.lambda;
2+
3+
/**
4+
* Class 函数式接口,方法,构造器
5+
*
6+
* @author hua xu
7+
* @version 1.0.0
8+
* @date 16/08/30
9+
*/
10+
public class Lambda2 {
11+
12+
/**
13+
* 每一个lambda表达式都对应一个类型,通常是接口类型。而“函数式接口”是指仅仅只包含一个抽象方法的接口,
14+
* 每一个该类型的lambda表达式都会被匹配到这个抽象方法。
15+
* 因为 默认方法 不算抽象方法,所以你也可以给你的函数式接口添加默认方法。我们可以将lambda表达式当作任意只包含一个抽象方法的接口类型,
16+
* 确保你的接口一定达到这个要求,你只需要给你的接口添加 @FunctionalInterface 注解,
17+
* 编译器如果发现你标注了这个注解的接口有多于一个抽象方法的时候会报错的。
18+
*/
19+
20+
@FunctionalInterface
21+
public static interface Converter<F, T> {
22+
T convert(F from);
23+
}
24+
25+
static class Something {
26+
String startsWith(String s) {
27+
return String.valueOf(s.charAt(0));
28+
}
29+
}
30+
31+
interface PersonFactory<P extends Person> {
32+
P create(String firstName, String lastName);
33+
}
34+
35+
public static void main(String[] args) {
36+
37+
/**基本的接口实现*/
38+
Converter<String, Integer> integerConverter1 = new Converter<String, Integer>() {
39+
@Override
40+
public Integer convert(String from) {
41+
return Integer.valueOf(from);
42+
}
43+
};
44+
45+
/**使用lambda表达式*/
46+
Converter<String, Integer> integerConverter2 = (from) -> Integer.valueOf(from);
47+
48+
Integer converted1 = integerConverter1.convert("123");
49+
Integer converted2 = integerConverter2.convert("123");
50+
System.out.println(converted1); // result: 123
51+
System.out.println(converted2); // result: 123
52+
53+
/**简写的lambda表达式*/
54+
Converter<String, Integer> integerConverter3 = Integer::valueOf;
55+
Integer converted3 = integerConverter3.convert("123");
56+
System.out.println(converted3); // result: 123
57+
58+
/**简写的lambda表达式:method*/
59+
Something something = new Something();
60+
Converter<String, String> stringConverter = something::startsWith;
61+
String converted4 = stringConverter.convert("Java");
62+
System.out.println(converted4); // result J
63+
64+
/**简写的lambda表达式:constructor*/
65+
PersonFactory<Person> personFactory = Person::new;
66+
Person person = personFactory.create("Peter", "Parker");//Java编译器会自动根据PersonFactory.create方法的签名来选择合适的构造函数。
67+
System.out.println(person.toString());
68+
}
69+
}
70+
71+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.xu.java8.lambda;
2+
3+
/**
4+
* Created by xu on 2016/8/30.
5+
*/
6+
public class Lambda3 {
7+
@FunctionalInterface
8+
interface Fun {
9+
void foo();
10+
}
11+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.xu.java8.lambda;
2+
3+
/**
4+
* Class Lambda作用域。
5+
*
6+
* @author hua xu
7+
* @version 1.0.0
8+
* @date 16/08/30
9+
*/
10+
public class Lambda4 {
11+
/**
12+
* 在lambda表达式中访问外层作用域和老版本的匿名对象中的方式很相似。
13+
* 你可以直接访问标记了final的外层局部变量,或者实例的字段以及静态变量。
14+
*/
15+
static int outerStaticNum;
16+
17+
int outerNum;
18+
19+
void testScopes() {
20+
int num = 1;
21+
22+
Lambda2.Converter<Integer, String> stringConverter =
23+
(from) -> String.valueOf(from + num);
24+
25+
String convert = stringConverter.convert(2);
26+
System.out.println(convert); // 3
27+
28+
Lambda2.Converter<Integer, String> stringConverter2 = (from) -> {
29+
outerNum = 13;
30+
return String.valueOf(from);
31+
};
32+
33+
String[] array = new String[1];
34+
Lambda2.Converter<Integer, String> stringConverter3 = (from) -> {
35+
array[0] = "Hi there";
36+
return String.valueOf(from);
37+
};
38+
39+
stringConverter3.convert(23);
40+
41+
System.out.println(array[0]);
42+
}
43+
public static void main(String[] args) {
44+
new Lambda4().testScopes();
45+
}
46+
}
47+
48+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.xu.java8.lambda;
2+
3+
/**
4+
* @author Benjamin Winterberg
5+
*/
6+
public class Person {
7+
public String firstName;
8+
public String lastName;
9+
10+
public Person() {
11+
}
12+
13+
public Person(String firstName, String lastName) {
14+
this.firstName = firstName;
15+
this.lastName = lastName;
16+
}
17+
18+
public String toString(){
19+
return firstName+lastName;
20+
}
21+
}

0 commit comments

Comments
 (0)