简体   繁体   中英

Selecting objects from an ArrayList based on value of certain fields

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.

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 Predicate s (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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM