StreamAPI
Stream API利用Lambda表达式完成,极大的提高了程序的效率和可读性,实现了万物皆可一行代码。以下是对学生信息的使用案例。
首先创建一个Student类来存放数据。里面放入学号等信息的定义,hashCode值,equals方法,toString方法和构造器方法等。
public class Student {
/**学号*/
private int sno;
/**姓名*/
private String name;
/**生日*/
private LocalDate birth;
/**专业*/
private String major;
/**学分*/
private double score;
/**性别*/
private String sex;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((birth == null) ? 0 : birth.hashCode());
result = prime * result + ((major == null) ? 0 : major.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(score);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
result = prime * result + sno;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (birth == null) {
if (other.birth != null)
return false;
} else if (!birth.equals(other.birth))
return false;
if (major == null) {
if (other.major != null)
return false;
} else if (!major.equals(other.major))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(score) != Double.doubleToLongBits(other.score))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
if (sno != other.sno)
return false;
return true;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getBirth() {
return birth;
}
public void setBirth(LocalDate birth) {
this.birth = birth;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student [sno=" + sno + ", name=" + name + ", birth=" + birth + ", major=" + major + ", score=" + score
+ ", sex=" + sex + "]";
}
}
然后就可以通过创建一个List集合来存放数据。
static List<Student> list = new ArrayList<Student>() {
{
add(new Student(140,"kill",LocalDate.of(1999,10,7),"软件工程",80.5,"男"));
add(new Student(123,"tom",LocalDate.of(1998,12,2),"计算机科学",88.5,"男"));
add(new Student(140,"kill",LocalDate.of(1999,10,7),"软件工程",80.5,"男"));
add(new Student(145,"aili",LocalDate.of(1998,3,7),"软件工程",56.5,"女"));
add(new Student(165,"true",LocalDate.of(1999,5,4),"网络工程",90.5,"女"));
add(new Student(134,"pen",LocalDate.of(1997,11,18),"电信技术",84.5,"男"));
add(new Student(162,"mick",LocalDate.of(1999,12,14),"软件工程",59.5,"男"));
add(new Student(187,"totty",LocalDate.of(1997,4,10),"网络工程",97.5,"女"));
add(new Student(157,"hellen",LocalDate.of(1999,6,16),"通信技术",83.5,"男"));
add(new Student(199,"rose",LocalDate.of(1998,11,18),"软件工程",88,"女"));
add(new Student(164,"year",LocalDate.of(1998,7,14),"软件工程",67.5,"男"));
add(new Student(141,"lily",LocalDate.of(1999,8,21),"软件工程",88.5,"女"));
add(new Student(180,"cat",LocalDate.of(1997,11,12),"软件工程",75.5,"男"));
add(new Student(184,"mouse",LocalDate.of(1999,12,7),"软件工程",91.5,"男"));
}
};
在筛选学生信息时,可以根据流式API来对数据进行筛选,截断以及排序等操作。使用Stream需要准备数据,再通过获取流、中间处理和结尾处理来实现。
//1.获取流
Stream<Student> stream = list.stream();
//2.中间处理
Stream<String> names= stream.filter(s->Objects.equals(s.getSex(),"男"))//找到所有男生
.filter(s->s.getScore() >=60) //分数超过60
.filter(s->"软件工程".equals(s.getMajor())) //专业
.distinct() //去除重复值
.sorted((s1,s2)->(int)(s1.getScore()-s2.getScore())) //根据分数从低到高排序
.map(s->s.getName())//对映射处理,去除name属性作为子集
.skip(2)//跳过的数据行数
.limit(4); //截取的数据行数
//3.结尾处理(输出只能有一个)
//收集 将中间处理完之后符合要求的元素收集到指定的集合中
List<String> listStu = names.collect(Collectors.toList());
System.out.println(list);
List<String> name= list.stream() //1.获取流
.filter(s->Objects.equals(s.getSex(),"男")) //中间处理,过滤
.filter(s->s.getScore() >=60) //分数超过60
.distinct() //去除重复
.sorted((s1,s2)->(int)(s1.getScore()-s2.getScore())) //根据分数从低到高排序
.skip(2)//跳过的数据行数
.limit(4) //截取的数据行数
.map(s->s.getName()) //中间处理,映射(获取属性子集)
.collect(Collectors.toList()); //尾部处理
}
}
通过对数据的中间处理,可以获取需要的信息数据,最后通过采集将数据放入指定集合中。