java8 Collectors.toMap()限制? [英] java8 Collectors.toMap() limitation?

查看:695
本文介绍了java8 Collectors.toMap()限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 ZipEntry <的 Stream 上使用java8的 Collectors.toMap / code>。它可能不是最好的主意,因为在处理过程中可能会出现异常,但我想它应该是可能的。

I am trying to use java8's Collectors.toMap on a Stream of ZipEntry. It may not be the best idea because of possible exceptions occuring during the processing, but I guess it ought to be possible.

我现在收到编译错误(类型推断)引擎我猜)我不明白。

I am now getting a compile error (type inference engine I guess) which I don't understand.

以下是一些提取的演示代码:

Here's some extracted demo code:

import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class TestMapCollector {

    private static class MyObject {
    }

    public static void main(String[] argv) throws IOException {
        try (ZipFile zipFile = new ZipFile("test")) {
            Map<String, MyObject> result = zipFile.stream()
                    .map(ZipEntry::getName)
                    .collect(Collectors.toMap(f -> "test", f -> new MyObject()));
        }
    }
}

此代码按原样构建但是,如果你只是评论 .map(ZipEntry :: getName)行,它就不会构建。好像 toMap 收集器在输入是 String 的流时可以工作,但如果输入是一个流 ZipEntry

This code builds as-is, however it doesn't build if you just comment the .map(ZipEntry::getName) line. As if the toMap collector could work if the input is a stream of String but not if the input is a stream of ZipEntry?

作为参考,这是构建错误的开始,它非常模糊:

For reference, here is the beginning of the build error, it's quite obscure:

no suitable method found for collect(Collector<Object,CAP#1,Map<String,MyObject>>)
    method Stream.<R#1>collect(Supplier<R#1>,BiConsumer<R#1,? super CAP#2>,BiConsumer<R#1,R#1>) is not applicable
      (cannot infer type-variable(s) R#1
        (actual and formal argument lists differ in length))
    method Stream.<R#2,A>collect(Collector<? super CAP#2,A,R#2>) is not applicable
      (cannot infer type-variable(s) R#2,A,CAP#3,T#2,K,U
        (argument mismatch; Collector<CAP#2,CAP#4,Map<Object,Object>> cannot be converted to Collector<? super CAP#2,CAP#4,Map<Object,Object>>))
  where R#1,T#1,R#2,A,T#2,K,U are type-variables:
    R#1 extends Object declared in method <R#1>collect(Supplier<R#1>,BiConsumer<R#1,? super T#1>,BiConsumer<R#1,R#1>)
    T#1 extends Object declared in interface Stream
    R#2 extends Object declared in method <R#2,A>collect(Collector<? super T#1,A,R#2>)
    A extends Object declared in method <R#2,A>collect(Collector<? super T#1,A,R#2>)
    T#2 extends Object declared in method <T#2,K,U>toMap(Function<? super T#2,? extends K>,Function<? super T#2,? extends U>)
    K extends Object declared in method <T#2,K,U>toMap(Function<? super T#2,? extends K>,Function<? super T#2,? extends U>)
    U extends Object declared in method <T#2,K,U>toMap(Function<? super T#2,? extends K>,Function<? super T#2,? extends U...


推荐答案

问题似乎是由于流的事实类型使用通配符 - 不确定这是否是预期的行为。解决方法是:

The problem seems to be due to the fact that the stream type uses wild cards - not sure if this is expected behaviour. A workaround would be:

zipFile.stream().map(ZipEntry.class::cast) //or .map(z -> (ZipEntry) z)

这篇关于java8 Collectors.toMap()限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆