raw-types

Cannot convert from List<List> to List<List<?>>

寵の児 提交于 2019-12-17 06:14:10
问题 A raw list converts to List<?> just fine. Why can't a list of raw lists convert to a list of List<?> ? { // works List raw = null; List<?> wild = raw; } { // Type mismatch: cannot convert from List<List> to List<List<?>> List<List> raw = null; List<List<?>> wild = raw; } Backstory (to mitigate the xy problem): An API I'm using returns List<JAXBElement> . I happen to know that it is always List<JAXBElement<String>> . I plan to loop and build my own List<String> , but I was trying to fix (but

Raw types inside of generic definition

此生再无相见时 提交于 2019-12-11 23:28:14
问题 I wonder why the following generic definition does not produce a compiler warning: class MyClass<T extends List> { } and how the above definition is different to class MyClass<T extends List<?>> { } Whenever you read about generics, you read about how raw types should be avoided and consequently, whenever you handle generic types, you get a compiler warning. The raw type inside of the first definition does however not create such a warning. Secondly, I wonder how the exact subtyping

Java generic programming with unknown generic type of interface

☆樱花仙子☆ 提交于 2019-12-11 02:18:54
问题 I'm using several interfaces with generics types. While combining it together I have some problems when I have to use them from a part of the code that is unaware of the concrete type of the generic parameter. Suppose I have the following interface: public interface MyObjectInterface<T extends Number> {} The object implementing that interfaceare stored in a generic collection with the same generic type: public interface MyCollectioninterface<T extends Number> { public void updateObject

Why does Scala complain about illegal inheritance when there are raw types in the class hierarchy?

我们两清 提交于 2019-12-04 03:54:35
问题 I'm writing a wrapper that takes a Scala ObservableBuffer and fires events compatible with the Eclipse/JFace Databinding framework. In the Databinding framework, there is an abstract ObservableList that decorates a normal Java list. I wanted to reuse this base class, but even this simple code fails: val list = new java.util.ArrayList[Int] val obsList = new ObservableList(list, null) {} with errors: illegal inheritance; anonymous class $anon inherits different type instances of trait

Scala class cant override compare method from Java Interface which extends java.util.comparator

大兔子大兔子 提交于 2019-12-01 06:23:41
I'm currently working on a port of a jEdit plugin to write all code in Scala. However Im forced at a certain point to implement my own Comparator. My simplified code is as follows: class compare extends MiscUtilities.Compare { def compare(obj1: AnyRef, obj2: AnyRef): Int = 1 } The MiscUtilities.Compare has the following signature when looking from IntelliJ public static interface Compare extends java.util.Comparator { int compare(java.lang.Object o, java.lang.Object o1); } However when im trying to compile my class I get a error saying: error: class compare needs to be abstract, since method

Explicit method type parameter ignored on a raw class type; compiler bug? [duplicate]

蓝咒 提交于 2019-12-01 04:26:04
问题 This question already has answers here : Java generic methods in generics classes (6 answers) Closed 4 years ago . I am getting a compiler error calling a generic method with explicit type parameters, as if the explicit type parameter had not been taken into account. Minimal example: class CastExample { static class ThingProducer<S> { public <T> T getThing() { return null; } } static class ThingA {} public static void main(String... args) { ThingProducer thingProducer = new ThingProducer();

List<List<?>> and List<List> are incompatible types in java [duplicate]

☆樱花仙子☆ 提交于 2019-12-01 03:31:56
This question already has an answer here: Cannot convert from List<List> to List<List<?>> 3 answers I did not get this code to compile either way: List<List> a = new ArrayList(); List<List<?>> b = new ArrayList(); a = b; // incompatible types b = a; // incompatible types It seems that java does not consider List and List<?> to be the same type when it comes to generics. Why is that? And is there some nice way out? Context There is a library function with following signature: public <T> Set<Class<? extends T>> getSubTypesOf(final Class<T> type) . This works fine for simple types passed as

List<List<?>> and List<List> are incompatible types in java [duplicate]

删除回忆录丶 提交于 2019-12-01 00:08:16
问题 This question already has answers here : Cannot convert from List<List> to List<List<?>> (3 answers) Closed 4 years ago . I did not get this code to compile either way: List<List> a = new ArrayList(); List<List<?>> b = new ArrayList(); a = b; // incompatible types b = a; // incompatible types It seems that java does not consider List and List<?> to be the same type when it comes to generics. Why is that? And is there some nice way out? Context There is a library function with following

Why does the Java Compiler complain on using foreach with a raw type? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-11-30 09:12:45
问题 This question already has answers here : What is a raw type and why shouldn't we use it? (15 answers) Closed 3 years ago . I got a strange compiler error when using generics within a for-each loop in Java. Is this a Java compiler bug, or am I really missing something here? Here is my whole class: public class Generics<T extends Object> { public Generics(T myObject){ // I didn't really need myObject } public List<String> getList(){ List<String> list = new ArrayList<String>(); list.add("w00t

Implementing Comparable with a generic class

て烟熏妆下的殇ゞ 提交于 2019-11-30 04:59:57
I want to define a class that implements the generic Comparable interface. While in my class I also defined a generic type element T . In order to implement the interface, I delegate the comparison to T . Here is my code: public class Item<T extends Comparable<T>> implements Comparable<Item> { private int s; private T t; public T getT() { return t; } @Override public int compareTo(Item o) { return getT().compareTo(o.getT()); } } When I try to compile it, I get the following error information: Item.java:11: error: method compareTo in interface Comparable<T#2> cannot be applied to given types;