Consider the following snippet: Here I am trying to understand how to interpret these two statements Expression:1 Here we say that the List on the RHS must be such that all the elements of the list will satisfy the condition ? super List<? super Integer> but doubleList / integerList / numberList are not satisfying this condition anyhow – as we
Tag: generics
What’s the meaning of this line of code? And how can I create an object of this class?
I was trying to construct an object of the MTree class (https://github.com/Waikato/moa/blob/master/moa/src/main/java/moa/clusterers/outliers/utils/mtree/MTree.java) The constructor of MTree looks like this: The DistanceFunction here is an interface, the code of it is: And it’s implementation is: And my first question is what’s the meaning of return new DistanceFunction<Data>() in the method public static <Data> DistanceFunction<Data> cached(final DistanceFunction<Data> distanceFunction) [the method is in
Unchecked Cast warning – shows up for Type parameters but not for Concrete types?
Consider the following snippet: From what I understand – whenever we convert Supertype -> Subtype Unchecked cast is flagged as the compiler does not know until the runtime if the type represented by the Supertype will ever match the SubType. Then over here – why is (Double) (Number)a not being flagged as the Unchecked Cast? Answer Unchecked cast is flagged
Using Generics for return type parameter Java
I am trying to create a method that will return a generic type parameter. I have a class VehicleOrder that extends the abstract class Order. In class Order, I created an abstract method receiveHiredObject. This method will not receive any parameter and will return a generic. I implemented this method in the class VehicleOrder and I set it up to
java: incompatible types: T cannot be converted to java.lang.String
I am currently working on a encryption and decryption service for my application, using Google Tink. The problem is the following: I want to program it without using (nearly) duplicate code and therefore I had the idea of using generics. If parsing the Strings to a byte[] is the only option I will be doing that, but I’d rather not.
Get map dynamically based on key’s class type and avoid “raw use of parameterized class ‘Map'”
I have a cache class in which I used 2 HashMaps to keep the cache. I want to be able to choose the right map given key’s class type so that: if key is Long, then get value from map longKeyCache if key is String, then get value from map stringKeyCache. (assume user will only pass in Long or String
ClassCastException on raw functional interface lambda invocation
Consider this example: The last line will give java.lang.ClassCastException: class java.lang. Object cannot be cast to class java.lang.String. This is not a surprise for me, as Function is declared to accept String. I know that raw types can give ClassCastException, but all examples I saw are about ClassCastException on unchecked call return object not about method arguments: I can find
Java Generic Types in Method signature which are not used
What is the use of specifying Generic types in the method signature when they are not getting used in the method, for instance consider the below method from Kafka Materialized: Here the types K,V,S are not used in the method. Github link for materialized class Answer What is the use of specifying Generic types in the method signature when they
Generics code not working when i try to return concrete object
The goal i am trying to achieve, is to have different FileLoaders like CSVFileLoader, ExcelFileLoader that can load up any object of type T, as long as it know how to convert using ‘C’ and create the object of type T. Hope this makes sense. I am trying to use generics to create a generic FileLoader that will take a
How to return the same generic Collection-type with different element-type?
I have a method, that maps elements of a collection to other object and returns a collection containing the mapped elements. I want the returned Collection to be of the same Collection-type as the input Collection, but with a different element type. My method would look something like this: Obviously the part R extends C<T> doesn’t work. How can I