Java 8 中的新特性,虽然现在都出到了Java14版本,不过在日常的开发过程中,8的版本是足够使用了,再说现在的8以上的版本也都面向商业收费了,很多新手,我所接触到的,像我那时候一样,追求船新版本,一上来就去学java14
的东西,当成一个爱好还行,重心还是要放在实用上
需求:我需要过滤高考分数大于500的人
首先,新建一个内部类
static class Student{
private String name;
private Integer score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name!=null ? name.trim() : null;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public Student(String name, Integer score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", score=" + score +
"}\n";
}
}
使用IntStream
遍历快速初始化一批值
public static void main(String[] args) {
List<Student> studentList = IntStream.rangeClosed(0,20)
.mapToObj(i -> new Student("Java Pro"+i,490+i))
.collect(Collectors.toList());
}
过滤出分数大于500的并输出
List<Student> studentGiao = studentList.stream()
.filter(student -> student.score > 500)
.collect(Collectors.toList());
System.out.println(studentGiao.toString());
输出:
[Student{
name='Java Pro11', score=501}
, Student{
name='Java Pro12', score=502}
, Student{
name='Java Pro13', score=503}
, Student{
name='Java Pro14', score=504}
, Student{
name='Java Pro15', score=505}
, Student{
name='Java Pro16', score=506}
, Student{
name='Java Pro17', score=507}
, Student{
name='Java Pro18', score=508}
, Student{
name='Java Pro19', score=509}
, Student{
name='Java Pro20', score=510}
]
需要考虑到为空和为0的情况
package com.github.gleans;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class LambdaLearning {
public static void main(String[] args) {
List<Double> nums = Arrays.asList(1.01, 2.11, 3.23, 4.222, null, 5.6);
double resNum = nums.stream()
.map(num -> Objects.isNull(num) ? 0 : num)
.mapToDouble(num -> num)
.sum();
System.out.println(resNum);
}
}
map
是重新指向一个对象,把->
右侧的对象赋予,此处判断若num为null
则赋予0值
注意,这里不可给null
使用filter
过滤掉,否则全为null的情况,会报空指针异常
public static void testTwo(){
List<Double> nums = Arrays.asList(1.01, 2.11, 3.23, 4.222, null, 5.6);
DoubleSummaryStatistics number = nums.stream()
.map(num -> Objects.isNull(num) ? 0 : num)
.mapToDouble(num -> num)
.summaryStatistics();
System.out.println("最大值:"+number.getMax());
System.out.println("最小值:"+number.getMin());
System.out.println("平均值:"+number.getAverage());
}
输出
最大值:5.6
最小值:0.0
平均值:2.6953333333333336
public static void main(String[] args) {
testOne();
}
public static void testOne(){
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
// 这里的 10 相当于初始值
int sum = numbers
.stream()
.reduce(10, Integer::sum);
System.out.println(sum);
}
根据年龄分组
package com.github.gleans;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SumWage {
public static void main(String[] args) {
List<UserDemo> userDemoList = new ArrayList<UserDemo>() {
{
add(new UserDemo(20, "jason", BigDecimal.valueOf(1000000)));
add(new UserDemo(22, "yasuo", BigDecimal.valueOf(2000000)));
add(new UserDemo(22, "ekko", BigDecimal.valueOf(100)));
}};
Map<Integer, List<UserDemo>> UserDemoMapByAge = userDemoList.stream()
.collect(Collectors.groupingBy(UserDemo::getAge));
System.out.println(UserDemoMapByAge.toString());
}
static class UserDemo {
private int age;
private String username;
private BigDecimal wage;
public UserDemo(int age, String username, BigDecimal wage) {
this.age = age;
this.username = username;
this.wage = wage;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public BigDecimal getWage() {
return wage;
}
public void setWage(BigDecimal wage) {
this.wage = wage;
}
@Override
public String toString() {
return "UserDemo{" +
"age=" + age +
", username='" + username + '\'' +
", wage=" + wage +
'}';
}
}
}
输出
{
20=[UserDemo{
age=20, username='jason', wage=1000000}], 22=[UserDemo{
age=22, username='yasuo', wage=2000000}, UserDemo{
age=22, username='ekko', wage=100}]}
json化观看观看更为直观
{
20:[
{
"age":20,
"username":"jason",
"wage":1000000
}
],
22:[
{
"age":22,
"username":"yasuo",
"wage":2000000
},
{
"age":22,
"username":"ekko",
"wage":100
}
]
}
Map<Integer, DoubleSummaryStatistics> userAvgWageByAge = userDemoList.stream()
.collect(Collectors.groupingBy(UserDemo::getAge, Collectors.summarizingDouble(s -> s.getWage().doubleValue())));
userAvgWageByAge.forEach((k, v) -> System.out.println(String.format("年龄:%d,平均工资:%f", k, v.getAverage())));
Stream.of(1, 2, 3, 4).collect(Collectors.toList())
后续会学习更多关于Lambda
的操作,日积月累…一定会成为一个秃头的程序猿