java Arraylist筛选并发送其他arraylist

ttisahbt  于 2024-01-05  发布在  Java
关注(0)|答案(4)|浏览(122)

模型类

  1. class Person {
  2. private String id;
  3. private String name;
  4. private int age;
  5. // Constructor
  6. public Person(String id,String name, int age) {
  7. this.id = id;
  8. this.name = name;
  9. this.age = age;
  10. }
  11. // Getter ve Setter metotları
  12. public String getID() {
  13. return id;
  14. }
  15. public void setID(String id) {
  16. this.id = id;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public int getAge() {
  25. return age;
  26. }
  27. public void setAge(int age) {
  28. this.age = age;
  29. }
  30. }

字符串
这里是主要的:

  1. public static void main(String[] args) {
  2. ArrayList<Person> personList = new ArrayList<>();
  3. personList.add(new Person("1","John", 25));
  4. personList.add(new Person("2","Alice", 30));
  5. personList.add(new Person("3","Bob", 28));
  6. }


我想像这样过滤-filterednewArraylist = (id =1 in person list)
我尝试了收集和流过滤器,但它不工作。

7fyelxc5

7fyelxc51#

正如你所指出的,流和过滤是要走的路:

  1. List<Person> filteredList =
  2. personList.stream()
  3. .filter(p -> p.getID().equals("1"))
  4. .collect(Collectors.toList());

字符串
如果你必须有一个ArrayList,而不是任何旧的列表(这可能不是一个好主意,例如What does it mean to "program to an interface"?),你可以使用toCollection而不是toList

  1. ArrayList<Person> filteredArrayList =
  2. personList.stream()
  3. .filter(p -> p.getID().equals("1"))
  4. .collect(Collectors.toCollection(ArrayList::new));

2sbarzqh

2sbarzqh2#

正如你提到的,你也可以用收藏的方式。这种方式更清晰,更容易,更简短。

  1. // Sorting based on ID
  2. Collections.sort(personList, Comparator.comparing(Person::getID));

字符串

ut6juiuv

ut6juiuv3#

要过滤personList以获得仅包含ID为“1”的人员的新ArrayList,可以使用Java流和filter方法。

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.stream.Collectors;
  4. public class Main {
  5. public static void main(String[] args) {
  6. ArrayList<Person> personList = new ArrayList<>();
  7. personList.add(new Person("1","John", 25));
  8. personList.add(new Person("2","Alice", 30));
  9. personList.add(new Person("3","Bob", 28));
  10. // Filter persons with ID "1"
  11. List<Person> filteredPersonList = personList.stream()
  12. .filter(person -> person.getID().equals("1"))
  13. .collect(Collectors.toList());
  14. // Print the filtered persons
  15. for (Person person : filteredPersonList) {
  16. System.out.println("ID: " + person.getID() + ", Name: " + person.getName() + ", Age: " + person.getAge());
  17. }
  18. }
  19. }

字符串

展开查看全部
atmip9wb

atmip9wb4#

在现代Java(JDK 17/21)中过滤List的正确方法如下:

  1. import java.util.List;
  2. public class FilterById {
  3. record Person(String id, String name, int age) {}
  4. public static void main(String[] args) {
  5. List<Person> people = List.of(
  6. new Person("1", "John", 25),
  7. new Person("2", "Alice", 30),
  8. new Person("3", "Bob", 28)
  9. );
  10. List<Person> peopleWithAnIdOfOne = people.stream()
  11. .filter(p -> p.id.equals("1"))
  12. .toList();
  13. System.out.println(peopleWithAnIdOfOne); // [Person[id=1, name=John, age=25]]
  14. }
  15. }

字符串
这是一种函数式方法。过滤后的结果是原始列表的不可变副本。我使用以下代码简化了这一点:

  • a Personrecord(JDK 14)
  • 一个不可变的输入列表~ List.of(JDK 9)
  • 不可变的输出列表~ Stream::toList(JDK 16)

可变性

我们甚至根本不需要显式地使用ArrayList。除非你需要修改结果,否则你应该将结果集合传递给一个新的ArrayList

  1. List<Person> filteredPeople = new ArrayList<>(peopleWithAnIdOfOne); // Mutable list

高级用法

如果你想合并组合一个访问器/Map器和一个 predicate ,你可以尝试下面的方法。

**注意:**这不适用于记录,因为Person::age不能从静态上下文引用。您将需要像Person::getAge这样的方法访问器。

  1. import lombok.Value;
  2. import java.util.List;
  3. import java.util.function.Function;
  4. import java.util.function.Predicate;
  5. public class FilterById {
  6. @Value
  7. static class Person {
  8. String id, name;
  9. int age;
  10. }
  11. // As a predicate functional interface
  12. private static final Predicate<Integer> isOlderThan25 = age -> age > 25;
  13. // As a static method
  14. private static boolean isOlderThan25(int age) {
  15. return age > 25;
  16. }
  17. private static final List<Person> people = List.of(
  18. new Person("1", "John", 25),
  19. new Person("2", "Alice", 30),
  20. new Person("3", "Bob", 28)
  21. );
  22. public static void main(String[] args) {
  23. {
  24. // Predicate functional interface
  25. List<Person> peopleOlderThan25 = people.stream()
  26. .filter(comparator(Person::getAge, FilterById::isOlderThan25))
  27. .toList();
  28. System.out.println(peopleOlderThan25);
  29. }
  30. {
  31. // Static method
  32. List<Person> peopleOlderThan25 = people.stream()
  33. .filter(comparator(Person::getAge, isOlderThan25))
  34. .toList();
  35. System.out.println(peopleOlderThan25);
  36. }
  37. }
  38. // Generic utility method that accepts a mapper and a predicate
  39. public static <T, R> Predicate<T> comparator(Function<T, R> mapper, Predicate<R> predicate) {
  40. return item -> predicate.test(mapper.apply(item));
  41. }
  42. }

展开查看全部

相关问题