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?
The best general purpose or 'primary' implementations are likely ArrayList , LinkedHashMap , and LinkedHashSet . Their overall performance is better, and you should use them unless you need a special feature provided by another implementation. That special feature is usually ordering or sorting.
If you need fast access to elements using index, ArrayList should be choice. If you need fast access to elements using a key, use HashMap. If you need fast add and removal of elements, use LinkedList (but it has a very poor seeking performance).
If you only insert and delete at the end of the list, then ArrayList is the way to go.
There are two methods to add elements to the list. add(E e): appends the element at the end of the list. Since List supports Generics, the type of elements that can be added is determined when the list is created. add(int index, E element): inserts the element at the given index.
You can use Guava's Iterables.concat method to create concatenated Iterable View..
Iterable<T> combined = Iterables.concat(list1, list2);
Iterable
which is not a list)Basically it creates an Iterable
through which you can iterate over the two
lists 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 than Iterating
over your lists and copy
each of your reference..
From the docs: -
It Combines two iterables into a single iterable. The returned iterable has an iterator that traverses the elements in a, followed by the elements in b. The source iterators are not polled until necessary. The returned iterable's iterator supports remove() when the corresponding input iterator supports it.
You also have a var-args
version 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..
Iterable<T> combined = Iterables.concat(list1, list2, list3, list4, ...);
This link --> google-guava-libraries-essentials might also be of interest to you..
Not really, since all "append" operations don't make any assumptions about the underlying collections. Technically two linked lists could be appended directly, but the append has to be generic so it uses iteration.
Another good reason to not allow direct concatenation is the fact that after the append changing one list will also affect the other, which I'm sure is not a desirable property.
addAll in ArrayList converts it to an Array and then uses a system call to copy (System.arraycopy). This should be quicker than looping through in java as it is native, I dont think there is a cheap appender.
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