Java generic return sub collection of type from parameters [closed]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
T: a type
C: a sub type of Collection
R: the return of comination of T and C
Example:
T = StackOverflow.class
C = List.class
R = List<StackOverflow>
How to exprime that in method signature?
It should be written like this?
<T, <C extends Collection>, R extends C<T>> HttpResponse<R>
process(Class<T> returnType, Class<C> parametrizedReturnType)
I know C<T>
does not work.
In order to do
List<StackOverflow> res = process(StackOverflow.class, List.class)
Any ideas?
java generics
closed as unclear what you're asking by il_raffa, Raedwald, Mark Rotteveel, grizzthedj, Rob Jan 4 at 13:37
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
T: a type
C: a sub type of Collection
R: the return of comination of T and C
Example:
T = StackOverflow.class
C = List.class
R = List<StackOverflow>
How to exprime that in method signature?
It should be written like this?
<T, <C extends Collection>, R extends C<T>> HttpResponse<R>
process(Class<T> returnType, Class<C> parametrizedReturnType)
I know C<T>
does not work.
In order to do
List<StackOverflow> res = process(StackOverflow.class, List.class)
Any ideas?
java generics
closed as unclear what you're asking by il_raffa, Raedwald, Mark Rotteveel, grizzthedj, Rob Jan 4 at 13:37
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
What is 'comination', what is 'exprime'?
– Mark Rotteveel
Jan 4 at 13:03
add a comment |
T: a type
C: a sub type of Collection
R: the return of comination of T and C
Example:
T = StackOverflow.class
C = List.class
R = List<StackOverflow>
How to exprime that in method signature?
It should be written like this?
<T, <C extends Collection>, R extends C<T>> HttpResponse<R>
process(Class<T> returnType, Class<C> parametrizedReturnType)
I know C<T>
does not work.
In order to do
List<StackOverflow> res = process(StackOverflow.class, List.class)
Any ideas?
java generics
T: a type
C: a sub type of Collection
R: the return of comination of T and C
Example:
T = StackOverflow.class
C = List.class
R = List<StackOverflow>
How to exprime that in method signature?
It should be written like this?
<T, <C extends Collection>, R extends C<T>> HttpResponse<R>
process(Class<T> returnType, Class<C> parametrizedReturnType)
I know C<T>
does not work.
In order to do
List<StackOverflow> res = process(StackOverflow.class, List.class)
Any ideas?
java generics
java generics
edited Jan 4 at 10:13
Maryo
asked Jan 4 at 8:51
MaryoMaryo
150216
150216
closed as unclear what you're asking by il_raffa, Raedwald, Mark Rotteveel, grizzthedj, Rob Jan 4 at 13:37
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by il_raffa, Raedwald, Mark Rotteveel, grizzthedj, Rob Jan 4 at 13:37
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
What is 'comination', what is 'exprime'?
– Mark Rotteveel
Jan 4 at 13:03
add a comment |
What is 'comination', what is 'exprime'?
– Mark Rotteveel
Jan 4 at 13:03
What is 'comination', what is 'exprime'?
– Mark Rotteveel
Jan 4 at 13:03
What is 'comination', what is 'exprime'?
– Mark Rotteveel
Jan 4 at 13:03
add a comment |
1 Answer
1
active
oldest
votes
In order to avoid the issues with raw types (etc) you can pass in something like a "prototype" object:
<T, C extends Collection<T>> HttpResponse<C> process(C prototype) {
...
}
So you can invoke this like:
HttpResponse<List<String>> response = process(new ArrayList<>());
The nature of the prototype object depends upon what you need to do with it in the process
method. Perhaps you can accept an empty list; perhaps you would need to pass in a supplier of an instance, for example:
<T, C extends Collection<T>> HttpResponse<C> process(Supplier<C> supplier) {
return new HttpResponse<>(supplier.get());
}
HttpResponse<List<String>> response = process(ArrayList::new);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In order to avoid the issues with raw types (etc) you can pass in something like a "prototype" object:
<T, C extends Collection<T>> HttpResponse<C> process(C prototype) {
...
}
So you can invoke this like:
HttpResponse<List<String>> response = process(new ArrayList<>());
The nature of the prototype object depends upon what you need to do with it in the process
method. Perhaps you can accept an empty list; perhaps you would need to pass in a supplier of an instance, for example:
<T, C extends Collection<T>> HttpResponse<C> process(Supplier<C> supplier) {
return new HttpResponse<>(supplier.get());
}
HttpResponse<List<String>> response = process(ArrayList::new);
add a comment |
In order to avoid the issues with raw types (etc) you can pass in something like a "prototype" object:
<T, C extends Collection<T>> HttpResponse<C> process(C prototype) {
...
}
So you can invoke this like:
HttpResponse<List<String>> response = process(new ArrayList<>());
The nature of the prototype object depends upon what you need to do with it in the process
method. Perhaps you can accept an empty list; perhaps you would need to pass in a supplier of an instance, for example:
<T, C extends Collection<T>> HttpResponse<C> process(Supplier<C> supplier) {
return new HttpResponse<>(supplier.get());
}
HttpResponse<List<String>> response = process(ArrayList::new);
add a comment |
In order to avoid the issues with raw types (etc) you can pass in something like a "prototype" object:
<T, C extends Collection<T>> HttpResponse<C> process(C prototype) {
...
}
So you can invoke this like:
HttpResponse<List<String>> response = process(new ArrayList<>());
The nature of the prototype object depends upon what you need to do with it in the process
method. Perhaps you can accept an empty list; perhaps you would need to pass in a supplier of an instance, for example:
<T, C extends Collection<T>> HttpResponse<C> process(Supplier<C> supplier) {
return new HttpResponse<>(supplier.get());
}
HttpResponse<List<String>> response = process(ArrayList::new);
In order to avoid the issues with raw types (etc) you can pass in something like a "prototype" object:
<T, C extends Collection<T>> HttpResponse<C> process(C prototype) {
...
}
So you can invoke this like:
HttpResponse<List<String>> response = process(new ArrayList<>());
The nature of the prototype object depends upon what you need to do with it in the process
method. Perhaps you can accept an empty list; perhaps you would need to pass in a supplier of an instance, for example:
<T, C extends Collection<T>> HttpResponse<C> process(Supplier<C> supplier) {
return new HttpResponse<>(supplier.get());
}
HttpResponse<List<String>> response = process(ArrayList::new);
answered Jan 4 at 9:27
Andy TurnerAndy Turner
84.9k983145
84.9k983145
add a comment |
add a comment |
What is 'comination', what is 'exprime'?
– Mark Rotteveel
Jan 4 at 13:03