How to use generic type in a map?

Mincong Huang

Previously, I wrote a method for a specific entity class called Address and here's the code :

private List<AddLuceneWork> buildAddLuceneWorks(List<Address> addresses) {
    List<AddLuceneWork> addWorks = new LinkedList<>();
    session = em.unwrap(Session.class);
    searchIntegrator = ContextHelper.getSearchintegrator(session);
    entityIndexBinding = searchIntegrator
            .getIndexBindings()
            .get(Address.class);  // specific type
    // ...
    return addWorks;
}

And now, I want to make the input to be generic, so that no matter what entity can be processed. But I didn't use generic type before and I don't know how to use it properly :

private <T> List<AddLuceneWork> buildAddLuceneWorks(List<T> entities) {
    List<AddLuceneWork> addWorks = new LinkedList<>();
    session = em.unwrap(Session.class);
    searchIntegrator = ContextHelper.getSearchintegrator(session);
    entityIndexBinding = searchIntegrator
            .getIndexBindings()
            .get(Address.class);  // <- how to change it to generic ?
    // ...
    return addWorks;
}

I tried the following, but they didn't work :

  • .get(T);
  • .get(T.class);
  • .get(clazz.getClass()); where Class<T> clazz = null;
Nicolas Filotto

Due to Type Erasure your code at Runtime is actually

private List buildAddLuceneWorks(List entities) {

Such that it cannot work, you need to add an additional parameter of type Class to provide the expected type explicitly like this:

private <T> List<AddLuceneWork> buildAddLuceneWorks(List<T> entities, Class<T> clazz) {

Then get(Address.class) will then be get(clazz)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to use generic protocol as a variable type

From Dev

How to use generic types to get object with same type

From Dev

How do I use a generic method in a derived type

From Dev

How to use generic function interface method type parameters in a lambda

From Dev

How to declare and Use Generic Map in this case

From Dev

How do I use a generic type projection in a method parameter?

From Dev

How to use a type array in map in c++

From Dev

Create a map with a generic type

From Dev

How to use a helper generic of type class of type

From Dev

Use Self as generic type

From Dev

Generic type use, in Java

From Dev

How do I use a generic method to build a list of same type?

From Dev

How do I use custom deserialization on gson for generic type?

From Dev

How to map a parametrized enum from a generic type to another?

From Dev

how to use GSON to parse a class with INNER generic type?

From Dev

How to use a swift class with a generic type in objective c

From Dev

How to use Class type Generic in a method

From Dev

how to use a class with generics without having to declare the generic type

From Dev

Trying to learn how to write methods that use generic type

From Dev

Golang Type Switch: How to match a generic slice/array/map/chan?

From Dev

How to add values for java generic map with undetermined "?" value type

From Dev

Java - Map generic type to consumer of said type

From Dev

How to use Cassandra Map type in Java

From Dev

How to Map MongoDB Bson Dosument in F# to generic type

From Dev

how do you assign a non-generic Map to a generic Map in flow type

From Dev

How to declare and Use Generic Map in this case

From Dev

How can I define in JAVA generic type that extends Map object with 2 generic parameters?

From Dev

How do I use an extended generic type's type in a class?

From Dev

Java Generic Map with Generic Type as key/ values

Related Related

  1. 1

    How to use generic protocol as a variable type

  2. 2

    How to use generic types to get object with same type

  3. 3

    How do I use a generic method in a derived type

  4. 4

    How to use generic function interface method type parameters in a lambda

  5. 5

    How to declare and Use Generic Map in this case

  6. 6

    How do I use a generic type projection in a method parameter?

  7. 7

    How to use a type array in map in c++

  8. 8

    Create a map with a generic type

  9. 9

    How to use a helper generic of type class of type

  10. 10

    Use Self as generic type

  11. 11

    Generic type use, in Java

  12. 12

    How do I use a generic method to build a list of same type?

  13. 13

    How do I use custom deserialization on gson for generic type?

  14. 14

    How to map a parametrized enum from a generic type to another?

  15. 15

    how to use GSON to parse a class with INNER generic type?

  16. 16

    How to use a swift class with a generic type in objective c

  17. 17

    How to use Class type Generic in a method

  18. 18

    how to use a class with generics without having to declare the generic type

  19. 19

    Trying to learn how to write methods that use generic type

  20. 20

    Golang Type Switch: How to match a generic slice/array/map/chan?

  21. 21

    How to add values for java generic map with undetermined "?" value type

  22. 22

    Java - Map generic type to consumer of said type

  23. 23

    How to use Cassandra Map type in Java

  24. 24

    How to Map MongoDB Bson Dosument in F# to generic type

  25. 25

    how do you assign a non-generic Map to a generic Map in flow type

  26. 26

    How to declare and Use Generic Map in this case

  27. 27

    How can I define in JAVA generic type that extends Map object with 2 generic parameters?

  28. 28

    How do I use an extended generic type's type in a class?

  29. 29

    Java Generic Map with Generic Type as key/ values

HotTag

Archive