Untitled
unknown
java
2 years ago
1.9 kB
6
Indexable
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Student { private int id; private String name; private String gender; private int age; public Student (int id, String name, String gender, int age) { this.id = id; this.name = name; this.gender = gender; this.age = age; } public String getName() { return name; } public int getId() { return id; } public String getGender() { return gender; } public int getAge() { return age; } } class StudentDTO { private int id; private String name; private String gender; private int age; public StudentDTO (int id, String name, String gender, int age) { this.id = id; this.name = name; this.gender = gender; this.age = age; } public String getName() { return name; } public int getId() { return id; } public String getGender() { return gender; } public int getAge() { return age; } } public class Main { public static void main(String[] args) { List<Student> list = Arrays.asList(new Student(1,"A","male",26), new Student(2,"B","female",26), new Student(3,"C","male",16), new Student(4,"D","male",26)); List<Student> maleList = list.stream().filter(student -> student.getGender().equals("male")).collect(Collectors.toList()); long count = list.stream().filter(student -> student.getAge() > 20).count(); List<StudentDTO> studentDTOList = list.stream() .map(s -> new StudentDTO(s.getId(), s.getName(), s.getGender(), s.getAge())) .collect(Collectors.toList()); } }
Editor is loading...