Selecting objects from an ArrayList based on value of certain fields

青春壹個敷衍的年華 提交于 2021-01-27 16:46:00

问题


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

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
}

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.


回答1:


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

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

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

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

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.




回答2:


I don't know if you're using Java 8 or not, but if you are, you could use streams and filter your ArrayList.

The syntax might look like:

return courseList.stream().filter(c -> c.getCourseName().equals(value));

You could have a separate method for byCourseName, byCourseLevel, etc. The principal would be the same.

If you're not using Java 8, you could still use separate methods and avoid the need for the switch/case.




回答3:


If you're using java 8, you can use Streams and in particular 'filter' . see for example the 'Filter' section in this tutorial: http://zeroturnaround.com/rebellabs/java-8-explained-applying-lambdas-to-java-collections/

If you were only filtering by name, then you can settle for a lambda expression "filter(p -> p.getName().equals(value)). But since you have a more complex case you're better off invoking a dedicated method.



来源:https://stackoverflow.com/questions/31523935/selecting-objects-from-an-arraylist-based-on-value-of-certain-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!