将列表收集到地图<object1,=“”map <object2,=“”object3 =“”>&gt;在Java 8中

时间:2017-10-19 09:29:14

标签: java lambda java-8 java-stream

我想迭代一个对象列表。每个对象都包含一些对象变量。更具体地说:

public class KCenterDocumentMapping {
    private DigitalFileCategory digitalFileCategory;
    private DocumentType documentType;
    private KCenterCategory kCenterCategory;
    private KCenterFolder kCenterFolder;
    private KCenterDocumentType kCenterDocumentType;

    //setters & getters
}

我的清单是:

List<KCenterDocumentMapping> allMappings = db4.retrieveMapping();

所需的地图结构是:

Map<DigitalFileCategory, Map<DocumentType, KCenterDocumentMapping>> kCenterDocumentMappingMap = new HashMap<>();

在我当前的实现中,我收到“无法从静态上下文引用非静态方法”。

Map<DigitalFileCategory, Map<DocumentType,KCenterDocumentMapping>> grouping = kCenterDocumentMappingList.stream()
                .collect(Collectors.groupingBy(KCenterDocumentMapping::getDigitalFileCategory,
                        Collectors.mapping(KCenterDocumentMapping::getDocumentType, this)));

enter image description here

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

Collectors.mapping(KCenterDocumentMapping::getDocumentType, this) // this?

我认为你正在寻找的是:

Collectors.toMap(KCenterDocumentMapping::getDocumentType,
         Function.identity())

Collectors.mapping接受FunctionCollector作为输入; this看起来不像Collector