I am not able to figure out solution to the below problem :
Problem statement : There is a class (GenericObject) which is a generic class and should be able to accept any type of objects.
Now I want to create objects of this class through a factory class. This factory class should give me either single object or a list of objects.
Now, single generic object I am able to get for object of my choice but I am not able to figure out the same for list of generic objects.
Below is the sample code :
package com.ge.hc.lcs.axis.components.factory;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String args[])
{
GenericObject<Integer> genericObject=Factory.getSingleInstance();
//below line does not compile
List<GenericObject<Integer>> genericObjectList=Factory.getListOfInstance();
}
}
class Factory{
public static GenericObject getSingleInstance()
{
return new GenericObject<>();
}
public static List<GenericObject> getListOfInstance()
{
List<GenericObject> genericObjectList=new ArrayList<>();
genericObjectList.add(new GenericObject<>());
genericObjectList.add(new GenericObject<>());
return genericObjectList;
}
}
class GenericObject<T>{
void add(T object)
{
System.out.println(object.getClass());
}
}
Now, below line works fine :
GenericObject<Integer> genericObject=Factory.getSingleInstance();
But this does not :
//below line does not compile
List<GenericObject<Integer>> genericObjectList=Factory.getListOfInstance();
How can I achieve this. Any sort of ideas or suggestions are appreciated.