Java8-常用总结-学习

package com.demo.java8.study;

import com.demo.java8.entity.Student;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @ClassName: Study
 * @Author: deng
 * @Description:
 * @Date: 18-12-27 下午1:56
 */
public class Study {

    @Test
    public void testLambda() {
        List<Student> list = new ArrayList<>();
        list.add(new Student(1, "小明", 18, "男"));
        list.add(new Student(2, "小华", 19, "男"));
        list.add(new Student(3, "小红", 17, "女"));
        list.add(new Student(4, "小黑", 12, "男"));
        list.add(new Student(5, "小白", 11, "女"));
        //声明式 不写大括号只写单挑语句
        list.sort((Student p, Student p1) ->p.getAge().compareTo(p1.getAge()));
//        Collections.sort(list, (a, b) -> a.getAge().compareTo(b.getAge()));
        list.forEach(System.out::println);
        System.out.println();
        //不声明式,使用大括号,可以写多条语句
        list.sort((a, b) -> {
            return a.getAge().compareTo(b.getAge());
        });
        list.forEach(System.out::println);
        System.out.println();
        //线程写法
        new Thread(()-> {
            //方法体
            System.out.println("线程启动了");
        }).start();

        Map<Integer, Student> map = new HashMap<>();


    }

    @Test
    public void testStream() {
        List<Student> list = new ArrayList<>();
        list.add(new Student(1, "小明", 18, "男"));
        list.add(new Student(2, "小华", 19, "男"));
        list.add(new Student(3, "小红", 17, "女"));
        list.add(new Student(4, "小黑", 12, "男"));
        list.add(new Student(5, "小白", 11, "女"));
        /**
         *java8 流操作  分三步走
         * 第一步转化流  eg: list.stream()
         * 第二部中间操作, eg : filter(),sorted(),map(),distinct(),limit(),skip()
         * 第三部终端操作,你想把你中间操作完后的新的流作为什么  或者成为什么,
         * eg: collect(Collections.toList/Collections.toSet/Collections.toMap/Collections.joining),
         *     count(),reduce(),findAny(),findFirst(),anyMatch(),allMatch(),noneMatch()
         *
         */
        list.stream()
                .sorted((Student s1, Student s2) -> s1.getAge() - s2.getAge())
                .collect(Collectors.toList())
                .forEach(System.out::println);
        System.out.println();
        list.stream()
                .sorted((Student s1, Student s2) -> s1.getAge() - s2.getAge())
                .filter(student -> student.getAge() > 12 && student.getSex().equals("男"))
                .collect(Collectors.toList())
                .forEach(System.out::println);
        System.out.println();
        list.stream()
                .map(Student::getAge)
                .filter(age -> age > 12)
                .collect(Collectors.toList())
                .forEach(System.out::println);
        System.out.println();

        String collect = list.stream()
                .map(Student::getName)
                .collect(Collectors.joining(","));
        System.out.println(collect);
        System.out.println();
        list.forEach(System.out::println);
        System.out.println();
        System.out.println("当前日期:" + LocalDate.now() +
                "\t" + "当前时间:" + LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")) +
                "\t" +"当前年:" + LocalDate.now().getYear() +
                "\t" +"当前月:" + LocalDate.now().getMonthValue() +
                "\t" +"当前日:" + LocalDate.now().getDayOfMonth());
        System.out.println();
        Map<String, List<Student>> map = list.stream()
                .collect(Collectors.groupingBy(Student::getName));
        map.forEach((k, v) -> {
            System.out.println(k + "===" + v);
        });
    }

    @Test
    public void testListTransforToMap() {
        List<Student> list = new ArrayList<>();
        list.add(new Student(1, "小明", 18, "男"));
        list.add(new Student(1, "小呆", 18, "男"));
        list.add(new Student(1, "小dai", 18, "男"));
        list.add(new Student(2, "小华", 19, "男"));
        list.add(new Student(3, "小红", 17, "女"));
        list.add(new Student(4, "小黑", 12, "男"));
        list.add(new Student(5, "小白", 11, "女"));
        Map<Integer, List<Student>> map = list.stream()
                .collect(Collectors.groupingBy(Student::getId));
        map.forEach((k, v) -> {
            System.out.println(k + "=" + v);
        });

        System.out.println();

        /**
         * 注意:list 转 map
         * toMap 如果集合对象有重复的key,会报错Duplicate key ....
         * Student1,Student2,Student3的id都为1。
         * 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
         */
        Map<Integer, Student> collect = list.stream()
                .collect(Collectors.toMap(Student::getId, student -> student, (k1, k2) -> k1));
        collect.forEach((k, v) -> {
            System.out.println(k + "=" +v);
        });

        System.out.println();
        List<Student> collect1 = collect.entrySet()
                .stream()
                .map(st -> st.getValue())
                .collect(Collectors.toList());
        collect1.forEach(student -> {
            System.out.println(student);
        });

    }

    static class Student {
        private Integer id;
        private String name;
        private Integer age;
        private String sex;

        public Student(Integer id, String name, Integer age, String sex) {
            super();
            this.id = id;
            this.name = name;
            this.age = age;
            this.sex = sex;
        }

        public Student(){
            super();
        }
        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", sex='" + sex + '\'' +
                    '}';
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值