Java 8 - 方法映射不适用于参数(<notype>)

时间:2016-10-01 12:08:02

标签: java eclipse java-8 java-stream

我想重构我的方法,过滤AssetLink对象,获取作为AssetLink目标的Content对象,然后根据Content对象设置ContentLinkMetadata对象的字段。我的新方法看起来像这样:

private List<ContentLinkMetadata> getAndFillInternalLinks(final Lesson lesson) {
    List<ContentLinkMetadata> internalLinks = new ArrayList<>();
        lesson.getAssetLinks().stream()
        .filter(linkAsAssetLink -> ALLOWED_INTERNAL_LINK_TYPES.contains(linkAsAssetLink.getTargetType()))
        .map(linkAsAssetLink -> {
            Content link = contentProvider.getLazyById(linkAsAssetLink.getTargetId());
            ContentLinkMetadata internalLink = new ContentLinkMetadata();

            internalLink.setDescription(link.getDescription());
            internalLink.setId(link.getId());
            internalLink.setTitle(link.getTitle());
            internalLink.setFriendlyUrl(friendlyUrlResolver.makeUrl(link));
            internalLink.setTypeId(link.getTypeId());

            internalLinks.add(internalLink);
        });

        return internalLinks;
}

但我在.map上遇到错误,上面写着“类型Stream中的方法映射不适用于参数(noType linkAsAssetLink),我不明白。为什么它说linkAsAssetLink是”noType“它是实际上我也可以在AssetProvider的方法中使用它的方法。你能告诉我我在哪里做错了吗?

2 个答案:

答案 0 :(得分:3)

如果在操作图中使用括号,则应使用return运算符。你的代码看起来应该是这样的

private List<ContentLinkMetadata> getAndFillInternalLinks(final Lesson lesson) {
List<ContentLinkMetadata> internalLinks = lesson.getAssetLinks().stream()
    .filter(linkAsAssetLink -> ALLOWED_INTERNAL_LINK_TYPES.contains(linkAsAssetLink.getTargetType()))
    .map(linkAsAssetLink -> {
        Content link = contentProvider.getLazyById(linkAsAssetLink.getTargetId());
        ContentLinkMetadata internalLink = new ContentLinkMetadata();
        internalLink.setDescription(link.getDescription());
        internalLink.setId(link.getId());
        internalLink.setTitle(link.getTitle());
        internalLink.setFriendlyUrl(friendlyUrlResolver.makeUrl(link));
        internalLink.setTypeId(link.getTypeId());
        return internalLink;
    }).collect(Collectors.toList());

}

或使用此代码

    List<ContentLinkMetadata> internalLinks = lesson.getAssetLinks().stream()
        .filter(linkAsAssetLink -> ALLOWED_INTERNAL_LINK_TYPES.contains(linkAsAssetLink.getTargetType()))
        .map(linkAsAssetLink -> linkAsAssetLink.getTargetId())
        .filter(Objects::nonNull)
        .map(function)
        }).collect(Collectors.toList())


private Function<Object, ContentLinkMetadata> function = this::toInternalLink;

private ContentLinkMetadata toInternalLink(Object linkAsAssetLink){
                Content link = contentProvider.getLazyById(linkAsAssetLink.getTargetId());
                ContentLinkMetadata internalLink = new ContentLinkMetadata();
                internalLink.setDescription(link.getDescription());
                internalLink.setId(link.getId());
                internalLink.setTitle(link.getTitle());
                internalLink.setFriendlyUrl(friendlyUrlResolver.makeUrl(link));
                internalLink.setTypeId(link.getTypeId());
                return internalLink;
}

答案 1 :(得分:2)

看起来你错了。请尝试以下方法之一

content

或者您也可以使用foreach:

private List<ContentLinkMetadata> getAndFillInternalLinks(final Lesson lesson) {
    List<ContentLinkMetadata> internalLinks = lesson.getAssetLinks().stream()
        .filter(linkAsAssetLink -> ALLOWED_INTERNAL_LINK_TYPES.contains(linkAsAssetLink.getTargetType()))
        .map(linkAsAssetLink -> {
            Content link = contentProvider.getLazyById(linkAsAssetLink.getTargetId());
            ContentLinkMetadata internalLink = new ContentLinkMetadata();

            internalLink.setDescription(link.getDescription());
            internalLink.setId(link.getId());
            internalLink.setTitle(link.getTitle());
            internalLink.setFriendlyUrl(friendlyUrlResolver.makeUrl(link));
            internalLink.setTypeId(link.getTypeId());

            return internalLink;
        }).collect(Collectors.toList());

        return internalLinks;
}

地图总是应该返回一些东西。返回语句可以是隐式的,但它应该在那里。