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

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

问题描述

我有一个课程班,其中有各种类型的领域,各自的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推移和比较基于所述参数标识目标字段,并且如果过程字段等于传递的值将它添加到被返回数组列表

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.

您可以考虑寻找到lambda表达式......他们看起来有点时髦的在第一,但他们真的很强大,一旦你习惯了它它的pretty容易。

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'的课程级别将被退回...

这是怎么回事?好。首先,我们的集合,并且把它变成一个。这允许我们使用 predicate S(这只是条件匹配)来过滤流。最后,我们可以转换过滤返回一个数组,然后添加这些元素回一个的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.

该拉姆达定义了条件。这仅仅是一个接受类型的单个参数课程的功能。在我的例子,这将是等同于以下内容:

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';
}

过滤方法遍历在流中的所有元素,如果运用 predicate (拉姆达,当量上述方法)返回真正,它包含在它的结果。

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天全站免登陆