相关系列文章参考:
http://www.importnew.com/tag/java8
1.时间戳转时间字符:
DateTimeFormatter df = DateTimeFormatter.ofPattern(Constants.DATE_YMD_HMS);
String k = df.format(LocalDateTime.ofInstant(Instant.ofEpochSecond(1535355250L), ZoneId.of("Asia/Shanghai")));
2.Optional的写法:(参考:Java8 如何正确使用 Optional)
以前:
public static String getChampionName(Competition comp) throws IllegalArgumentException {
if (comp != null) {
CompResult result = comp.getResult();
if (result != null) {
User champion = result.getChampion();
if (champion != null) {
return champion.getName();
}
}
}
throw new IllegalArgumentException("The value of param comp isn't available.");
}
现在:
public static String getChampionName(Competition comp) throws IllegalArgumentException {
return Optional.ofNullable(comp)
.map(c->c.getResult())
.map(r->r.getChampion())
.map(u->u.getName())
.orElseThrow(()->new IllegalArgumentException("The value of param comp isn't available."));
}
TestService service = testServiceList.stream().filter(x -> x.isSupport(supportBean))
.sorted(Comparator.comparing(TestService::getPriority))
.map(Optional::ofNullable)
.findFirst()
.flatMap(Function.identity())
.orElse(null);
3.forEach轮询Map对象:
Map one = Maps.newHashMap();
for(int k=0;k<10;k++) {
one.put("key" + k, "value" + k);
}
one.forEach((key,value)->{
System.err.println(key + " : " + value);
});