我有一种情况,我有一个类型的对象的列表,我需要从它创建另一种类型的另一个对象的列表。
下面是代码:我有员工和需要的列表,从第一个列表创建EmployeeInfo的名单。需要注意的是员工有一个帐户属性和EmployeeInfo有帐户的列表。在这种情况下,同样的员工可以有多个账户,所以在employeeinfo列表中产生,每个信息对象将有帐户列表。下面是如何我都做到了:
public class Employee {
private final int dept;
private final String name;
private final String city;
private final String account;
public Employee(int dept, String name, String city, String account) {
this.dept = dept;
this.name = name;
this.city = city;
this.account = account;
}
public int getDept() {
return dept;
}
public String getName() {
return name;
}
public String getCity() {
return city;
}
public String getAccount() {
return account;
}
}
public class EmployeeInfo {
private final String name;
private final List<String> accounts;
public EmployeeInfo(String name, List<String> accounts) {
this.name = name;
this.accounts = accounts;
}
public String getName() {
return name;
}
public List<String> getAccounts() {
return accounts;
}
public EmployeeInfo addToList(EmployeeInfo employeeInfo) {
List<String> l = new ArrayList<>();
l.addAll(this.getAccounts());
l.addAll(employeeInfo.getAccounts());
return new EmployeeInfo(employeeInfo.name, l);
}
}
测试类:
public static void main(String[] args){
List<Employee> employees = new ArrayList<>();
//tradeId, secPool, datasetid, restricValue
employees.add(new Employee(1, "Mary", "Boston", "A1"));
employees.add(new Employee(1, "Mary", "Boston", "A2"));
employees.add(new Employee(1, "Alex", "NYC", ""));
employees.add(new Employee(2, "Peter", "DC", ""));
employees.add(new Employee(1, "Sophia", "DC", "A4"));
TestEmployeeGrouping testEmployeeGrouping = new TestEmployeeGrouping();
Map<Integer, List<EmployeeInfo>> result = new HashMap<>();
Map<Integer, Map<String, List<Employee>>> map = employees.stream().collect(groupingBy(Employee::getDept, groupingBy(testEmployeeGrouping::createKey)));
map.forEach((integer, stringListMap) -> {
List<EmployeeInfo> employeeInfos = createInfo(stringListMap);
result.put(integer, employeeInfos);
});
}
private static List<EmployeeInfo> createInfo(Map<String,List<Employee>> stringListMap) {
List<EmployeeInfo> employeeInfos = new ArrayList<>();
stringListMap.forEach((s, employees) -> {
List<String> accounts = employees.stream().map(Employee::getAccount).collect(Collectors.toList());
employeeInfos.add(new EmployeeInfo(employees.get(0).getName(), accounts));
});
return employeeInfos;
}
private String createKey(Employee employee) {
return employee.getDept() + employee.getName();
}
虽然上面的一块工作正常,最后给我employeeinfo的列表,通过部门分组,每个都有其帐户列表,我想这样做在爱茉莉泛函的方式,如:
employees.stream().collect(groupingBy(Employee::getDept, groupingBy(testEmployeeGrouping::createKey, reducing(EmployeeInfo::addToList))));
上述线抛出错误:不兼容的类型:T是无法转换为雇员。是否有人可以帮助我弄清楚如何解决这个问题?
谢谢!
在那里,你所做的任何理由name
在EmployeeInfo
最后?如果你可以改变这个解决方案将工作
添加这两种方法EmployeeInfo
public void setName(String name) {
this.name = name;
}
public void AddAccount(String account) {
this.accounts.add(account);
}
然后你就可以做到这一点
Collector<Employee, EmployeeInfo, EmployeeInfo> empToInfo = Collector.of(
() -> new EmployeeInfo("", new ArrayList<String>()),
(info, e) -> {
info.AddAccount(e.getAccount());
info.setName(e.getName());
},
(p1,p2) -> p1.addToList(p2));
Collector<Employee, ?, Collection<EmployeeInfo>> byName = collectingAndThen(groupingBy(Employee::getName, empToInfo),
(Map<String, EmployeeInfo> finisher) -> {return finisher.values();});
Map<Integer, Collection<EmployeeInfo>> r2 = employees.stream().collect(groupingBy(Employee::getDept, byName));
如果你想保持一成不变EmployeeInfo,您可以用减少的,而不是集合,它会是这个样子
Map<Integer, Collection<EmployeeInfo>> result2 = employees.stream().collect(groupingBy(Employee::getDept,
collectingAndThen(groupingBy(Employee::getName, reducing(new EmployeeInfo("", new ArrayList<String>()),
empl3 -> new EmployeeInfo(empl3.getName(),Arrays.asList(empl3.getAccount())),
(inf1, inf2) -> inf1.addToList(inf2))),
finisher -> finisher.values())));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句