Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing parts of lists to avoid copying

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:

enter image description here

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?

like image 461
Frithjof Avatar asked Feb 09 '16 20:02

Frithjof


1 Answers

For Iterable, Set, and Multiset, Guava has you covered with Iterables.concat(), Sets.union(), and Multisets.sum() respectively. Lists, unfortunately, lacks the equivalent method.

like image 122
Douglas Avatar answered Nov 16 '22 11:11

Douglas