文章目录

在本页中,我们将提供 java 8 Stream map()示例。它返回由给定函数处理的流实例。

map()返回对象流,为了得到IntStreamLongStreamDoubleStream等原始数据类型的流,Java8 stream分别提供了mapToInt()mapToLong()mapToDouble()方法。

1.在函数实例中Stream map()方法的使用

Stream.map()方法的语法如下

map(Function mapper)

我们需要将函数实例作为lambda表达式传递。此方法返回具有给定函数处理结果的流实例。这是一个中间操作。

2.使用Stream Map()Map转换为List

我们使用Stream Map()作为中间操作将HashMap转换为List对象。

MapToList.java

package com.concretepage;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class MapToList {
	public static void main(String[] args) {
		Map map = new HashMap<>();
		map.put(111, "Lalkrishna");
		map.put(154, "Atal");		
		map.put(30, "Narendra");
		map.put(200, "Amit");
		
		List list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
				.map(e -> new User(e.getKey(), e.getValue())).collect(Collectors.toList());

		list.forEach(l -> System.out.println("Id: "+ l.getId()+", Name: "+ l.getName()));		
	}
}
class User {
	private int id;
	private String name;
	public User(int id, String name) {
		this.id = id; 
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
} 

输出

Id: 30, Name: Narendra
Id: 111, Name: Lalkrishna
Id: 154, Name: Atal
Id: 200, Name: Amit 

3.使用Stream Map()List转换为另一个List

我们使用Stream Map()作为中间操作将List转换为另一个List对象。

ListToAnotherList.java

package com.concretepage;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ListToAnotherList {
	public static void main(String[] args) {
		Person p1 = new Person(1, "Mohan", "student");
		Person p2 = new Person(2, "Sohan", "teacher");
		Person p3 = new Person(3, "Dinesh", "student");
		List personList = Arrays.asList(p1, p2, p3);
		
		List stdList = personList.stream().filter(p -> p.getPersonType().equals("student"))
			.map(p -> new Student(p.getId(), p.getName()))
			.collect(Collectors.toList());
		
		stdList.forEach(e -> System.out.println("Id:"+ e.getId()+ ", Name: "+ e.getName()));
	}
}
class Person {
	private int id;
	private String name;
	private String personType;
	public Person(int id, String name, String personType) {
		this.id = id;
		this.name = name;
		this.personType = personType;
	}
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public String getPersonType() {
		return personType;
	}
}
class Student {
	private int id;
	private String name;
	public Student(int id, String name) {
		this.id = id;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
} 

输出

Id:1, Name: Mohan
Id:3, Name: Dinesh 

4.Stream mapToInt()示例

这里我们提供mapToInt()的示例,mapToLong()mapToDouble()的方法与之相同。

MapToIntDemo.java

package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class MapToIntDemo {
	public static void main(String[] args) {
		Employee e1 = new Employee(1, 20);
		Employee e2 = new Employee(2, 15);
		Employee e3 = new Employee(3, 30);
		List list = Arrays.asList(e1, e2, e3);
		int sum = list.stream().mapToInt(e -> e.getAge()).sum();
		System.out.println("Sum: "+ sum);
	}
}
class Employee {
	private int id;
	private int age;
	public Employee(int id, int age) {
		this.id = id; 
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public int getAge() {
		return age;
	}
} 

输出

Sum: 65

参考文献

【1】Java 8 Stream map() Example

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐