I have an interface that contains an array (or list) of T and some metadata. If I write the simplest implementation of the interface, I get a compile error on the emptyArray(): “Cannot use T as a reified type parameter. Use a class instead.” However, if I change both the interface and the implemen…
Tag: generics
InvalidTypesException for Generic Pattern Transformation in Apache Flink
I have problem regarding Apache Flink. I want to have an abstract class that consumes a stream. However the pattern applied to this stream should be interchangeable. TEventType marks the type of the event that is generated from the pattern. Every pattern must implement an interface called IEventPattern The ab…
trouble initialize List Using Arrays.asList
When I initialize List, I am able to do this: But when I want to simplify it using Arrays.asList: It cannot compile with error: Why it cannot do the type inference right and how to fix this? Answer Remember that … is just syntactic sugar for an array parameter. You can call a method with a variadic para…
Java instance for comparable
Why is it legal to create new Box(); and new Box<Integer>();? Is it because Box is comparable? Answer You have declared the class with a generic type parameter. This is not the same as implementing the Comparable interface: Is the same as: Which is not the same as: Because the type parameter is unbounde…
Java cooperating generic classes: can we avoid unchecked cast?
I have two abstract generic classes. They cooperate and hence depend on each other. Occasionally one needs to pass this to the other. I am trying to find a type safe way to do this: My question is whether I can find a cleaner way so I avoid the unchecked cast in AbstractB.setA(). I had hoped to declare it set…
Insert Dimensions to complete Expression/ReferenceType
I’m a newbie to Java. I have provided a short snippet from my code for BFS. According to Eclipse, I have an error on each of the last 4 lines. Syntax Error: insert “Dimensions” to complete expression/referencetype. I would appreciate any input/advice! Answer Cause of this error -You are tryi…
Implement Cartesian product of several collections by Java Stream
Right now I can only implement the Cartesian product of two collections, here is the code: This code works fine in IntelliJ, but not in Eclipse. Both with compiler compliance level of 1.8: Here is Pair.java: How to fix this error? Is there an elegant way to implement the Cartesian product of several collectio…
TreeSet constructor with Comparator parameter
In Java’s documentation for its class TreeSet one of the constructors is shown to have the following header: Can someone help explain why there is a constructor for TreeSet which takes a comparator object as its argument? I have no clue why this is done. Answer The elements in a TreeSet are kept sorted. If yo…
How to check does interface with generic fit to class
I have some interface with generic type interface DummyInterface<T> and i have class to which i have to set interface Imagine the situation where i store DummyClass with different generics in ArrayList. When i get class from list i don’t know the generic of class, and i want to check if interface …
Case-insensitive matching of a string to a Java enum
Java provides a valueOf() method for every Enum<T> object, so given an enum like one can do a lookup like If the string passed to valueOf() does not match (case sensitive) an existing Day value, an IllegalArgumentException is thrown. To do a case-insensitive matching, one can write a custom method insid…