【问题标题】:How to filter the content of an ArrayList the right way?如何以正确的方式过滤 ArrayList 的内容?
【发布时间】:2019-09-08 23:19:17
【问题描述】:

我正在努力寻找一种更好的方法来过滤 ArrayList 的内容

例如,这个程序有一个名为“students”的主 ArrayList,然后我从这个列表的内容中创建了其他子列表(oldStudents、youngStudents、stupidStudents、smartStudents。目标是根据学生的用户选择过滤 ArrayList (年轻、年老、聪明或愚蠢)

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

它正在工作,但我相信有更好的方法来实现这一点

【问题讨论】:

  • 第一次将所有这些列表插入学生列表中...
  • 除非您需要在类中包含其他功能,否则最好分配给接口类型。您还可以通过执行List&lt;String&gt; stupid = new ArrayList&lt;&gt;(List.of("stupid1", "stupid2")); 之类的操作来创建可变列表。还有其他使用streams() 和收集器的方法。

标签: java search arraylist filter


【解决方案1】:

您可以使用这种方法:

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

【讨论】:

    【解决方案2】:

    Java 是一种面向对象的语言。使用它。

    创建一个包含 3 个字段的 Student 类:String nameboolean oldboolean smart。添加 getter 方法。

    您现在可以轻松过滤,例如

    // New list with dumb students
    dumpStudents = studentList.stream().filter(s -> ! s.isSmart()).collect(Collectors.toList());
    
    // Remove old people from list
    studentList.removeIf(Student::isOld);
    

    【讨论】:

      【解决方案3】:

      在 Java 8 中,您可以使用 Stream 来过滤列表。在此链接中,您可以找到不同的示例:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-19
        • 2019-09-21
        • 1970-01-01
        • 2017-10-25
        • 2015-03-19
        • 2012-05-17
        • 1970-01-01
        • 2012-07-11
        相关资源
        最近更新 更多