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() { Listlist = 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() { Listlist = 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(); } }}