博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java8新特性练手--从菜鸟教程中
阅读量:5278 次
发布时间:2019-06-14

本文共 6074 字,大约阅读时间需要 20 分钟。

package com.fh.jdk8;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import org.junit.Test;public class Learning01 {    @Test    public void sort() {        List
list = new ArrayList<>(); list.add("feng"); list.add("hao"); list.add("haha"); list.add("xixi"); sort7(list); sort8(list); } public void sort7(List
list) { Collections.sort(list, new Comparator
() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); for (String string : list) { System.out.println(string); } } public void sort8(List
list) { Collections.sort(list, (o1,o2) -> o1.compareTo(o2)); list.forEach(demo ->{ System.out.println(demo); }); } /** * lambda表达式学习--闭包 */ @Test public void lambda() { Learning01 instance = new Learning01(); MathOperation add = (int a,int b) -> a+b; MathOperation sub = (a,b) ->a-b; MathOperation mul = (a,b) ->{
return a * b;}; MathOperation div = (a,b) -> a/b; System.out.println(instance.operator(1, 2, add)); GreetingService service = (message) -> System.out.println("hello "+message); service.sayMessgae("hahah"); } public int operator(int a,int b,MathOperation mathOperation) { return mathOperation.operation(a, b); } interface MathOperation { int operation(int a,int b); } interface GreetingService { void sayMessgae(String message); } @Test public void test22() { Person person = new Man(); person.print(); }}
package com.fh.jdk8;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.LocalTime;import java.time.Month;import java.time.ZoneId;import java.time.ZonedDateTime;import java.util.Arrays;import java.util.Base64;import java.util.IntSummaryStatistics;import java.util.List;import java.util.Random;import java.util.UUID;import java.util.stream.Collectors;import org.junit.Test;/** * Stream API test * @author Administrator * */public class Learning02 {    @Test    public void test01() {        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); }); } @Test public void test02() { Random random = new Random(); random.ints().limit(1).forEach(System.out::println); } @Test public void test03() { 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); }); } @Test public void test04() { List
list = Arrays.asList("ha","niin","hhh","jj","ll"); String string = list.parallelStream().findFirst().get(); System.out.println(string); } @Test public void test05() { List
list = Arrays.asList("ha","niin","hhh","jj","ll"); String string = list.parallelStream().filter(demo ->!demo.isEmpty()).collect(Collectors.joining(",")); System.out.println(string); } @Test public void test06() { 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()); } @Test public void test07() { 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); } @Test public void test08() { 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); } @Test public void test09() { 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 = new StringBuilder(); 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(); } }}

 

转载于:https://www.cnblogs.com/nihaofenghao/p/9341962.html

你可能感兴趣的文章
Redis的安装与使用
查看>>
P1970 花匠
查看>>
java语言与java技术
查看>>
NOIP2016提高A组五校联考2总结
查看>>
iOS 项目的编译速度提高
查看>>
table中checkbox选择多行
查看>>
Magento开发文档(三):Magento控制器
查看>>
性能调优攻略
查看>>
ie6解决png图片透明问题
查看>>
瞬间的永恒
查看>>
2019-8-5 考试总结
查看>>
JS中实现字符串和数组的相互转化
查看>>
web service和ejb的区别
查看>>
Windows Azure Cloud Service (29) 在Windows Azure发送邮件(下)
查看>>
CS61A Efficiency 笔记
查看>>
微信上传素材返回 '{"errcode":41005,"errmsg":"media data missing"}',php5.6返回
查看>>
div或者p标签单行和多行超出显示省略号
查看>>
Elasticsearch 滚动重启 必读
查看>>
Hadoop基本概念
查看>>
java.util.zip压缩打包文件总结一:压缩文件及文件下面的文件夹
查看>>