Needs:
- Storing objects of a class which overrides equals and hash code
- Will be looping and shoving objects into the datastructure
- Need to be able to call
containsto check whether a certain object is stored in the structure - If
containsreturns true then fetch that specific object from the structure and call a certaingetteron that object
Options I’ve considered:
-
Map – this works for all the needs but I don’t really have a map (key and a value). all I have is bunch of objects. Would it be a good practice to forcefully use a map by storing objects as key and integer or something in the value?
-
Set would work, however, it doesn’t have a fetch method like get.
- List would also work, but it doesn’t have a method to fetch that is non index based. Meaning, once
containsreturns true I’ll have to loop through the list to find the index of my particular object and then fetch it.
I’m open to using different libraries like apache commons or guava for example.
Listhas anindexOf(Object)method which will do exactly what you want.