packagecom.fh.jdk8;importjava.time.LocalDate;importjava.time.LocalDateTime;importjava.time.LocalTime;importjava.time.Month;importjava.time.ZoneId;importjava.time.ZonedDateTime;importjava.util.Arrays;importjava.util.Base64;importjava.util.IntSummaryStatistics;importjava.util.List;importjava.util.Random;importjava.util.UUID;importjava.util.stream.Collectors;importorg.junit.Test;/*** Stream API test
*@authorAdministrator
**/
public classLearning02 {
@Testpublic voidtest01() {
List list = Arrays.asList("1","2","3","","4","5","6");
List collect = list.stream().filter(demo ->!demo.isEmpty()).collect(Collectors.toList());
collect.forEach(demo->{
System.out.println(demo);
});
}
@Testpublic voidtest02() {
Random random= newRandom();
random.ints().limit(1).forEach(System.out::println);
}
@Testpublic voidtest03() {
List asList = Arrays.asList(2,2,3,4,5,6,7);
List collect = asList.stream().map(demo -> demo*demo).distinct().collect(Collectors.toList());
collect.forEach(demo->{
System.out.println(demo);
});
System.out.println("*********");
collect.stream().filter(demo-> demo>5).collect(Collectors.toList()).forEach(demo->{
System.out.println(demo);
});
}
@Testpublic voidtest04() {
List list = Arrays.asList("ha","niin","hhh","jj","ll");
String string=list.parallelStream().findFirst().get();
System.out.println(string);
}
@Testpublic voidtest05() {
List list = Arrays.asList("ha","niin","hhh","jj","ll");
String string= list.parallelStream().filter(demo ->!demo.isEmpty()).collect(Collectors.joining(","));
System.out.println(string);
}
@Testpublic voidtest06() {
List list = Arrays.asList(2,3,4,5,6,6,7,8,8,8,8,8);
IntSummaryStatistics summer= list.stream().mapToInt(demo ->demo).summaryStatistics();
System.out.println("average:"+summer.getAverage());
System.out.println("count:"+summer.getCount());
System.out.println("max:"+summer.getMax());
System.out.println("min:"+summer.getMin());
System.out.println("sum:"+summer.getSum());
}
@Testpublic voidtest07() {
LocalDateTime currentTime=LocalDateTime.now();//System.out.println(currentTime);//System.out.println(currentTime.toLocalDate());//System.out.println(currentTime.toLocalTime());
Month month =currentTime.getMonth();int day =currentTime.getDayOfMonth();int second =currentTime.getSecond();//System.out.println("月:"+month+"天:"+day+"秒:"+second);
LocalDateTime withYear= currentTime.withDayOfMonth(10).withYear(2018);//System.out.println("withDayOfMonth:"+withYear);
/*2018-07-20T15:18:33.054
2018-07-20
15:18:33.054
月:JULY天:20秒:33
withDayOfMonth:2018-07-10T15:18:33.054*/LocalDate of= LocalDate.of(2018, Month.DECEMBER, 20);
System.out.println(of);
LocalTime times= LocalTime.of(12, 12);
System.out.println(times);
LocalTime parse= LocalTime.parse("15:22:22");
System.out.println(parse);
}
@Testpublic voidtest08() {
ZonedDateTime parse= ZonedDateTime.parse("2015-12-03T10:15:30+05:30[Asia/Shanghai]");
System.out.println(parse);
ZoneId of= ZoneId.of("Europe/Paris");
System.out.println(of);
ZoneId systemDefault=ZoneId.systemDefault();
System.out.println(systemDefault);
}
@Testpublic voidtest09() {try{//编码
String encodeToString = Base64.getEncoder().encodeToString("run?java8".getBytes("utf-8"));
System.out.println(encodeToString);//解码
byte[] decode =Base64.getDecoder().decode(encodeToString);
System.out.println(new String(decode,"utf-8"));
String urlEncoder= Base64.getUrlEncoder().encodeToString("http://baidu.com".getBytes("utf-8"));
System.out.println(urlEncoder);
StringBuilder builder= newStringBuilder();for (int i = 0; i < 20; i++) {
builder.append(UUID.randomUUID().toString());
}
String mime= Base64.getMimeEncoder().encodeToString(builder.toString().getBytes("utf-8"));
System.out.println(mime);
}catch(Exception e) {
e.printStackTrace();
}
}
}