I'd like to create the following:
//infer the type from parameter but restrict it to one of
// Proxy's subtype. return a list of this sub type
public static List<T> process(<T extends Proxy> proxy)
{
return new ArrayList<T>(); //just for example
}
Usage:
List<ConcreteTypeOfProxy> list = process(new ConcreteTypeOfProxy());
The above example has compilation issues. I think this should logically be available in java, just not sure what the syntax is
//Any array that extends T
public static <T extends Proxy> List<T> process(T proxy)
{
return new ArrayList<T>(); //just for example
}
//Returns array that has T type of parameter
public static <T> List<T> process(T proxy)
{
return new ArrayList<T>(); //just for example
}
//Returns a map of generic type which you define in the method
public static <T, E extends Proxy> Map<T, E> process(T key, E value)
{
Map<T, E> map = new HashMap<T, E>();
map.put(key, value);
return map;
}
You don't need any method, and consequently you don't need any parameters, to do this:
List<ConcreteTypeOfProxy> list = new ArrayList<>();
Remember: there is no difference between ArrayList<ConcreteTypeOfProxy>
and ArrayList<AnyOtherType>
: it's just an ArrayList
.
The type parameter is merely an instruction to the compiler to check the type of what is added - at compile time only - and to automatically cast values obtained from the list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.