mrspringy
mrspringy

Reputation: 27

How to filter the content of an ArrayList the right way?

I was working on finding a better way to filter the content of an ArrayList

For example, this program has a main ArrayList called "students" and then I made other sublistes from the content of this list (oldStudents,youngStudents,stupidStudents,smartStudents. The goal is to filter the ArrayList based on user choice of students (young,old, smart or stupids)

    ArrayList<String> students = new ArrayList<String>(); 
    ArrayList<String> smartStudents = new ArrayList<String>(); 
    ArrayList<String> stupidStudents = new ArrayList<String>(); 
    ArrayList<String> oldStudents = new ArrayList<String>(); 
    ArrayList<String> youngStudents = new ArrayList<String>(); 


    //adding all the students to students list 
    Collections.addAll(students, "Ram", "Mohan", "Sohan", "Rabi", "Shabbir","Jack", "Johnson", "Peter", "Despina", "Me");
    //adding young students to youngStudents list 
    Collections.addAll(youngStudents, "Ram", "Mohan", "Sohan", "Rabi", "Shabbir");
    //adding smart students to oldStudents list 
    Collections.addAll(oldStudents, "Jack", "Johnson", "Peter", "Despina", "Me");
    //adding smart students to smartStudents list 
    Collections.addAll(smartStudents, "Sohan", "Rabi", "Peter", "Despina");
    //adding smart students to stupidStudents list 
    Collections.addAll(stupidStudents, "Ram", "Mohan", "Shabbir","Jack", "Johnson", "Me");

    Scanner input = new Scanner(System.in);
    String uInput = "";



    System.out.print("This is a students search engine, write 'young' for younger students and 'old' for older ones ");
    uInput = input.nextLine();

    if(uInput.equals("young")) {

        students.removeAll(oldStudents); 

    } else if (uInput.equals("old")) {

        students.removeAll(youngStudents);

    }


    System.out.print("now write 'Smart' for smarter students and 'Stupid' for less smart students ");
    uInput = input.nextLine();

  if(uInput.equals("smart")) {

        students.removeAll(stupidStudents); 

    } else if (uInput.equals("Stupid")) {

        students.removeAll(smartStudents);

    }


  System.out.println(students);

It is working but I believe there is a better way to achieve this

Upvotes: 0

Views: 77

Answers (3)

Aldo Fernando Saia
Aldo Fernando Saia

Reputation: 69

In Java 8 you can use Stream to filter the list. In this link you can find different examples:

https://www.baeldung.com/java-stream-filter-lambda

Upvotes: 0

Andreas
Andreas

Reputation: 159086

Java is an Object-Oriented language. Use it.

Create a Student class with 3 fields: String name, boolean old, and boolean smart. Add getter methods.

You can now filter easily, e.g.

// New list with dumb students
dumpStudents = studentList.stream().filter(s -> ! s.isSmart()).collect(Collectors.toList());

// Remove old people from list
studentList.removeIf(Student::isOld);

Upvotes: 0

spi
spi

Reputation: 1735

You can use this approach:

class Student { String name; boolean old; boolean stupid; }

List<Student> students = initializeListOfStudents();

// alter these flags accordingly to your needs
Boolean old = null;
Boolean stupid = true;

// then do the filter
List<Student> allStupids = students.stream()
    .filter(it -> (old==null || it.old == old) && (stupid==null || it.stupid==stupid))
    .collect(Collectors.toList());

Upvotes: 1

Related Questions