当前位置:   article > 正文

Stream map()转化成新的类型流

stream map

它返回一个由给定函数处理的 Stream 实例。 map()返回对象流,为了得到IntStream、LongStream、DoubleStream等原始数据类型的流,Java8 stream分别提供了mapToInt()、mapToLong()和mapToDouble()方法。

map()函数

Stream.map () 方法如下。

map(Function mapper)

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

使用 Stream map() 将 Map 转换为 List

在这里,我们将使用 Stream.map() 作为中间操作将 HashMap 转换为对象列表。

  1. public class MapToList {
  2. public static void main(String[] args) {
  3. Map<Integer, String> map = new HashMap<>();
  4. map.put(111, "Lalkrishna");
  5. map.put(154, "Atal");
  6. map.put(30, "Narendra");
  7. map.put(200, "Amit");
  8. List<User> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
  9. .map(e -> new User(e.getKey(), e.getValue())).collect(Collectors.toList());
  10. list.forEach(l -> System.out.println("Id: "+ l.getId()+", Name: "+ l.getName()));
  11. }
  12. }
  13. class User {
  14. private int id;
  15. private String name;
  16. public User(int id, String name) {
  17. this.id = id;
  18. this.name = name;
  19. }
  20. public int getId() {
  21. return id;
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. }

输出

  1. Id: 30, Name: Narendra
  2. Id: 111, Name: Lalkrishna
  3. Id: 154, Name: Atal
  4. Id: 200, Name: Amit

使用 Stream map() 将 List 转换为另一个 List

在这个例子中,我们将使用 Stream.map() 作为中间操作将一个对象的 List 转换为另一个不同对象的 List。

  1. public class ListToAnotherList {
  2. public static void main(String[] args) {
  3. Person p1 = new Person(1, "Mohan", "student");
  4. Person p2 = new Person(2, "Sohan", "teacher");
  5. Person p3 = new Person(3, "Dinesh", "student");
  6. List<Person> personList = Arrays.asList(p1, p2, p3);
  7. List<Student> stdList = personList.stream().filter(p -> p.getPersonType().equals("student"))
  8. .map(p -> new Student(p.getId(), p.getName()))
  9. .collect(Collectors.toList());
  10. stdList.forEach(e -> System.out.println("Id:"+ e.getId()+ ", Name: "+ e.getName()));
  11. }
  12. }
  13. class Person {
  14. private int id;
  15. private String name;
  16. private String personType;
  17. public Person(int id, String name, String personType) {
  18. this.id = id;
  19. this.name = name;
  20. this.personType = personType;
  21. }
  22. public int getId() {
  23. return id;
  24. }
  25. public String getName() {
  26. return name;
  27. }
  28. public String getPersonType() {
  29. return personType;
  30. }
  31. }
  32. class Student {
  33. private int id;
  34. private String name;
  35. public Student(int id, String name) {
  36. this.id = id;
  37. this.name = name;
  38. }
  39. public int getId() {
  40. return id;
  41. }
  42. public String getName() {
  43. return name;
  44. }
  45. }

输出

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

Stream mapToInt()示例

这里我们提供了 mapToInt() 的例子,我们可以用同样的方式来处理 mapToLong() 和 mapToDouble()。

  1. public class MapToIntDemo {
  2. public static void main(String[] args) {
  3. Employee e1 = new Employee(1, 20);
  4. Employee e2 = new Employee(2, 15);
  5. Employee e3 = new Employee(3, 30);
  6. List<Employee> list = Arrays.asList(e1, e2, e3);
  7. int sum = list.stream().mapToInt(e -> e.getAge()).sum();
  8. System.out.println("Sum: "+ sum);
  9. }
  10. }
  11. class Employee {
  12. private int id;
  13. private int age;
  14. public Employee(int id, int age) {
  15. this.id = id;
  16. this.age = age;
  17. }
  18. public int getId() {
  19. return id;
  20. }
  21. public int getAge() {
  22. return age;
  23. }
  24. }

输出

Sum: 65

参考:

https://www.concretepage.com/java/jdk-8/java-8-stream-map-example

https://blog.csdn.net/qq_31635851/article/details/111355079

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/495173
推荐阅读
相关标签
  

闽ICP备14008679号