根据某些字段的值从 ArrayList 中选择对象 [英] Selecting objects from an ArrayList based on value of certain fields

查看:22
本文介绍了根据某些字段的值从 ArrayList 中选择对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Course 类,其中包含各种类型的字段,以及它们各自的 getter 和 setter,例如:

I have a Course class, which has fields of various types, with their respective getters and setters, such as:

float percentage
char courseLevel
String courseName
boolean takingNow

然后我有一个 ArrayList 的课程对象和一个方法:

I then have an ArrayList of course objects and a method:

public <T> ArrayList<Course> getCourses(int parameterId, T value) {
    ArrayList<Course> res = new ArrayList<Course>();
    switch(parameterId) {
        case COURSE_NAME:
            for(Course c: courseList) {
                if(c.getCourseName().equals(value) {
                    res.add(c)
                }
            }
            break;
        ....
        //rest of the cases
        ....
    }
    return res
}

然后该方法遍历课程的 ArrayList 并根据 parameterId 比较某个字段,如果课程字段等于传递的值,则将其添加到要返回的数组列表中.

the method then goes through the ArrayList of courses and compares a certain field based on the parameterId and if the course field equals the passed value it adds it to the to be returned arraylist.

有没有比以这种方式使用泛型更好的方法,我觉得这是一种非常糟糕的编码实践.

Is there a better way to do this than using generics in this way, which I feel is a very poor coding practice.

推荐答案

据我所知,您希望有一个方法可以返回符合指定条件的所有课程.

As far as I understand it, you want to have a method that returns all courses that match a specified condition.

您可能会考虑研究 lambdas ......它们起初看起来有点时髦,但它们真的很强大,一旦你习惯了它就很容易了.

You might consider looking into lambdas... they look a little funky at first, but they're really powerful and once you get used to it it's pretty easy.

对于您的示例,您可能有:

For your example, you might have this:

import java.util.ArrayList;
import java.util.function.Predicate;

class Course {

    float percentage;
    char courseLevel;
    String courseName;
    boolean takingNow;

    public static ArrayList<Course> allCourses;

    public ArrayList<Course> getCourses(Predicate<Course> coursePredicate) {
        ArrayList<Course> toReturn = new ArrayList<>();
        for (Course c : allCourses
                            .stream()
                            .filter(coursePredicate)
                            .toArray(Course[]::new)) {
            toReturn.add(c);
        }
        return toReturn;
    }
}

这意味着我们可以这样称呼它:

This means we can call it like so:

getCourses(c -> c.courseLevel != 'B');

意味着所有没有'B'课程级别的将被退回...

meaning all that don't have a course level of 'B' will be returned...

这是怎么回事?好的.首先,我们获取一个集合,并将其转换为Stream".这允许我们使用 Predicates(它们只是要匹配的条件)来过滤流.最后,我们可以将过滤后的 Stream 转换回数组,然后将这些元素添加回 ArrayList.

What's going on here? OK. First, we take a collection, and turn it into a "Stream". This allows us to use Predicates (which are just conditions to match on) to filter the stream. Finally, we can convert the filtered Stream back to an array, and then add those elements back into an ArrayList.

lambda 定义了该条件.它只是一个接受 Course 类型的单个参数的函数.在我的示例中,它等效于以下内容:

The lambda defines that condition. It is just a function that accepts a single argument of type Course. In my example, it would be equivalent to the following:

static boolean isMatch(Course c) {
    return c.courseLevel != 'B';
}

filter 方法循环遍历 Stream 中的所有元素,如果应用了 Predicate(lambda,相当于上述方法)返回 true,它包含在结果中.

The filter method loops over all the elements in the Stream and if applying the Predicate (the lambda, equiv to the above method) returns true, it's included in its result.

这篇关于根据某些字段的值从 ArrayList 中选择对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆