java Lambda表达式如何按条件动态传入方法引用?

java中如何按条件传入分组
例如有个学生类集合,如何动态传入一个方法引用来分组?

list.stream().collect(Collectors.groupingBy(Student::getSex));
list.stream().collect(Collectors.groupingBy(Student::getAge));
public List<Student> test(动态传入){
// ...list
list.stream().collect(Collectors.groupingBy(动态传入));
return list
}
阅读 1.2k
avatarAI BotBETA

在 Java 中,你可以通过使用方法引用和 Lambda 表达式来按条件动态传入方法引用以进行分组。在你的示例中,你需要将某个方法作为参数传递给 Collectors.groupingBy()。这可以通过使用 Function 接口来实现。

下面是一个示例代码,展示了如何通过动态传入方法引用进行分组:

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

class Student {
    private String name;
    private String sex;
    private int age;

    // constructor, getters, setters...

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public int getAge() {
        return age;
    }

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

public class Main {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        // add some students...

        // 使用 lambda 表达式动态传入方法引用进行分组
        Map<String, List<Student>> groupedBySex = students.stream()
            .collect(Collectors.groupingBy(Student::getSex));
        Map<Integer, List<Student>> groupedByAge = students.stream()
            .collect(Collectors.groupingBy(Student::getAge));

        // 输出结果
        System.out.println(groupedBySex);
        System.out.println(groupedByAge);
    }
}

在这个示例中,我们创建了一个 Student 类,它有一些属性(例如姓名、性别和年龄)。然后,我们使用 Collectors.groupingBy() 方法根据性别和年龄对学生列表进行分组。我们通过使用方法引用 Student::getSexStudent::getAge 来动态传递要进行分组的属性。

2 个回答
public static List<Student> groupBy(List<Student> list, Function<Student, ?> dynamicReference) {
    Map<?, List<Student>> groupedStudents = list.stream()
        .collect(Collectors.groupingBy(dynamicReference));

    return groupedStudents.values().stream()
            .flatMap(Collection::stream)
            .collect(Collectors.toList());
}

public static void main(String[] args) {
    List<Student> students = Arrays.asList(
            new Student("Alice", "female", 20),
            new Student("Bob", "male", 22),
            new Student("Charlie", "male", 23),
            new Student("Alice", "female", 21),
            new Student("Bob", "male", 22)
    );

    List<Student> groupedBySex = groupBy(students, Student::getSex);
    List<Student> groupedByAge = groupBy(students, Student::getAge);

    System.out.println(Arrays.toString(groupedBySex.toArray()));
    System.out.println(Arrays.toString(groupedByAge.toArray()));
}

参数传一个Function,代码如下

public static Map<String, List<Student>> process(Function<Student, String> function) {
    List<Student> studentList = new ArrayList<>();
    Map<String, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(function));
    return map;
}

public static void main(String[] args) {
    Map<String, List<Student>> map=process(Student::getName);
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题