繁体   English   中英

方法unmodifiablelist()不能应用于给定类型

[英]Method unmodifiablelist() cannot be applied to given type

我试图编译一个前同事的代码,但它说方法unmodifiableList()不能应用于给定的类型。 Eclipse中的代码不显示任何错误。 但它仍然不允许我编译它。 可能是什么错误?

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();
   }
}

这是编译应用程序后eclipse打印的错误日志/信息:

[INFO] BUILD FAILURE

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-      plugin:3.6.1:compile (default-compile) on project 
         AutomationTesting: Compilation failure: Compilation failure: 
[ERROR] /C:/Users/Newbie/eclipse-   workspace/automationTesting/Data.java:[42,35] method unmodifiableList in    class java.util.
          Collections cannot be applied to given types;
[ERROR]   required: java.util.List<? extends T>
[ERROR]   found: java.util.Collection<framework.data.people.NonReference>
[ERROR]   reason: inferred type does not conform to upper bound(s)
[ERROR]     inferred: java.lang.Object&java.util.List<? extends   java.lang.Object>&java.util.Collection<framework.data.people.NonReference>
[ERROR]     upper bound(s):   java.util.Collection<framework.data.people.NonReference>,java.util.List<?  extends java.lang.Object>,java.lang.
              ObjectCompilation failure: Compilation failure: 
[ERROR] /C:/Users/Newbie/eclipse-workspace/automationTesting/Data.java:  
[42,35] method unmodifiableList in class java.util.Collections 
        cannot be applied to given types;
[ERROR]   required: java.util.List<? extends T>
[ERROR]   found:    java.util.Collection<framework.data.people.NonReference>
[ERROR]   reason: inferred type does not conform to upper bound(s)
[ERROR]     inferred: java.lang.Object&java.util.List<? extends   java.lang.Object>&java.util.Collection<framework.data.people.NonReference>
[ERROR]     upper bound(s):  java.util.Collection<framework.data.people.NonReference>,java.util.List<? extends java.lang.Object>,java.lang.Object

您需要显式声明泛型类型参数。 由于某种原因*,无法推断。

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())的类型collect(Collectors.toCollection()) ,反之亦然,正如您可能期望的那样。

您可以通过准确说明您想要的内容来帮助编译器进行类型推断。 你说的是unmodifiableList应该采取什么或者什么.collect(Collectors.toCollection())应该返回。

上面提到的四个片段中的任何一个都可以帮助您解决问题。


x -> (NonReference) x可以用NonReference.class::cast替换。

暂无
暂无

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

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