I often face the problem to have a collection whose elements are accessed both via index and via key.
A collection that contains the rows rapresented in a table (JTable or anything else) must first of all be accessed via integer index, but each row has likely a 'key' used to identify that row.
Another exapmple involve jaxb mapping, I'll talk about it in a possible thread :-)
thanks in advance
You can maintain two collections. One Map from Key to entry, not the index and another a List.
However, as your collections are likely to be small (ie << 1 million entries) a brute force search will be so fast it won't make a difference on a GUI.
Searching through 10K Strings takes about 25 us, 100K String takes about 900 us, 1 million Strings takes about 7000 us or 7ms which a refresh rate for a fast gaming console (but no human can see).
It's for the sake of readibility, a simple
is much more readable than the cycle-based equivalent.
I find that this situation is frequent enough to embed the possible search code in the 'general' collection class and not in the businness logic.