加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

lambda表达式使用list实现属性的条件查询

(2020-04-02 19:08:38)
标签:

it

教育

文化

分类: javaEE
JAVA 8 lambda 表达式filter实现按某个属性值查找对象集合中符合条件的对象

-----------------------------核心方法-----------------------

T1是个对象
//当前时间
Date now = new Date();
long nowLong = DateUtil.dateToLong(now);
//当前时间+10分钟
Date dateAdd = DateUtil.dateAdd(now, 600000);
long dateAddLong = DateUtil.dateToLong(dateAdd);

//filter
List collect = t1s.stream()
.filter(T1 -> (T1.getSlong()) >= nowLong && T1.getSlong() <= dateAddLong)
.collect(Collectors.toList());
for (int i = 0; i < collect.size(); i++){
System.out.println("10钟内测试" + collect.get(i).getS());
}

---------------------以下十个DEMO-----------------------

public static void main(String[] args){
long i = 1;

List studentList=new ArrayList<>();
studentList.add(new Student(i++,"1","0101","小明","",10));
studentList.add(new Student(i++,"1","0101","小灰","",8));
studentList.add(new Student(i++,"1","0101","","",9));
studentList.add(new Student(i++,"1","0101","小美","",11));
studentList.add(new Student(i++,"1","0101","","",10));


System.out.println("最大年:"+ studentList.stream().mapToInt(Student::getAge).max().getAsInt());
System.out.println("最小年:"+studentList.stream().mapToInt(Student::getAge).min().getAsInt());
System.out.println("平均年:"+ studentList.stream().mapToDouble(Student::getAge).average().getAsDouble());


List studyScoreList = new ArrayList<>();
i=1;
studyScoreList.add(new StudyScore(i++,"","小明",new BigDecimal(58.5)));
studyScoreList.add(new StudyScore(i++,"","小灰",new BigDecimal(59.5)));
studyScoreList.add(new StudyScore(i++,"","",new BigDecimal(67)));
studyScoreList.add(new StudyScore(i++,"","小美",new BigDecimal(99)));
studyScoreList.add(new StudyScore(i++,"","",new BigDecimal(98)));
studyScoreList.add(new StudyScore(i++,"数学","小明",new BigDecimal(30)));
studyScoreList.add(new StudyScore(i++,"数学","小灰",new BigDecimal(15)));
studyScoreList.add(new StudyScore(i++,"数学","",new BigDecimal(62)));
studyScoreList.add(new StudyScore(i++,"数学","小美",new BigDecimal(97)));
studyScoreList.add(new StudyScore(i++,"数学","",new BigDecimal(97.7)));


List ywStudyScoreList = studyScoreList.stream().filter(m1->"".equals(m1.getCourse())).collect(Collectors.toList());
System.out.println("文成集合:");
for(StudyScore studyScore:ywStudyScoreList)
{
System.out.println("id:"+studyScore.get_id()+" - "+studyScore.getCourse()+" - "+studyScore.getName()+" - "+studyScore.getScore().setScale(2,RoundingMode.HALF_UP));
}


BigDecimal totalYwStudyScore= ywStudyScoreList.stream().map(StudyScore::getScore).reduce(BigDecimal.ZERO, BigDecimal::add); //求和
System.out.println("文成绩综:"+totalYwStudyScore);


long personNum=ywStudyScoreList.size();
System.out.println("考人:"+personNum);


System.out.println("文平均分:"+(totalYwStudyScore.divide(new BigDecimal(personNum))).setScale(2, RoundingMode.HALF_UP));
}


* 学生类
*/
public class Student {

private long studentId;

private String cls;

private String number;

private String name;

private String sex;

private int age;

public Student(long studentId, String cls, String number, String name, String sex, int age) {
this.studentId = studentId;
this.cls = cls;
this.number = number;
this.name = name;
this.sex = sex;
this.age = age;
}

public long getStudentId() {
return studentId;
}

public void setStudentId(long studentId) {
this.studentId = studentId;
}

public String getCls() {
return cls;
}

public void setCls(String cls) {
this.cls = cls;
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}



public class StudyScore {

private long _id;

private String course;

private String name;

private BigDecimal score;

public StudyScore(long _id, String course, String name, BigDecimal score) {
this._id = _id;
this.course = course;
this.name = name;
this.score = score;
}

public long get_id() {
return _id;
}

public void set_id(long _id) {
this._id = _id;
}

public String getCourse() {
return course;
}

public void setCourse(String course) {
this.course = course;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public BigDecimal getScore() {
return score;
}

public void setScore(BigDecimal score) {
this.score = score;
}
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有