Skip to content
Advertisement

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’t

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 of those method

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 type arrays. You need to pass arrays of the respective wrapper types: But, you don’t need to frame

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.class is passed in. What do you do if the generic type parameter

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 generic class. It issues a compile-time error, operator + cannot be applied to E,E Is

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 return statement at end.

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> Answer They are both parameterized types: types that take other types as parameters. The fact that you have different types

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’t allow generic types on enum constants. They

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 <long> or whatever and return

Advertisement