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;
}







1















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?










share|improve this 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


















1















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?










share|improve this 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














1












1








1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












1 Answer
1






active

oldest

votes


















3














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);





share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    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);





    share|improve this answer




























      3














      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);





      share|improve this answer


























        3












        3








        3







        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);





        share|improve this answer













        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);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 9:27









        Andy TurnerAndy Turner

        84.9k983145




        84.9k983145

















            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas