Adam
Adam

Reputation: 3795

Return types and arguments of interfaces and lists in Java?

I'm trying to write a function in three classes that will compare two lists of objects of that same class.

General Idea

List<Toads> aTList = ...
List<Toads> bTList = ...
List<Toads> tResult = compare(aTList , bTList ); 

List<Frogs> aFList = ...
List<Frogs> bFList = ...
List<Frogs> fResult = compare(aFList , bFList );

Note: Toad and Frog can both extend a interface that provides necessary funcitons to compare

The issue is that after comparing I would like to use the returned lists (tResult and fResult) with their specific type (Toad and Frog).

I tried something like specified here Java interfaces and return types, but I couldn't get all the types to line up.

I also tried using things like <? extends Amphibian> as the parameter and return type to compare, but then the returned type is Amphibian.

Upvotes: 1

Views: 200

Answers (3)

matt
matt

Reputation: 9401

Perhaps something this is what you're looking for?

public class AmphibianComparer<T extends Amphibian> {
  public List<T> compare(List<T> listOne, List<T> listTwo) {
    ...
  }
}

Upvotes: 2

Isaac Truett
Isaac Truett

Reputation: 8884

You don't actually give the signature for compare. Is this what you want?

<T extends YourInterface> T compare(List<T>, List<T>)

Upvotes: 1

pickypg
pickypg

Reputation: 22332

public <T extends Amphibian> List<T> compare(List<T> a, List<T> b);

Upvotes: 1

Related Questions