I have two almost identical methods, but I'm trying to avoid code duplication.
Each of them takes a unique object as arguments and finds out the highest value from it.
Here's an example:
public Integer getHighestIndexValue(List<ObjectA> list) {
int highestValue;
List<Integer> indexes = new ArrayList<Integer>();
for (ObjectA a: list) {
indexes.add(Integer.parseInt(a.getA()));
}
highestValue = Collections.max(indexes);
return highestValue;
}
Or:
public Integer getHighestIndexValue(List<ObjectB> list) {
int highestValue;
List<Integer> indexes = new ArrayList<Integer>();
for (ObjectB b: list) {
indexes.add(Integer.parseInt(b.getB()));
}
highestValue = Collections.max(indexes);
return highestValue;
}
How do I combine these two using generic parameters?
I tried creating a BaseClass
that contains these two classes and extended it in the method signature. Still it requires casting.
public <T extends BaseClass> Integer getHighestIndexValue(List<T> objectList) {
int highestValue;
List<Integer> indexes = new ArrayList<Integer>();
for (T objects: objectList) {
indexes.add(Integer.parseInt(objects.getAorB())); ------ this line needs casting
}
highestValue = Collections.max(indexes);
return highestValue;
}
I have used Generics before but not generics parameters yet.
Is there a way to resolve this?
Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used. It is possible to use both generic methods and wildcards in tandem.
Generic Argument Clause The generic argument list is a comma-separated list of type arguments. A type argument is the name of an actual concrete type that replaces a corresponding type parameter in the generic parameter clause of a generic type. The result is a specialized version of that generic type.
A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.
Can getA
and getB
be combined into a single interface method?
For example:
interface ProvidesIndex {
Integer getIndex();
}
Now, you can simply call getIndex
, and your method signature will be:
public Integer getHighestIndexValue(List<? extends ProvidesIndex> list)
As a side note, if you define your interface to extend Comparable<ProvidesIndex>
i.e.:
interface ProvidesIndex extends Comparable<ProvidesIndex>
Then you can use Collections.max
directly on the initial list:
List<ProvidesIndex> list = new ArrayList<ProvidesIndex>();
list.add(new ObjectA());
list.add(new ObjectB());
Integer max = Collections.max(list).getIndex();
Adding to the answer by Raman :
public Integer getHighestIndexValue(List<? extends CommonInterface> list) {
int highestValue;
List<Integer> indexes = new ArrayList<Integer>();
for (CommonInterface a: list) {
indexes.add(Integer.parseInt(a. getIndex()));
}
highestValue = Collections.max(indexes);
return highestValue;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With