I am looking for a data structure implementing List
that allows me to reuse parts of the list occurring in multiple lists.
To illustrate this consider the following three lists:
As you can see, the red sequence (42, 88, 76, 60
) and the blue sequence (21, 9, 47
) are used multiple times, each representing an independent List shared as part of other lists. In reality, the lists could be much longer.
The list only needs to be read-only as it will be used as a return value. The procedure creating the collection of lists will be the only one (allowed to) to modify the list (use add(..)
operations etc.). Sharing parts of the lists will make it redundant to expansively copy list elements using addAll(..)
(which internally uses System.arraycopy(..)
).
Are there any light-weighted List
implementations out there to fulfill my needs? Or is there a relatively simple way of implementing one myself?
For Iterable
, Set
, and Multiset
, Guava has you covered with Iterables.concat()
, Sets.union()
, and Multisets.sum()
respectively. Lists
, unfortunately, lacks the equivalent method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With