How can I make a List
or similar Collection
that represents a subset of another collection, masking (filtering out) unwanted elements but without creating an entirely new collection structure?
The second list could then be more quickly modified by enabling/disabling the visibility of individual elements. Doing so should be more performant than constantly rebuilding a second separate collection structure.
I imagine some kind of bit-map view into the original collection.
Ideally this would be thread-safe, and would fail-fast if the original collection were modified.
For example, two masked collections backed by the same master collection:
- The master collection might be [ dog , cockatiel , cat , lion , wolf , hummingbird ].
- A masked collection might be named
canine
containing [ dog , wolf ] with no references to the other elements. - Another masked collection might be name
feline
containing [ cat , lion ].
Another example: We have a list of many LocalDate
objects. The user selects some of those dates for some purpose, perhaps selecting only weekdays but not weekends. Then the user changes their selection, making a manual exception for certain dates. Rather than build a new list each time of selected elements, a masked collection would be tracking which ones are selected with the others being ignored by omission.