我正嘗試在ZipEntry
的Stream
上使用java8的Collectors.toMap
。這可能不是最好的想法,因爲在處理過程中可能會出現異常,但我想它應該是可能的。java8 Collectors.toMap()限制?
我現在得到一個編譯錯誤(我推測類型推理引擎),我不明白。
這裏的一些提取演示代碼:
3210此代碼生成原來的樣子,但它不建立,如果你只是發表意見.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...
您使用了哪種編譯器?我測試了'jdk1.8.0_05','jdk1.8.0_20','jdk1.8.0_40beta'和'Eclipse Luna(4.4.0)';他們沒有任何問題編譯代碼。 – Holger 2014-10-02 13:05:45
@Holger我可以重現這個問題(你需要評論'.map(ZipEntry :: name)'出)與jdk1.8.0_20使用javac。 – assylias 2014-10-02 13:15:13
@assylias:我明白了。由於1.8u20是我列表中唯一存在該問題的人,因此我們有[此問題](http://stackoverflow.com/q/25414867/2711488)的副本(或者可能與[該問題](http: //stackoverflow.com/q/25490581/2711488))? – Holger 2014-10-02 13:30:07