list元素属性操作后返回新list
说明:
本机一定要装java8环境,这里面使用了jdk1.8的新特性,stream流
准备工作
java8环境
新建一个实体类Emp.java
package com.shaoming;
/**
* @ClassName: Emp
* @Author: 邵明
* @Date: 2021/9/12 01:14
* @Description:
*/
public class Emp {
private Integer id;
private String name;
private Double salary;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
模拟获取list操作
package com.shaoming;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @ClassName: App2
* @Author: 邵明
* @Date: 2021/9/12 01:15
* @Description: stream流测试
*/
public class EmpListGet {
public static void main(String[] args) {
List<Emp> empList = getEmpData();
System.out.println(empList);
}
public static List<Emp> getEmpData(){
List<Emp> empList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Emp emp = new Emp();
emp.setId(i);
emp.setName("name"+i);
emp.setSalary(1000D);
empList.add(emp);
}
return empList;
}
}
控制台打印:
[Emp{id=0, name=‘name0’, salary=1000.0}, Emp{id=1, name=‘name1’, salary=1000.0}, Emp{id=2, name=‘name2’, salary=1000.0}, Emp{id=3, name=‘name3’, salary=1000.0}, Emp{id=4, name=‘name4’, salary=1000.0}, Emp{id=5, name=‘name5’, salary=1000.0}, Emp{id=6, name=‘name6’, salary=1000.0}, Emp{id=7, name=‘name7’, salary=1000.0}, Emp{id=8, name=‘name8’, salary=1000.0}, Emp{id=9, name=‘name9’, salary=1000.0}]
需求
要求在返回的list集合中每个emp的name属性加个stream前缀
比如 name1 改为 stream-name1
常规解决方案
package com.shaoming;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @ClassName: App2
* @Author: 邵明
* @Date: 2021/9/12 01:15
* @Description:
*/
public class EmpListGet {
public static void main(String[] args) {
List<Emp> empList = getEmpData();
System.out.println(empList);
List<Emp> empListNew = new ArrayList<>();
for (Emp emp : empList) {
Emp empNew = new Emp();
empNew.setId(emp.getId());
empNew.setSalary(emp.getSalary());
empNew.setName("stream-"+emp.getName());
empListNew.add(empNew);
}
System.out.println(empListNew);
}
public static List<Emp> getEmpData(){
List<Emp> empList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Emp emp = new Emp();
emp.setId(i);
emp.setName("name"+i);
emp.setSalary(1000D);
empList.add(emp);
}
return empList;
}
}
控制台打印:
使用stream流解决方案
package com.shaoming;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @ClassName: App2
* @Author: 邵明
* @Date: 2021/9/12 01:15
* @Description:
*/
public class EmpListGet {
public static void main(String[] args) {
List<Emp> empList = getEmpData();
System.out.println(empList);
//常规解决方案
List<Emp> empListNew = new ArrayList<>();
for (Emp emp : empList) {
Emp empNew = new Emp();
empNew.setId(emp.getId());
empNew.setSalary(emp.getSalary());
empNew.setName("stream-"+emp.getName());
empListNew.add(empNew);
}
System.out.println(empListNew);
//使用stream流解决方案
List<Emp> empList2 = empList.stream().map(e -> {
Emp emp = new Emp();
emp.setId(e.getId());
emp.setName("stram流-"+e.getName());
emp.setSalary(e.getSalary());
return emp;
}).collect(Collectors.toList());
System.out.println(empList2);
}
public static List<Emp> getEmpData(){
List<Emp> empList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Emp emp = new Emp();
emp.setId(i);
emp.setName("name"+i);
emp.setSalary(1000D);
empList.add(emp);
}
return empList;
}
}
个人csdn博客网址:https://blog.csdn.net/shaoming314
个人博客网址:www.shaoming.club