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;
}
}
加载中,请稍候......