Java中的map()函数及示例
在本文中,我们将看到Java Stream API中map()函数的示例,该示例用于对流中的元素进行一些转换。当我们使用map操作时,在将给定函数应用于源流的元素后,将返回一个新流,其中包含结果元素。
java.util.stream.Stream接口中的广义map()函数是
<R> Stream<R> map(Function<? super T,? extends R> mapper)
这里R是新接口的元素类型。
映射器是应用于每个元素的无干扰,无状态函数,映射器的类型为Function,它是一个函数接口,可以实现为lambda表达式。
除了通用的map()函数之外,还有分别返回IntStream,LongStream和DoubleStream的mapToInt(),mapToLong()和mapToDouble()方法,它们是这些原始数据类型的专用原始类型流。
在原始类型的stream中,还有一个mapToObj()方法,该方法返回一个对象值的Stream。
map()Java Stream示例
1将Stream中的每个元素转换为大写并在List中收集这些元素。对于此需求,可以使用map()方法将upperCase功能应用于流的所有元素,然后使用collect()方法将此转换的结果收集到List中。
List<String> names = Stream.of("Hyman", "Lisa", "Scott", "Nikita", "Tony")
.map(s -> s.toUpperCase())
.collect(Collectors.toList());
names.forEach(System.out::println);
输出:
Hyman LISA SCOTT NIKITA TONY
2使用map()方法从源流中获取仅包含选定字段的新Stream。这样,我们可以将流转换为具有新类型的元素。
假设有一个Employee类,其中包含名称,部门,年龄字段,并且源流包含Employee类型的对象。要求是将名称和部门字段映射到EmpDept类型的新流。
class Employee {
private String name;
private String dept;
private int salary;
Employee(String name, String dept, int salary){
this.name = name;
this.dept = dept;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
class EmpDept {
private String name;
private String dept;
EmpDept(String name, String dept){
this.name = name;
this.dept = dept;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
public class EmpStream {
public static void main(String[] args) {
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("Hyman", "Finance", 5500));
employeeList.add(new Employee("Lisa", "Finance", 5600));
employeeList.add(new Employee("Scott", "Finance", 7000));
employeeList.add(new Employee("Nikita", "IT", 4500));
employeeList.add(new Employee("Tony", "IT", 8000));
List<EmpDept> emp = employeeList.stream()
.map(e -> new EmpDept(e.getName(), e.getDept()))
.collect(Collectors.toList());
emp.forEach(e -> System.out.println("Name- " + e.getName() +
" Department- " + e.getDept()));
}
}
输出:
Name- Hyman Department- Finance Name- Lisa Department- Finance Name- Scott Department- Finance Name- Nikita Department- IT Name- Tony Department- IT
具有过滤器示例的3map()–使用map方法获取新的流,该流具有财务部门中的员工姓名。过滤方法用于过滤那些不匹配给定谓词的员工。
List<String> emp = employeeList.stream()
.filter(e -> e.getDept().equals("Finance"))
.map(e -> e.getName())
.collect(Collectors.toList());
emp.forEach(System.out::println);
输出:
Hyman Lisa Scott
Java mapToInt()示例
1如果要获取员工的平均工资,请使用mapToInt()方法获取包含薪水的IntStream,然后在该int流上应用average()方法。
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("Hyman", "Finance", 5500));
employeeList.add(new Employee("Lisa", "Finance", 5600));
employeeList.add(new Employee("Scott", "Finance", 7000));
employeeList.add(new Employee("Nikita", "IT", 4500));
employeeList.add(new Employee("Tony", "IT", 8000));
double avgSalary = employeeList.stream()
.mapToInt(e -> e.getSalary())
.average()
.getAsDouble();
System.out.println("Average salary- " + avgSalary);
输出:
Average salary- 6120.0
2如果我们想获得最高薪水,则可以使用mapToInt()方法获取一个由薪水组成的IntStream,然后在该int流上应用max()方法。
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("Hyman", "Finance", 5500));
employeeList.add(new Employee("Lisa", "Finance", 5600));
employeeList.add(new Employee("Scott", "Finance", 7000));
employeeList.add(new Employee("Nikita", "IT", 4500));
employeeList.add(new Employee("Tony", "IT", 8000));
int maxSalary = employeeList.stream()
.mapToInt(e -> e.getSalary())
.max()
.getAsInt();
System.out.println("Maximum salary- " + maxSalary);
输出:
Maximum salary- 8000

