• java8新特性练手--从菜鸟教程中


    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<String> list = new ArrayList<>();
            list.add("feng");
            list.add("hao");
            list.add("haha");
            list.add("xixi");
            sort7(list);
            sort8(list);
        }
        
        public void sort7(List<String> list) {
            Collections.sort(list, new Comparator<String>() {
    
                @Override
                public int compare(String o1, String o2) {
                    return o1.compareTo(o2);
                }
                
            });
            for (String string : list) {
                System.out.println(string);
            }
        }
        public void sort8(List<String> 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<String> list = Arrays.asList("1","2","3","","4","5","6");
            List<String> 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<Integer> asList = Arrays.asList(2,2,3,4,5,6,7);
            List<Integer> 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<String> list = Arrays.asList("ha","niin","hhh","jj","ll");
            String string = list.parallelStream().findFirst().get();
            System.out.println(string);
        }
        
        @Test
        public void test05() {
            List<String> 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<Integer> 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();
            }
        }
    
    
    }
  • 相关阅读:
    Python集成开发环境Pycharm+Git+Gitee(码云)
    【AI图像识别二】JMeter轻松实现大数据量AI图像识别接口测试
    pycharm中django同步数据库问题
    绿盟-WEB应用漏洞扫描系统
    Python脚本轻松实现批量图片重命名
    Linxu下JMeter进行接口压力测试
    依赖Anaconda环境安装TensorFlow库,避免采坑
    【AI图像识别一】人脸识别测试探索
    Postgresql死锁的处理
    PostgreSQL 9.5,带来 UPSERT 等新特性
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/9341962.html
Copyright © 2020-2023  润新知