编程语言
首页 > 编程语言> > Java8流式编程GroupBy和求最值示例

Java8流式编程GroupBy和求最值示例

作者:互联网

流的创建(演示常用的)

数组创建

Arrays.stream
Arrays的静态方法stream() 可以获取数组流

     String[] arr = { "a", "b", "c", "d", "e", "f", "g" };
     Stream<String> stream = Stream.of(arr);
     Stream<String> stream1 = Arrays.stream(arr);

集合创建

Collection接口提供了两个默认方法创建流:stream()、parallelStream()

    List<String> list = new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.add("c");
    Stream<String> stream = list.stream();

值创建

Stream.of

使用静态方法 Stream.of(), 通过显示值创建一个流。它可以接收任意数量的参数。

    Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);

函数创建

Stream.iterate
Stream.generate
使用静态方法 Stream.iterate() 和 Stream.generate()创建无限流。

    Stream<Double> stream = Stream.generate(Math::random).limit(5);
    Stream<Integer> stream1 = Stream.iterate(0, i -> i + 1).limit(5);

注意:使用无限流一定要配合limit截断,不然会无限制创建下去。

GroupBy和求最值示例

package com.demo;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
 
/** lombok的使用需要的依赖:
 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <version>1.18.16</version>
 </dependency>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Student {
    private long id;
    private String name;
    private int age;
    private int grade;
    private String major;
    private String school;
}
package com.demo;
 
import java.util.*;
import java.util.stream.Collectors;
 
public class FileTest {
    public static void main(String[] args) {
 
        List<Student> students = new ArrayList<Student>() {
            {
                add(new Student(20160001, "孔明", 20, 1, "土木工程", "武汉大学"));
                add(new Student(20160003, "玄德", 22, 3, "经济管理", "武汉大学"));
                add(new Student(20160004, "云长", 21, 2, "信息安全", "武汉大学"));
                add(new Student(20161001, "翼德", 21, 2, "机械与自动化", "华中科技大学"));
                add(new Student(20161002, "元直", 23, 4, "土木工程", "华中科技大学"));
                add(new Student(20161003, "奉孝", 23, 4, "计算机科学", "华中科技大学"));
                add(new Student(20162001, "仲谋", 22, 3, "土木工程", "浙江大学"));
                add(new Student(20162002, "鲁肃", 23, 4, "计算机科学", "浙江大学"));
                add(new Student(20163001, "丁奉", 24, 5, "土木工程", "南京大学"));
            }
        };
 
        // group by 统计用法示例
        Map<String, Long> group2 = students.stream().collect(
                Collectors.groupingBy(
                        Student::getSchool,
                        Collectors.counting()
                )
        );
        //输出:{南京大学=1, 武汉大学=3, 浙江大学=2, 华中科技大学=3}
        System.out.println(group2);
 
        TreeMap<String, Set<String>> collect = students.stream().collect(
                Collectors.groupingBy(
                        Student::getSchool,
                        TreeMap::new,
                        Collectors.mapping(Student::getName, Collectors.toSet())
                )
        );
        //输出:{华中科技大学=[翼德, 元直, 奉孝], 南京大学=[丁奉], 武汉大学=[孔明, 云长, 玄德], 浙江大学=[鲁肃, 仲谋]}
        System.out.println(collect);
 
        // 求最大  最小值
        Student studentMax = students.stream().max((s1, s2) -> s1.getAge() - s2.getAge()).get();
        Student studentMin = students.stream().min((s1, s2) -> s1.getAge() - s2.getAge()).get();
        // 输出:Student(id=20163001, name=丁奉, age=24, grade=5, major=土木工程, school=南京大学)
        System.out.println(studentMax);
        // 输出:Student(id=20160001, name=孔明, age=20, grade=1, major=土木工程, school=武汉大学)
        System.out.println(studentMin);
 
 
 
 
 
 
        List<Integer> list = new ArrayList<Integer>() {
            {
                add(20);
                add(18);
                add(18);
                add(17);
            }
        };
        Map<Integer, Long> collect1 = list.stream().collect(
                Collectors.groupingBy(
                        (integer) -> integer,
                        Collectors.counting()
                )
        );
        // 输出:{17=1, 18=2, 20=1}
        System.out.println(collect1);
 
        TreeMap<Integer, List<Integer>> collect2 = list.stream().collect(
                Collectors.groupingBy(
                        Integer::intValue,
                        TreeMap::new,
                        Collectors.mapping((integer) -> integer, Collectors.toList())
                )
        );
        // 输出:{17=[17], 18=[18, 18], 20=[20]}
        System.out.println(collect2);
 
        Integer max = list.stream().max(Integer::compareTo).get();
        Integer min = list.stream().min(Integer::compareTo).get();
        System.out.println(max);// 输出:20
        System.out.println(min);// 输出:17
 
 
    }
 
 
}

 

标签:Collectors,和求,Stream,stream,示例,add,Student,new,GroupBy
来源: https://www.cnblogs.com/xiejn/p/16312575.html