Java 8 collection 特性

package com.aidysoft.prisonmanagement.utiles.ownTest;

import java.util.*;
import java.util.stream.Collectors;

public class Java8CollectExample {

    public static void main(String[] args) {

     
        Student s1 = new Student(1L, "肖战", 15, "浙江");
        Student s2 = new Student(2L, "王一博", 15, "湖北");
        Student s3 = new Student(3L, "杨紫", 17, "北京");
        Student s4 = new Student(4L, "李现", 17, "浙江");
        Student s5 = new Student(1L, "肖战", 15, "浙江");
        List<Student> students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        students.add(s5);

        LongSummaryStatistics lss = students.stream().collect(Collectors.summarizingLong(Student::getAge));
        System.out.println("lss.getMax() = " + lss.getMax());
        System.out.println("lss.getMin() = " + lss.getMin());
        System.out.println("lss.getAverage() = " + lss.getAverage());
        System.out.println("lss.getCount() = " + lss.getCount());
        System.out.println("lss.getSum() = " + lss.getSum());

        //过滤
        new Student().testFilter(students).forEach(s -> System.out.println("过滤------" + s));

        //求和
        System.out.println("testSum(students) = " + new Student().testSum(students));

        //最大值
        System.out.println("testMax(students) = " + new Student().testMax(students));

        //最小值
        System.out.println("testMin(students) = " + new Student().testMin(students));

        //分组
        new Student().testGroup(students).forEach((k, v) -> System.out.println("分组------" + k + "-" + v));

        //转换 获取到该对象每个属性
        new Student().testMap(students).forEach(System.out::println);

        //转map
        new Student().testToMap(students).forEach((k, v) -> System.out.println("list转map------" + k + "-" + v));

        //去重
        new Student().testDistinct(students).forEach(s -> System.out.println("去重----" + s));

        //去重 根据条件
        new Student().testDistinctBy(students).forEach(s -> System.out.println("条件去重----" + s));

        //排序
        new Student().testSort(students).forEach(s -> System.out.println("排序----" + s));

        students.sort(Comparator.comparing(Student::getId).thenComparing(Student::getAge).reversed());
        System.out.println(students);

        //限制个数
        new Student().testLimit(students).forEach(s -> System.out.println("限制数量----" + s));

        //删除前几个元素
        new Student().testSkip(students).forEach(s -> System.out.println("删除----" + s));

        //根据条件删除元素
        new Student().testRemove(students).forEach(s -> System.out.println("条件删除----" + s));

    }
    
}

class Student {

    private Long id;

    private String name;

    private int age;

    private String address;

    public Student() {
    }

    public Student(Long id, String name, int age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(id, student.id) &&
                Objects.equals(name, student.name) &&
                Objects.equals(address, student.address);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age, address);
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


    /**
     * 集合的筛选
     *
     * @param students
     * @return
     */
    public static List<Student> testFilter(List<Student> students) {

        //筛选住在浙江省的学生
        return students
                .stream()
                .filter
                        (
                                s ->
                                        !"浙江".equals(s.getAddress()) &&
                                                !"湖北".equals(s.getAddress())

                        )
                .collect(Collectors.toList());
    }

    /**
     * 集合转换
     *
     * @param students
     * @return
     */
    public static List<String> testMap(List<Student> students) {
        //在地址前面加上部分信息,只获取地址输出
        return students.stream().map
                (
                        s -> "住址:" + s.getAddress() + "姓名" + s.getName()

                )
                .collect(Collectors.toList());
    }

    /**
     * 集合分组
     *
     * @param students
     * @return
     */
    public static Map testGroup(List<Student> students) {
        //在地址前面加上部分信息,只获取地址输出
        return students.stream().collect(Collectors.groupingBy(Student::getAddress));

    }

    /**
     * 集合转map
     *
     * @param students
     * @return
     */
    public static Map testToMap(List<Student> students) {
        //在地址前面加上部分信息,只获取地址输出
        return students.stream().collect(Collectors.toMap(Student::getId, value -> value, (k1, k2) -> k1));

    }

    /**
     * 去重
     *
     * @param students
     * @return
     */
    public static List<Student> testDistinct(List<Student> students) {
        return students.stream().distinct().collect(Collectors.toList());
    }

    public static List<Student> testDistinctBy(List<Student> students) {
        return students
                .stream()
                .collect
                        (
                                Collectors
                                        .collectingAndThen
                                                (
                                                        Collectors.toCollection(
                                                                () -> new TreeSet<>(Comparator.comparing(Student::getAddress))
                                                        ), ArrayList::new
                                                )
                        );
    }

    /**
     * 排序
     *
     * @param students
     * @return
     */
    public static List<Student> testSort(List<Student> students) {
        return students
                .stream()
                .limit(2)
                .sorted(Comparator.comparing(Student::getId))//升序
                .sorted(Comparator.comparing(Student::getAge).reversed())//降序
                .collect(Collectors.toList());
    }

    /**
     * 限制个数
     *
     * @param students
     * @return
     */
    public static List<Student> testLimit(List<Student> students) {
        return students
                .stream()
                .sorted(Comparator.comparing(Student::getId))//升序
                .sorted(Comparator.comparing(Student::getAge).reversed())//降序
                .limit(2)
                .collect(Collectors.toList());
    }

    /**
     * 集合skip,删除前n个元素
     */
    public static List<Student> testSkip(List<Student> studentList) {
        return studentList.stream().skip(1).collect(Collectors.toList());
    }

    /**
     * 删除某个元素
     */
    public static List<Student> testRemove(List<Student> studentList) {
        studentList.removeIf(s -> s.id == 1);
        return studentList;
    }

    /**
     * 属性求和
     *
     * @param studentList
     * @return
     */
    public static Integer testSum(List<Student> studentList) {
        return studentList.stream().mapToInt(Student::getAge).sum();
    }

    /**
     * 属性最大值
     *
     * @param studentList
     * @return
     */
    public static Student testMax(List<Student> studentList) {
        return studentList.stream().max(Comparator.comparingInt(Student::getAge)).get();
    }

    /**
     * 属性最小值
     *
     * @param studentList
     * @return
     */
    public static Student testMin(List<Student> studentList) {
        return studentList.stream().min(Comparator.comparingInt(Student::getAge)).get();
    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值