Skip to content

Commit 3d524ad

Browse files
author
hyao
committed
- Test new java 8 feature
1 parent 85e77a0 commit 3d524ad

7 files changed

Lines changed: 154 additions & 1 deletion

File tree

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
package com.hong.dev.practice.java8;
22

3+
import com.hong.dev.practice.java8.capi.functionalInterface.IConverter;
4+
import com.hong.dev.practice.java8.capi.functionalInterface.ISomethingFactory;
5+
import com.hong.dev.practice.java8.internal.Calculator;
6+
import com.hong.dev.practice.java8.internal.lambda.Lambda;
7+
8+
/**
9+
* {@link http://winterbe.com/posts/2014/03/16/java-8-tutorial/}
10+
*
11+
* @author HYao
12+
*
13+
*/
314
public class Main {
415

516
public static void main(String[] args) {
617
System.out.println("Hello, World!");
7-
}
818

19+
// default method
20+
Calculator calculator = new Calculator();
21+
double res = calculator.calculate(20);
22+
System.out.println(res);
23+
24+
// lambda
25+
Lambda lambda = new Lambda();
26+
lambda.sort(lambda.names);
27+
28+
// functional interface
29+
IConverter<String, String> converter = (from) -> "new " + from;
30+
System.out.println(converter.convert("world"));
31+
32+
// method reference
33+
// the class must be located in the same package?
34+
IConverter<String, String> converter2 = Something2::startWith;
35+
System.out.println(converter2.convert("Hi"));
36+
37+
IConverter<String, Integer> converter3 = String::valueOf;
38+
System.out.println(converter3.convert(1234));
39+
40+
// constructor reference
41+
ISomethingFactory<Something> factory = Something::new;
42+
factory.create("yoo", "hoo").printThings();
43+
44+
// lambda could read the final or implicitly final local variable which
45+
// is out of the scope of the expression. For instance or static field
46+
// there is no limit.
47+
// The default methods of interface can not be accessed in lambda
48+
// expression.
49+
int i1 = 1;
50+
final int i2 = 2;
51+
IConverter<String, Integer> converter4 = (i) -> i1 + i + "";
52+
System.out.println(converter4.convert(23));
53+
// i1 = 20; // Compile error.
54+
IConverter<String, Integer> converter5 = (i) -> i2 + i * 2 + "";
55+
System.out.println(converter5.convert(89));
56+
57+
}
958
}
59+
60+
class Something2 {
61+
static String startWith(String pStr) {
62+
return String.valueOf(pStr.charAt(0));
63+
}
64+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.hong.dev.practice.java8;
2+
3+
public class Something {
4+
5+
private String thing1;
6+
private String thing2;
7+
8+
Something(String theThing, String thatThing) {
9+
this.thing1 = theThing;
10+
this.thing2 = thatThing;
11+
}
12+
13+
public void printThings(){
14+
System.out.println(getThing1() + " - " + getThing2());
15+
}
16+
17+
static String startWith(String pStr) {
18+
return String.valueOf(pStr.charAt(0));
19+
}
20+
21+
public String getThing1() {
22+
return thing1;
23+
}
24+
25+
public void setThing1(String thing1) {
26+
this.thing1 = thing1;
27+
}
28+
29+
public String getThing2() {
30+
return thing2;
31+
}
32+
33+
public void setThing2(String thing2) {
34+
this.thing2 = thing2;
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.hong.dev.practice.java8.capi.defaultMethod;
2+
3+
/**
4+
*
5+
* @author HYao
6+
*
7+
*/
8+
public interface IFormula {
9+
double calculate(int a);
10+
11+
// default method
12+
default double sqrt(int a) {
13+
return Math.sqrt(a);
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.hong.dev.practice.java8.capi.functionalInterface;
2+
3+
@FunctionalInterface
4+
public interface IConverter<T, F> {
5+
T convert(F from);
6+
default void printType() {
7+
System.out.println(this.getClass().getName());
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.hong.dev.practice.java8.capi.functionalInterface;
2+
3+
import com.hong.dev.practice.java8.Something;
4+
5+
@FunctionalInterface
6+
public interface ISomethingFactory<P extends Something> {
7+
P create(String str1, String str2);
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.hong.dev.practice.java8.internal;
2+
3+
import com.hong.dev.practice.java8.capi.defaultMethod.IFormula;
4+
5+
public class Calculator implements IFormula {
6+
7+
@Override
8+
public double calculate(int a) {
9+
return sqrt(a);
10+
}
11+
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.hong.dev.practice.java8.internal.lambda;
2+
3+
import java.net.Socket;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
public class Lambda {
9+
10+
public List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
11+
12+
public void sort(List<String> pList) {
13+
System.out.println(pList);
14+
Collections.sort(pList, (a, b) -> a.compareTo(b));
15+
System.out.println(pList);
16+
}
17+
18+
}

0 commit comments

Comments
 (0)