我将举一个我需要的小例子。我有这个简单的豆子:
public class Item {
private String name;
private String code;
private Category category;
//getters and setters omitted for brevity
}
public class SimpleItem {
private String name;
private String code;
//getters and setters omitted for brevity
}
我需要将 a 映射和分组Collection<Item>
到 a Map<Category,List<SimpleItem>>
。我尝试过这样的事情
public class itemMapper {
public Map<Category,List<SimpleItem>> map(Collection<Item> items){
return items.stream()
.map(this::mapToSimpleItem)
.collect(groupingBy(Item::getCategory));
}
private SimpleItem mapToSimpleItem(Item item){
//
}
}
问题是映射后,字段类别不再存在。有任何想法吗?