package com.graduation.project.dataCollector.test.stream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class studentStream {
private List students;
static{
// 初始化
List students = new ArrayList() {
{
add(new Student(20160001, "孔明", 20, 1, "土木工程", "武汉大学"));
add(new Student(20160002, "伯约", 21, 2, "信息安全", "武汉大学"));
add(new Student(20160003, "玄德", 22, 3, "经济管理", "武汉大学"));
add(new Student(20160004, "云长", 21, 2, "信息安全", "武汉大学"));
add(new Student(20161001, "翼德", 21, 2, "机械与自动化", "华中科技大学"));
add(new Student(20161002, "元直", 23, 4, "土木工程", "华中科技大学"));
add(new Student(20161003, "奉孝", 23, 4, "计算机科学", "华中科技大学"));
add(new Student(20162001, "仲谋", 22, 3, "土木工程", "浙江大学"));
add(new Student(20162002, "鲁肃", 23, 4, "计算机科学", "浙江大学"));
add(new Student(20163001, "丁奉", 24, 5, "土木工程", "南京大学"));
}
};
}
public static void main(String[] args) {
List civilStudents = students.stream()
.filter(student -> "土木工程".equals(student.getMajor())).limit(2)
.collect(Collectors.toList());
}
}
package com.graduation.project.dataCollector.test.stream;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Data;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Student {
/** 学号 */
private long id;
private String name;
private int age;
/** 年级 */
private int grade;
/** 专业 */
private String major;
/** 学校 */
private String school;
}
根据楼下的回答,修改完
package com.graduation.project.dataCollector.test.stream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class StudentStream {
private static List students;
static{
// 初始化
students = new ArrayList() {
{
add(new Student(20160001, "孔明", 20, 1, "土木工程", "武汉大学"));
add(new Student(20160002, "伯约", 21, 2, "信息安全", "武汉大学"));
add(new Student(20160003, "玄德", 22, 3, "经济管理", "武汉大学"));
add(new Student(20160004, "云长", 21, 2, "信息安全", "武汉大学"));
add(new Student(20161001, "翼德", 21, 2, "机械与自动化", "华中科技大学"));
add(new Student(20161002, "元直", 23, 4, "土木工程", "华中科技大学"));
add(new Student(20161003, "奉孝", 23, 4, "计算机科学", "华中科技大学"));
add(new Student(20162001, "仲谋", 22, 3, "土木工程", "浙江大学"));
add(new Student(20162002, "鲁肃", 23, 4, "计算机科学", "浙江大学"));
add(new Student(20163001, "丁奉", 24, 5, "土木工程", "南京大学"));
}
};
}
public static void main(String[] args) {
// List civilStudents = students.stream()
// .filter(student -> "土木工程".equals(student.getMajor())).limit(2)
// .collect(Collectors.toList());
students.stream()
.filter(student -> "土木工程".equals(student.getMajor()))
.limit(2)
.forEach(System.out::println);
}
}