Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Java collection allows cheap appending?

Tags:

java

list

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?

like image 454
ceving Avatar asked Oct 10 '12 11:10

ceving


People also ask

Which collection is best for insertion in Java?

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.

Which is faster collection in Java?

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).

Which collection is best for insertion and deletion in Java?

If you only insert and delete at the end of the list, then ArrayList is the way to go.

How do you append to a List in Java?

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.


3 Answers

You can use Guava's Iterables.concat method to create concatenated Iterable View..

Iterable<T> combined = Iterables.concat(list1, list2);
  • This does not copy elements from one list to another..
  • So, it does not change any of your lists..
  • Also, this does not create a new list (It creates an 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..

like image 103
Rohit Jain Avatar answered Oct 25 '22 04:10

Rohit Jain


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.

like image 5
Tudor Avatar answered Oct 25 '22 02:10

Tudor


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.

like image 4
RNJ Avatar answered Oct 25 '22 03:10

RNJ