Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java 7 Generics
interface Entity<ID> {
ID getId();
setId( ID id );
}
class Repository<ID, ENTITY extends Entity<ID>> {
Map<ID, ENTITY> identityMap = new HashMap<>();
void add( ENTITY entity ) {
identityMap.put( entity.getId(), entity );
}
}
// ID could be an Integer, UUID, String, or anything, ENTITY could be anything that
// implements the Entity interface, and it's all strict typed at compile time, so you can't
// add a UUID, User to a repository specified with Integer, Post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.