当前位置: 首页 > 知识库问答 >
问题:

java 8流从类型A的集合创建类型B的集合[重复]

卫弘懿
2023-03-14

我想使用流从类型B创建类型a的集合。

假设我有两节课

Class Employee{
   String firstName;
   String lastName;
   int age;
   String id;
   String email;
   double salary;
}

Class Person {
  String firstName;
  String lastName;
  String email;
}

为了从员工集合中创建人员集合,我编写了以下代码

public static List<Person> createPersonsFromEmployees(List<Employee> employees) {

        List<Person> persons = new ArrayList<>();

        employees.stream().filter(Object :: nonNull)
                  .forEach(e -> {
                       persons.add(new Person(e.getFirstName(),
                                           e.getLastName(), 
                                           e.getEmail());
                   };)


        return persons;
}

目前,这段代码可以正常工作。但我想知道,是否有更好的方法可以不使用forEach从员工创建人员集合。

共有3个答案

微生宝
2023-03-14

员工映射到人员您可以使用其他人已经提供的Collectors.mapping/Stream.map,因此我将跳过它。

注意,映射方式比映射然后收集方式快,因为<代码>收集(映射(…)) 是O(N),但<代码>映射(…)。收集(…) 是O(2N),但<代码>映射(…)。收集(…) 比收集更可读(映射(…) 和映射是指公共的转换(员工)方法参考,而不是函数

public List<Person> transform(List<Employee> employees) throws Throwable {
    return employees.stream()
            .filter(Objects::nonNull)
            .collect(Collectors.mapping(this::transform, Collectors.toList()));
}

public Person transform(Employee it) {
    return new Person(it.firstName, it.lastName, it.email);
}
施俊远
2023-03-14

创建适配器类:

class EmployeeToPersonAdapter {

    private EmployeeToPersonAdapter() {
    }

    public static Person toPerson(Employee employee) {
        if (employee == null) {
            return null;
        }
        return new Person(employee.getFirstName(),
                employee.getLastName(),
                employee.getEmail());
    }
}

然后使用它:

public static List<Person> createPersonsFromEmployees(List<Employee> employees) {
    return employees.stream()
            .filter(Objects::nonNull)
            .map(EmployeeToPersonAdapter::toPerson)
            .collect(Collectors.toList());
}
越星晖
2023-03-14

这里有一个更简洁的方法。使用。流中的forEach()表明可能有更好的方法来使用流。流是功能性的,它们试图远离可变性。

public static List<Person> createPersonsFromEmployees(List<Employee> employees)
    Function<Employee, Person> employeeToPerson = e -> new Person(e.getFirstName, e.getLaseName(), e.getEmail());

    return employees.stream()
                    .filter(Object :: nonNull)
                    .map(employeeToPerson)
                    .collect(Collectors.toList());

}
 类似资料:
  • 我知道重载是在编译时决定的,但当我试图运行下面的示例时,它给出了我无法理解的结果 当我每次运行这个代码片段时,我都会得到“Collection”的输出,这意味着调用参数为Collection的classify方法。 请解释

  • Swift 提供了三种主要的集合类型,所谓的数组、合集还有字典,用来储存值的集合。数组是有序的值的集合。合集是唯一值的无序集合。字典是无序的键值对集合。 Swift 中的数组、合集和字典总是明确能储存的值的类型以及它们能储存的键。就是说你不会意外地插入一个错误类型的值到集合中去。它同样意味着你可以从集合当中取回确定类型的值。 注意 Swift 的数组、合集和字典是以泛型集合实现的。要了解更多关于泛

  • 本页包含内容: 数组(Arrays) 集合(Sets) 字典(Dictionaries) 集合的可变性(Mutability of Collections) Swift 语言提供经典的数组和字典两种集合类型来存储集合数据。数组用来按顺序存储相同类型的数据。字典虽然无序存储相同类型数据值但是需要由独有的标识符引用和寻址(就是键值对)。 Swift 语言里的数组和字典中存储的数据值类型必须明确。 这意

  • 问题内容: 如果我有一个通用类: 我想实例化几个项目,例如… …并将它们添加到集合中。如何定义集合,使其可以容纳泛型类型列表?然后,我想在某个时刻迭代集合,并使用Value属性。可能? 问题答案: 让您的泛型类从非泛型基类继承,或实现一个非泛型接口。然后,您可以拥有此类型的集合,并将其转换为用于访问集合内容的任何代码。 这是一个例子。

  • 问题内容: 在解决了之前的问题的基础上,但又导致了另一个问题。如果协议/类类型存储在集合中,则取回并实例化它们会引发错误。下面是一个假设的例子。该范例基于“程序到接口而不是实现”,“程序到接口” 编辑 -我当前的解决方法是遍历集合,但是由于api必须知道各种实现,所以这当然是有限的。另一个限制是这些类型的子类(例如PersianCat,GermanShepherd)将不会调用其重写的函数,否则我将

  • 就像C++的stl一样,Rust提供了一系列的基础且通用的容器类型。善用这些集合类型,可以让Rust编程更加方便轻松,但每种数据结构都会有其局限性,合理的选型方能维持更好的效率。 本章目录: 动态数组 Vec 哈希表 HashMap