Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic method arguments - Java

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?

like image 609
yonikawa Avatar asked Jan 24 '14 17:01

yonikawa


People also ask

How do you use generic method arguments in Java?

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.

What are generic arguments?

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.

What is a generic type parameter in Java?

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.


2 Answers

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();
like image 55
Raman Avatar answered Oct 10 '22 21:10

Raman


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;
}
like image 45
user1428716 Avatar answered Oct 10 '22 21:10

user1428716