Tutorial :Detect type of object in ArrayList


I'm attempting to make a class that will convert ArrayLists of objects into ArrayLists of other objects. i.e.

ArrayList<Foo> convert(ArrayList<Bar> input){       //conversion logic  }    ArrayList<Bar> convert(ArrayList<Foo> input){       //conversion logic  }  

Unfortunately Java doesn't want to have two functions with the same name and what it believes to be the same inputs and outputs.

I'm attempting to go a different route. Instead of multiple functions with the same name, I want to make one function that accepts an ArrayList, determines which type of object is inside, does the proper conversion, and returns an ArrayList:

ArrayList convert(ArrayList input){       //conversion logic for Foo         //conversion logic for Bar  }  

Is something like this possible?


How about an interface:

public class Converter<From, To> {      List<To> convert(List<From> input);  }  

And then have as many implementations as you want. For example:

private static final Converter<Foo, Bar> fooToBarConverter = new Converter<Foo, Bar>() {      public List<Bar> convert(List<Foo> input) {          ...      }  }  



public <T, U> ArrayList<U> convert(Class<T> typeIn, ArrayList<T> input){           // dispatch on typeIn  }  

Or better yet

public <T, U, V extends ArrayList<U>> V convert(Class<T> typeIn,   Class<V> typeOut, ArrayList<T> input){           // dispatch on typeIn      return typeOut.cast(yourConversionResult);  }  

Because you might return ArrayList<Foo> or ArrayList<Bar> within the same method and having the proper cast will help you return them without compiler warnings.

Edit: The return type cast for the second sample wasn't going to work. Tried to fix it


You are basically describing a "map" in terms of functional programming. Take a list of objects, apply some operation to each object and accumulate the results in another list. There are libraries out there that implement this stuff already, although i haven't looked recently. I know commons collections has this for pre-generics collections.

the gist of the solution is (similar to mmeyers solution):

public interface Function<From,To> {    public To apply(From);  }    public <From,To> List<To> map(List<From> fromList, Function<From,To> fun)  {    // call fun.apply() on every element in fromList and return a new result list ...  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »