Java8新特性(部分)

-----****************************************-----
-----*****-----Lambda 匿名函数/闭包-----*****-----
-----****************************************-----


=====Java8新特性(Lambda) -- 示例1(Runnable)=====
System.out.println("\n==========Java8新特性(Lambda) -- 示例1(Runnable)==========");
        // Runnable之前:
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Java8之前写法--四行代码");
            }
        }).start();
        // Java8新特性:
        new Thread(() -> System.out.println("Java8-Lambda新特性--一行代码")).start();




=====Java8新特性(Lambda) -- 示例2(事件监听)=====
        System.out.println("\n==========Java8新特性(Lambda) -- 示例2(事件监听)==========");
        JButton show = new JButton("Show");
        // Java8之前:
        show.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("事件监听java8之前 = [" + e + "]");
            }
        });
        // Java8新特性
        show.addActionListener((e) -> System.out.println("事件监听java8新特性 = [" + e + "]"));




=====Java8新特性(Lambda) -- 示例3(Collections.sort(list,new Comparator<Object>()))=====
        System.out.println("\n==========Java8新特性(Lambda) -- 示例3(Collections.sort(list,new Comparator<Object>()))==========");
        ArrayList<TestEntity> list = new ArrayList<>();
        TestEntity en1 = new TestEntity();
        en1.setId("1001");
        en1.setName("name1");
        TestEntity en2 = new TestEntity();
        en2.setId("1002");
        en2.setName("name2");
        TestEntity en3 = new TestEntity();
        en3.setId("1003");
        en3.setName("name3");
        list.add(en2);list.add(en3);list.add(en1);
        // Java8之前:
        Collections.sort(list, new Comparator<TestEntity>() {
            @Override
            public int compare(TestEntity o1, TestEntity o2) {
                return o1.getId().compareTo(o2.getId());
            }
        });
        // Java8新特性:
        Collections.sort(list,(o1,o2) -> Integer.valueOf(o1.getId()).compareTo(Integer.valueOf(o2.getId())));
        for(TestEntity x : list){
            System.out.println("x = [" + x.getId() + "]");
        }




=====Java8新特性(Lambda) -- 示例4(对列表进行迭代)=====
        System.out.println("\n==========Java8新特性(Lambda) -- 示例4(对列表进行迭代)==========");
        List<String> arrs = Arrays.asList("list1", "list2", "list3", "list4", "list5");
        // Java8之前:
        for(String a : arrs){
            System.out.println("Java8之前 = [" + a + "]");
        }
        // Java8新特性:
        arrs.forEach(n -> System.out.println("Java8新特性 = [" + n + "]"));
        arrs.forEach(System.out::println); // 方法引用由::双冒号操作符标示。




=====Java8新特性(Lambda) -- 示例5(函数式接口Predicate)=====
        System.out.println("\n==========Java8新特性(Lambda) -- 示例5(函数式接口Predicate)==========");
        List<String> arr = Arrays.asList("Java", "PHP", "Python", "Echars");
        filter(arr,(str) -> str.equals("Java"));
        // Predicate提供类似于逻辑操作符AND和OR的方法,名字叫做and()、or()和xor(),用于将传入 filter() 方法的条件合并起来
        Predicate<String> startsWithJ = (n) -> n.startsWith("J");
        Predicate<String> fourLetterLong = (n) -> n.length() == 4;
        arr.stream()
                .filter(startsWithJ.and(fourLetterLong))
                .forEach((n) -> System.out.print("nName, which starts with 'J' and four letter long is : " + n));




// 过滤
public static void filter(List<String> names, Predicate predicate) {
            names.forEach((name) -> {
                if (predicate.test(name)) {
                    System.out.println("过滤后 = [" + name + "]");
                }
           });
        }




=====Java零时插入一个函数Stream=====
        System.out.println("\n==========Java零时插入一个函数Stream==========");
        arr.stream().filter(f -> f.length() > 3).forEach(r -> System.out.println("r = [" + r + "]"));




=====Java8新特性(Lambda) -- 示例6(Lambda表达式的Map和Reduce)=====
        System.out.println("\n==========Java8新特性(Lambda) -- 示例6(Lambda表达式的Map和Reduce)==========");
        List<Integer> arr2 = Arrays.asList(100, 200, 300, 400, 500, 600);
        // Java8之前:
        for(Integer x : arr2){
            double price = x +.12 * x;
            System.out.println("Java8之前: = [" + price + "]");
        }
        // Java8新特性:
        arr2.stream().map((price) -> price + .12 * price).forEach(x -> System.out.println("Java8新特性 = [" + x + "]"));
        // reduce() 方法计算总和
        Double aDouble = arr2.stream().map((price) -> price + .12 * price).reduce((sum, price) -> sum + price).get();
        System.out.println("aDouble = [" + aDouble + "]");




=====Java8新特性(Lambda) -- 示例7(对列表的每个元素应用函数)=====
        System.out.println("\n==========Java8新特性(Lambda) -- 示例7(对列表的每个元素应用函数)==========");
        List<String> arr3 = Arrays.asList("JAVA", "PHP", "Python", "Echarts", "Hadoop");
        String collect = arr3.stream().map((x) -> x.toLowerCase()).collect(Collectors.joining("','","'","'"));
        System.out.println("collect = [" + collect + "]");




=====Java8新特性(Lambda) -- 示例8(利用流的distinct() 方法来对集合进行去重)=====
        System.out.println("\n==========Java8新特性(Lambda) -- 示例8(利用流的distinct() 方法来对集合进行去重)==========");
        List<Integer> arr4 = Arrays.asList(2, 3, 5, 6, 5, 6, 4, 5, 8, 5, 6, 9, 5, 9);
        List<Integer> collect1 = arr4.stream().map(n -> n).distinct().collect(Collectors.toList());
        collect1.forEach(x -> System.out.println("distinct去重 = [" + x + "]"));




/*******************************************
*****-----Java8新特性(方法引用)-----*****
*******************************************/


        Person person1 = new Person("小一","2017-10-22");
        Person person2 = new Person("小二","2016-10-22");
        Person person3 = new Person("小三","2015-10-22");
        Person person4 = new Person("小四","2014-10-22");
        Person person5 = new Person("小四","2014-10-22");
        List<Person> persons = Arrays.asList(person1, person2, person3, person4,person5);
        // 普通写法
        Collections.sort(persons, new PersonComparator());
        persons.forEach(person -> System.out.println(person.getBirthday()));
        // java8新特性(方法引用)
        persons.sort(Person::comparator);
        persons.forEach(person -> System.out.println("Java8新特性 = [" + person.getBirthday() + "]"));
        Set<Person> people = new TreeSet<>((o1,o2) -> o1.getName().compareTo(o2.getName()));
        people.addAll(persons);
        ArrayList<Person> arr5 = new ArrayList<>(people);
        arr5.forEach(array -> System.out.println("TreeSet去重 = [" + array.getBirthday() + "]"));




/*******************************************
*****-----Java8新特性(Stream)-----*****
*******************************************/


        System.out.println("\n==========Java8新特性(Stream) --(summaryStatistics)==========");
        List<Integer> arr6 = Arrays.asList(5, 9, 6, 4, 8, 7, 2, 1);
        IntSummaryStatistics isstics = arr6.stream().mapToInt(n -> n).summaryStatistics();
        System.out.println("个数 = [" + isstics.getCount() + "]");
        System.out.println("最大 = [" + isstics.getMax() + "]");
        System.out.println("最小 = [" + isstics.getMin() + "]");
        System.out.println("总数 = [" + isstics.getSum() + "]");
        System.out.println("平均数 = [" + isstics.getAverage() + "]");




/***********************************************
*****-----Java8新特性(日期时间 API)-----*****
***********************************************/


        LocalDateTime dateNow = LocalDateTime.now();
        int year = dateNow.getYear();
        Month month = dateNow.getMonth();
        DayOfWeek week = dateNow.getDayOfWeek();
        int minute = dateNow.getMinute();
        int hour = dateNow.getHour();


        System.out.println("dateNow = [" + dateNow + "]");
        System.out.println("year = [" + year + "]");
        System.out.println("month = [" + month + "]");
        System.out.println("week = [" + week + "]");
        System.out.println("minute = [" + minute + "]");
        System.out.println("hour = [" + hour + "]");
        // 解析字符串为时间
        LocalDateTime parse = LocalDateTime.parse("2017-12-01T16:50:02.415");
        System.out.println("parse = [" + parse + "]");


        // 获取时区
        Set<String> zoneId = ZoneId.getAvailableZoneIds();
        zoneId.forEach(zone -> System.out.println("时区 = [" + zone + "]"));




/***********************************************
   *****-----Java8新特性(Base64)-----*****
***********************************************/


        String name = "zhangmingliang";
        try {
            // 基本编码
            String encoder = Base64.getEncoder().encodeToString(name.getBytes("utf-8"));
            System.out.println("encoder = [" + encoder + "]");
            // 解码
            byte[] decoder = Base64.getDecoder().decode(encoder);
            System.out.println("decoder = [" + new String(decoder) + "]");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值