亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

方法 unmodifiablelist() 不能應用于給定類型

方法 unmodifiablelist() 不能應用于給定類型

阿波羅的戰車 2023-05-10 14:14:59
我正在嘗試編譯一位前同事的代碼,但它說該方法unmodifiableList()不能應用于給定類型。Eclipse 中的代碼沒有顯示任何錯誤。但它仍然不讓我編譯它??赡苁鞘裁村e誤?package framework.interview.demographics;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Objects;import java.util.stream.Collectors;import java.util.stream.Stream;import org.apache.xmlbeans.impl.xb.xsdschema.Public;import framework.data.people.NonReference;import framework.data.people.people;public class schedualData {    private final List<people> schedual;    private schedualData(List<people> schedual) {            this.schedual = Objects.requireNonNull(schedual);    }    public static schedualData getSchedualData(List<people> schedual) {        if(schedual.size() < 1)            throw new IllegalArgumentException("schedual must   contain at least one people");        if(Stream.of(schedual).filter(people -> (!(people instanceof NonReference))).count() != 1)            throw new IllegalArgumentException("There must be one and only one Reference between"   + "People, number, and Review");        return new schedualData(schedual);    }        //****** Getters ******\\       public people getReference() {          return schedual.stream()         .filter(people -> !(people instanceof NonReference))         .toArray(people[]::new)[0];    }       public List<NonReference> getNonReferenceschedual() {        //This is where the error is showing.         return Collections               .unmodifiableList(schedual.stream()               .filter(NonReference.class::isInstance)               .map(x -> (NonReference) x)               .collect(Collectors.toCollection     (ArrayList<NonReference>::new)));      }    public List<people> getFullschedual() {        return Collections.unmodifiableList(schedual);    }     public int size() {       return schedual.size();   }}
查看完整描述

1 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

您需要顯式聲明泛型類型參數。由于某種原因*,無法推斷。

return Collections.<NonReference>unmodifiableList(schedual.stream()
                                        .filter(NonReference.class::isInstance)
                                        .map(NonReference.class::cast)
                                        .collect(Collectors.toCollection(ArrayList::new)));

或者

return Collections.unmodifiableList(schedual.stream()
                                        .filter(NonReference.class::isInstance)
                                        .map(NonReference.class::cast)
                                        .collect(Collectors.<NonReference, List<NonReference>>toCollection(ArrayList::new)));

或者

ArrayList<NonReference> result = schedual.stream()
    .filter(NonReference.class::isInstance)
    .map(NonReference.class::cast)
    .collect(Collectors.toCollection(ArrayList::new));
    return Collections.unmodifiableList(result);

雖然,你可以簡單地去

return Collections.unmodifiableList(schedual.stream()
                                        .filter(NonReference.class::isInstance)
                                        .map(NonReference.class::cast)
                                        .collect(Collectors.toList()));

*問題在于,它Collections.unmodifiableList決定了 的類型collect(Collectors.toCollection()),而不是相反,正如您可能期望的那樣。

您可以通過準確說明您想要的內容來幫助編譯器進行類型推斷。你說要么unmodifiableList拿走什么,.collect(Collectors.toCollection())要么返回什么。

上面提到的四個片段中的任何一個都可以幫助您解決問題。


x -> (NonReference) x可以替換為NonReference.class::cast.


查看完整回答
反對 回復 2023-05-10
  • 1 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號