2

I have a java method that i'm trying to make generic so that it can take a list of 2 different types of object as a parameter. (Trivial example shown below). These 2 different objects will both always have the methods getDate() and getHour(). The code looks like this:

public <T> List<T> getListOfStuff(List<T> statistics) {

    List<T> resultList = new ArrayList<T>(statistics.size());

    if(statistics.size() > 0){
        resultList.add(statistics.get(0));

        int date = Integer.parseInt(resultList.get(0).getDate());
        int hour = Integer.parseInt(resultList.get(0).getHour());
    }
    return resultList;
}

However this doesn't work. These two lines don't work:

int date = Integer.parseInt(resultList.get(0).getDate());
int hour = Integer.parseInt(resultList.get(0).getHour());

The errors say: "The method getDate() is undefined for the type T" and "The method getHour() is undefined for the type T"

It offers me a suggestion to add a cast to the method receiver but it won't let me use T and instead forces the object name upon me like this which won't work for me:

int date = Integer.parseInt((ObjectName1)resultList.get(0).getDate());
int hour = Integer.parseInt((ObjectName1)resultList.get(0).getHour());

Is there any way to do what I want here?

Stackman
  • 129
  • 3
  • 14

2 Answers2

5

You'll want to use the following:

public <T extends ObjectName1> List<T> getListOfStuff(List<T> statistics) {
    List<T> resultList = new ArrayList<>(statistics.size());

    if (!statistics.isEmpty()) {
        resultList.add(statistics.get(0));

        int date = Integer.parseInt(resultList.get(0).getDate());
        int hour = Integer.parseInt(resultList.get(0).getHour());
    }

    return resultList;
}

The only Lists that can be passed to this method now must either hold ObjectName1 or an object that extends it.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
  • Thanks, now I understand. Unfortunately this wont work for my situation because the 2 classes both have getDate() and getHour() methods but those methods are defined in those 2 classes and not in one of the common super classes that the 2 classes inherit from. So this is probably a sign of bad class design I guess. Those common properties/methods should really be in a common super class. If this was the case then I would be able to use this way of making the method generic. – Stackman May 12 '17 at 15:08
2

Your method specifies to use a type T about which all the compiler knows is that it extends Object. Object does not have the methods you invoked. You have to assert that the type has the methods you use. For that you need <T extends Foo>, where Foo is a type with those methods.

ObjectName1 is one of the worst names for a type possible.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
  • Thanks for the info. That makes sense. Regarding the name 'ObjectName1' - I changed the type name when I posted the question here as I didnt want to include the real code. Its just for an example. – Stackman May 12 '17 at 15:04