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

257
Vistas
Java duplicate generic definition

I'm trying to implement a basic class to create, read, update and delete objects. I'm sure I could find a library to do that but I'm doing it myself for educational purposes.

So I made it that everything that get saved are entities and they has an Identifier of a specific type:

interface Entity<T extends Serializable> extends Serializable
{
    T getIdentifier();
}

Entities are serializable and their identifier as well. Then, I have my EntityPersistor which is supposed to save entity from their content (using serialization) and name them from their key. My only implementation of this persistor for now is saving them on the disk. It is using the identifier as the name of the file to save it into.

Here's my entity persistor:

interface EntityPersistor<K extends Serializable, T extends Entity<K>> extends Iterable<T>
{
    void save(T entity);

    void delete(T entity);

    void deleteById(K id);

    T getById(K id);

    boolean contains(K id);

    List<T> loadAll();
}

So, the problem is I have to redefine the type for the key twice when I want to use it. I would like to write:

interface EntityPersistor<T extends Entity<K extends Serializable>> extends Iterable<T>

but it yields a compiler error. So I have to use it that way:

EntityPersistor<String, Product> persistor = new FileEntityPersistor<>();

which is unconvenient.

Is there a workaround or a better way to do that ? I feel like it will make my declaration less readable but the usage is more important.

EDIT:

I forgot to mention that class Product implements Entity<String>

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

0

Normally there would be a way to remove one or the other, but the problem is that you're using both K and T as parameter types.

There's no way to tell the compiler that the parameter of a method should be the "inner" generic type of the object T (The Entity). Therefore, you need two generics:

One for referring to the "inner" generic type of the Entity (K)

And one for the entity itself:Entity<K> (T)

Due to type erasure, Java wouldn't be able to figure out the parameter type K at runtime without your declaration of it.

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