Skip to content

Commit 2d06d0c

Browse files
author
DeleiGuo
committed
add features demo
1 parent c9fe9bd commit 2d06d0c

File tree

5 files changed

+350
-0
lines changed

5 files changed

+350
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package cn.delei.java.feature;
2+
3+
import cn.delei.util.PrintUtil;
4+
5+
import java.io.PrintWriter;
6+
import java.util.*;
7+
import java.util.function.Consumer;
8+
import java.util.function.Predicate;
9+
import java.util.stream.Stream;
10+
11+
/**
12+
* Lambda 函数编程 Demo
13+
*
14+
* @author deleiguo
15+
* @since 1.8
16+
*/
17+
public class LambdaDemo {
18+
public static void main(String[] args) {
19+
List<String> strList = Arrays.asList("delei", "guo", "Java", "PHP", "Go");
20+
List<Integer> intList = Arrays.asList(new Integer[]{10, 8, 21, 33});
21+
// ==> 构造
22+
new Thread(() -> {
23+
System.out.println(Thread.currentThread().getName() + " Start");
24+
}, "T01");
25+
26+
Collections.sort(intList, Integer::compareTo);
27+
Collections.sort(intList, (Integer i1, Integer i2) -> {
28+
return i1 - i2;
29+
});
30+
PrintUtil.printDivider("forEach");
31+
// ==> forEach
32+
strList.forEach(new Consumer<String>() {
33+
@Override
34+
public void accept(String s) {
35+
System.out.print(s + " ");
36+
}
37+
});
38+
System.out.println();
39+
strList.forEach(s -> {
40+
System.out.print(s + " ");
41+
});
42+
System.out.println();
43+
Map<Integer, Integer> map = new HashMap<>();
44+
map.put(1, 1);
45+
map.put(10, 10);
46+
map.forEach((k, v) -> System.out.println("key=" + k + " ,value=" + v));
47+
48+
PrintUtil.printDivider("方法引用");
49+
// ==> 方法引用,由::双冒号操作符标识
50+
strList.forEach(System.out::println);
51+
52+
PrintUtil.printDivider("Predicate");
53+
// ==> Predicate
54+
Predicate<String> conditon = (str) -> str.length() > 4;
55+
filter(strList, conditon);
56+
conditon = (str) -> str.startsWith("delei");
57+
filter(strList, conditon);
58+
conditon = (str) -> true;
59+
filter(strList, conditon);
60+
conditon = (str) -> false;
61+
filter(strList, conditon);
62+
}
63+
64+
static void filter(List<String> list, Predicate<String> predicate) {
65+
list.stream().filter((s) -> (predicate.test(s))).forEach((s) -> {
66+
System.out.println(s + " ");
67+
});
68+
}
69+
70+
}
71+
72+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cn.delei.java.feature;
2+
3+
/**
4+
* Lambda Functional Interface
5+
* @author deleiguo
6+
*/
7+
@FunctionalInterface
8+
public interface LambdaFunctionalInterface {
9+
void func();
10+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package cn.delei.java.feature;
2+
3+
import cn.delei.util.PrintUtil;
4+
5+
import java.time.*;
6+
import java.time.format.DateTimeFormatter;
7+
import java.time.temporal.ChronoUnit;
8+
9+
/**
10+
* LocalDate 日期类 Demo
11+
*
12+
* @author deleiguo
13+
* @since 1.8
14+
*/
15+
public class LocalDateDemo {
16+
public static void main(String[] args) {
17+
18+
// ==> Clock
19+
PrintUtil.printDivider("Clock");
20+
Clock clock = Clock.systemUTC();
21+
System.out.println(clock.millis());
22+
clock = Clock.systemDefaultZone();
23+
System.out.println("ZoneId=" + clock.getZone());
24+
clock = Clock.system(ZoneId.of("Europe/Paris")); // 巴黎时区
25+
System.out.println(clock.millis()); // 每次调用将返回当前瞬时时间(UTC)
26+
clock = Clock.system(ZoneId.of("Asia/Shanghai"));// 上海时区
27+
System.out.println(clock.millis());// 每次调用将返回当前瞬时时间(UTC)
28+
29+
// ==> Instant时间戳
30+
PrintUtil.printDivider("Instant 时间戳");
31+
Instant instant = Instant.now();
32+
System.out.println("toEpochMilli=" + instant.toEpochMilli()); //精确到毫秒
33+
System.out.println("getNano=" + instant.getNano());
34+
System.out.println("getEpochSecond=" + instant.getEpochSecond()); //精确到秒 得到相对于1970-01-01 00:00:00 UTC的一个时间
35+
System.out.println("toString=" + instant.toString());
36+
37+
// ==> 格式化
38+
PrintUtil.printDivider("格式化");
39+
LocalDate localDate = LocalDate.now();
40+
System.out.println(String.format("date now : %s", localDate));
41+
LocalDateTime localDateTime = LocalDateTime.now().withNano(0);
42+
System.out.println(String.format("time now : %s", localDateTime));
43+
localDateTime = LocalDateTime.now(ZoneId.of("Europe/Paris"));
44+
System.out.println(String.format("Europe/Paris now : %s", localDateTime));
45+
// 必须指定时区
46+
ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.now(), ZoneId.systemDefault());
47+
System.out.println(format(localDateTime));
48+
System.out.println(format(zonedDateTime));
49+
System.out.println(format(LocalDateTime.now()));
50+
51+
// ==> 字符串转日期
52+
PrintUtil.printDivider("字符串转日期");
53+
localDate = LocalDate.of(2019, 12, 20);
54+
System.out.println(" LocalDate.of: " + format(localDate));
55+
localDate = LocalDate.parse("2021-04-19");
56+
System.out.println(" LocalDate.parse: " + format(localDate));
57+
localDateTime = LocalDateTime.of(2019, 12, 20, 23, 10, 59);
58+
System.out.println(" LocalDateTime.of: " + format(localDateTime));
59+
localDateTime = LocalDateTime.parse("2021-04-19T15:54:54");
60+
System.out.println(" LocalDateTime.parse: " + format(localDateTime));
61+
62+
// ==> 日期获取
63+
PrintUtil.printDivider("日期获取");
64+
localDate = LocalDate.of(2019, 12, 20);
65+
localDateTime = LocalDateTime.of(2019, 12, 20, 23, 10, 59, 889);
66+
System.out.println("日期 " + format(localDateTime));
67+
System.out.println("年份:" + localDateTime.getYear());
68+
System.out.println("月份:" + localDateTime.getMonth());
69+
System.out.println("当年中的第多少天:" + localDateTime.getDayOfYear());
70+
System.out.println("当月的第多少天:" + localDateTime.getDayOfMonth());
71+
System.out.println("星期:" + localDateTime.getDayOfWeek());
72+
System.out.println("时:" + localDateTime.getHour());
73+
System.out.println("分:" + localDateTime.getMinute());
74+
System.out.println("秒:" + localDateTime.getSecond());
75+
System.out.println("毫秒:" + localDateTime.getNano());
76+
System.out.println("是否闰年:" + localDate.isLeapYear());
77+
78+
// ==> 日期计算
79+
PrintUtil.printDivider("日期计算");
80+
LocalDate aferCalc;
81+
localDate = LocalDate.now();
82+
System.out.println("日期 " + format(localDate));
83+
aferCalc = localDate.plusWeeks(1L);
84+
System.out.println("一周后:" + format(aferCalc));
85+
aferCalc = localDate.plusDays(20L);
86+
System.out.println("20后:" + format(aferCalc));
87+
aferCalc = localDate.minusDays(20L);
88+
System.out.println("20天前:" + format(aferCalc));
89+
aferCalc = localDate.plus(1, ChronoUnit.WEEKS);
90+
System.out.println("2周后 plus ChronoUnit:" + format(aferCalc));
91+
92+
PrintUtil.printDivider("日期相隔");
93+
LocalDate date01 = LocalDate.of(2008, 12, 25);
94+
LocalDate date02 = LocalDate.of(2019, 3, 11);
95+
System.out.println("日期01: " + format(date01));
96+
System.out.println("日期02: " + format(date02));
97+
Period period = Period.between(date01, date02);
98+
// 这里period.getDays()得到的天是抛去年月以外的天数,并不是总天数
99+
System.out.println("date01 到 date02 相隔:"
100+
+ period.getYears() + "年"
101+
+ period.getMonths() + "月"
102+
+ period.getDays() + "天");
103+
long day = date02.toEpochDay() - date01.toEpochDay();
104+
System.out.println("date01 到 date02 相隔:" + day + "天");
105+
106+
PrintUtil.printDivider("时间相隔");
107+
LocalDateTime time01 = LocalDateTime.of(2018, 12, 25, 8, 15, 46);
108+
LocalDateTime time02 = LocalDateTime.of(2019, 3, 11, 13, 10, 35);
109+
//表示两个瞬时时间的时间段
110+
System.out.println("时间01: " + format(time01));
111+
System.out.println("时间02: " + format(time02));
112+
Duration d1 = Duration.between(time01.toInstant(ZoneOffset.UTC), time02.toInstant(ZoneOffset.UTC));
113+
//得到相应的时差
114+
System.out.println(d1.toDays());
115+
System.out.println(d1.toHours());
116+
System.out.println(d1.toMinutes());
117+
System.out.println(d1.toMillis());
118+
System.out.println(d1.toNanos());
119+
}
120+
121+
static String format(LocalDate date) {
122+
final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
123+
return format.format(date);
124+
}
125+
126+
static String format(LocalDateTime time) {
127+
final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
128+
return format.format(time);
129+
}
130+
131+
static String format(ZonedDateTime zonedDateTime) {
132+
final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSZ");
133+
return format.format(zonedDateTime);
134+
}
135+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cn.delei.java.feature;
2+
3+
import cn.delei.pojo.Person;
4+
import cn.delei.util.PrintUtil;
5+
6+
import java.util.Optional;
7+
8+
/**
9+
* Optional类 使用
10+
*
11+
* @author deleiguo
12+
*/
13+
public class OptionalDemo {
14+
public static void main(String[] args) {
15+
String parameter = "deleiguo";
16+
String nullObj = null;
17+
String nullStr = "";
18+
// ==> of
19+
PrintUtil.printDivider("of");
20+
Optional<String> parameterOptional = Optional.of(parameter);
21+
Optional<String> nullObjOptional = Optional.of(nullStr);
22+
// Optional<String> nullStrOptional = Optional.of(nullObj); // throw NPE
23+
24+
// ==> ofNullable
25+
PrintUtil.printDivider("ofNullable");
26+
parameterOptional = Optional.ofNullable(parameter);
27+
nullObjOptional = Optional.ofNullable(null);
28+
Optional<String> nullStrOptional = Optional.ofNullable(nullObj);
29+
30+
// ==> isPresent
31+
PrintUtil.printDivider("isPresent");
32+
if (parameterOptional.isPresent()) {
33+
//在Optional实例内调用get()返回已存在的值
34+
System.out.println(parameterOptional.get());
35+
}
36+
37+
// ==> orElse
38+
PrintUtil.printDivider("orElse");
39+
System.out.println(parameterOptional.orElse("There is nobody"));
40+
System.out.println(nullStrOptional.orElse("There is nobody"));
41+
System.out.println(nullStrOptional.orElseGet(() -> "default person"));
42+
43+
// ==> map
44+
PrintUtil.printDivider("map");
45+
Optional<String> optional = Optional.ofNullable(parameter).map(s -> s.toUpperCase());
46+
System.out.println(optional.orElse("No value found"));
47+
48+
// ==> 高频使用方法
49+
Person delei = new Person("deleiguo", 28, "12345678");
50+
Person nullPerson = null;
51+
Optional.ofNullable(delei).map(Person::getName);
52+
}
53+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package cn.delei.java.feature;
2+
3+
import cn.delei.util.PrintUtil;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.function.Supplier;
8+
import java.util.stream.Stream;
9+
10+
/**
11+
* Stream API Demo
12+
*
13+
* @author deleiguo
14+
* @since 1.8
15+
*/
16+
public class StreamDemo {
17+
public static void main(String[] args) {
18+
List<String> strList = Arrays.asList("delei", "guo", "Java", "PHP", "Go", "JavaScript", "", "", "Java",
19+
"deleiguo", "Go", "PHP");
20+
21+
/*
22+
* 无法重用此 Stream 对象
23+
* java.lang.IllegalStateException: stream has already been operated upon or closed
24+
*
25+
* 运行如下代码会报上述错误
26+
* <code>
27+
* streamSupplier.get().forEach(System.out::println);
28+
* streamSupplier.get().forEach(System.out::println);
29+
* </code>
30+
*/
31+
Stream<String> strStream = strList.stream();
32+
33+
// 重用 Stream,使用 get 方法每次获取一个新的 stream
34+
Supplier<Stream<String>> streamSupplier = () -> strList.stream();
35+
36+
// ==> forEach
37+
PrintUtil.printDivider("forEach");
38+
streamSupplier.get().forEach(s -> {
39+
System.out.print(s + " ");
40+
});
41+
System.out.println();
42+
streamSupplier.get().forEach(System.out::print);
43+
System.out.println();
44+
45+
// ==> filter
46+
PrintUtil.printDivider("filter");
47+
long count = streamSupplier.get().filter(s -> "deleiguo".equals(s)).count();
48+
System.out.println("filter count:" + count);
49+
streamSupplier.get().filter(s -> s.startsWith("Java")).forEach(s -> {
50+
System.out.print(s + " ");
51+
});
52+
System.out.println();
53+
54+
// ==> limit
55+
PrintUtil.printDivider("limit");
56+
streamSupplier.get().limit(3).forEach(s -> {
57+
System.out.print(s + " ");
58+
});
59+
System.out.println();
60+
61+
// ==> map
62+
PrintUtil.printDivider("map");
63+
streamSupplier.get().map(s -> s + "-after").forEach(s -> {
64+
System.out.print(s + " ");
65+
});
66+
System.out.println();
67+
68+
// ==> match
69+
PrintUtil.printDivider("match");
70+
System.out.println(streamSupplier.get().anyMatch(s -> s.startsWith("Java")));
71+
System.out.println(streamSupplier.get().allMatch(s -> s.startsWith("Java")));
72+
73+
// ==> skip
74+
PrintUtil.printDivider("skip");
75+
streamSupplier.get().skip(2).forEach(s -> {
76+
System.out.print(s + " ");
77+
});
78+
System.out.println();
79+
}
80+
}

0 commit comments

Comments
 (0)