I am searching for a list data structure in Java which allows cheap appending of long lists. I tried LinkedList but I found in the documentation of addAll, that an iterator is used to append the two lists. This means that the list, which gets appended, gets cloned during the operation. The iterator walks though the whole list returning every single element. Is there any collection available which omits the iteration while appending two lists?
Share
You can use Guava’s Iterables.concat method to create concatenated Iterable View..
Iterablewhich is not a list)Basically it creates an
Iterablethrough which you can iterate over thetwolists back to back (It iterates elements from list1 then from list2..NOTE: – If you want a list as a concatenation of the
two lists, then this might not help you much.. Because, it does not create a list, but an Iterable.. For that case, you have no other choice thanIteratingover your lists andcopyeach of your reference..From the docs: –
You also have a
var-argsversion of this method.. See Docs.. This can take any number of lists, and returns Iterables that can iterate over those lists in order.. So, you can do like this..This link –> google-guava-libraries-essentials might also be of interest to you..