While working on interview questions, I have come across below code: The above two put method’s are throwing compile time error. But, when I add m.put(3, new ArrayList()); it is adding to map with no compile time error. It is very clear for me that I can add new Object() as a value in HashMap because of…
Tag: generics
Java Generics With Any Type
I’m trying to write a method in Java that will be able to add a custom Key object to an array, or change an already existing key in the array if there is one. However, I can’t seem to get it to work. The types that keys will use are primarily be String and Integer, but my universal approach doesn&…
Override generic method with more narrow parametrization
What I have: I have a generic method which I want to override: What I want: There will be few classes that extend C-class and narrow parametrization. Example: Question: How can I achieve that? EDIT: Class C has many heirs: D, F, G, H… Each of those classes has the single method f(T param). And for each …
Generic method to print arrays in java
I am trying to write a generic method printAll which prints an array of integer or character. Here’s the code: } Error: Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – Erroneous tree type: <.any> Answer printAll(T[] t) will not accept primitive t…
How do I get the `.class` attribute from a generic type parameter?
The accepted answer to this question describes how to create an instance of T in the Generic<T> class. This involves passing in a Class<T> parameter to the Generic constructor and callin the newInstance method from that. A new instance of Generic<Bar> is then created, and the parameter Bar.c…
Using a generic class to perform basic arithmetic operations
I want to perform basic arithmetic operations like addition, subtraction, multiplication and division using only one generic method per operation for wrapper types like Integer, Float, Double … (excluding BigDecimal and BigInteger). I have tried to do something like the following (for addition) using a …
Missing return value on Void method?
The following code gives me compile-time errors: missing return value and missing return statement, what value would I return for this Void Type? Answer Void is not void, change it to void type if you don’t want to return anything. Void is a class, void is type. If you want Void, then you need to add re…
What is meant by parameterized type?
This link states the following: The instantiation of a generic type with actual type arguments is called a parameterized type . Example (of a parameterized type): Collection<String> coll = new LinkedList<String>(); So what is the parameterized type? Collection<String> or LinkedList<String…
How to implement enum with generics?
I have a generic interface like this: This interface has limited instances, hence it would be best to implement them as enum values. The problem is those instances have different type of values, so I tried the following approach but it does not compile: Any idea about this? Answer You can’t. Java doesn&…
Java Generics and adding numbers together
I would like to generically add numbers in java. I’m running into difficulty because the Numbers class doesn’t really support what I want to do. What I’ve tried so far is this: Obviously this will not work. How can I go about correcting this code so I could be given a list of <int> or …