繁体   English   中英

java8 Collectors.toMap()限制?

[英]java8 Collectors.toMap() limitation?

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

我现在得到一个编译错误(类型推理引擎,我猜),我不明白。

这是一些提取的演示代码:

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流?

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

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...

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM