I’m looking to have a collection of objects that implement a certain interface, but I’d like to only have one per concrete type within the collection.
collection of implementers of dog:
- instance of dachshund
- instance of beagle
- instance of corgi
In .NET, there’s a “KeyedByTypeCollection”. Does something similar exist in Java in such a way that I could use it on Android?
Thanks!
You should look at generics. E.g.:
List<Dogs> dogList = new ArrayList<Dogs>();EDIT: to have only unique instances in your collection, you should use
Set<Dogs> dogList = new HashSet<Dogs>();