java8 list打印输出_Java8的stream,使用static对List students赋值放到Collectors打印出来,怎么改代码?...

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值