Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

503
Vistas
What is Type & TypeToken?

I have the following method in my project

 public void execute(final int apiId, final ResponseHandler handler, final Type type)

and the type is determined using TypeToken as follows

final Type serviceErrorType = new TypeToken<>() {
    }.getType();

I went through this link here but couldn't understand completely about Type and TypeToken

Can anyone please share a link or help understand these two concepts?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

From the link you provided:

Forces clients to create a subclass of this class which enables retrieval the type information even at runtime.

Let's speak with example.

Assume you want to parse a JSON to Java class with Gson library. Now, you have to specifically tell Gson, that:

I want this JSON to be translated to a List of User objects

How would you tell that to Gson? If it was only User, you could tell User.class. But it isn't possible to say List<User>.class

new Gson().fromJson(json, new TypeToken<List<User>>(){}.getType())

But now you can specify precisely that Gson should convert it to a List of Users.

Should quote explanation from docs:

Represents a generic type T. Java doesn't yet provide a way to represent generic types, so this class does.

Have a look at this blog post for more details on the topic.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda