Java generics compile in Eclipse, but not in javac

后端 未结 5 1373
面向向阳花
面向向阳花 2020-12-16 14:49

I had to discover I have Java code in my project, which compiles and runs fine in Eclipse, but throws a compilation error in javac.

A self-contained snippet:

5条回答
  •  庸人自扰
    2020-12-16 15:12

    I know it's old question, but I want to mention, the function could be written as:

    import java.util.HashSet;
    import java.util.Set;
    
    public class Main {
    
    public static void main(String[] args) {
            Set setOfInts = new HashSet();
            Set setOfObjects = covariantSet(setOfInts);
        }
    
        public static  Set covariantSet(Set set) {
            return new HashSet(set);
        }
    
    }
    
    
    

    It's a little bit cleaner and you can use the function exactly how you intented to(with implicit generic typing).

    提交回复
    热议问题