How can I concat two linked lists in O(1) with Java via jdk1.6, google or apache commons collection or whatever? E.g. in the jdk there is only the addAll method which is O(n).
Another feature I miss is to concat two lists where each of them could be in inverse order. To illustrate this assume two lists a->b->c and e->f->g could merged into
- a->b->c->e->f->g
- a->b->c->g->f->e
- c->b->a->e->f->g
- c->b->a->g->f->e
Do you know of such a list implemenation or do I have to implement my own linked list? It would be also helpful to know how to tweak existing solutions (e.g. the jdk LinkedList has a lot of private methods only). These features seems to me very obvious, hopefully I am not missing something stupid.
As MicSim pointed out the question Merge two lists in constant time in Java is related but not a real duplicate! Now the questions are:
- is it possible with other collection libs?
- how to concat the inverse?
6 Answers 6
If you are willing to settle for Iterable result, you can use google-collections Iterables.concat and Iterables.reverse
http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Iterables.html
public static <T> Iterable<T> concat(Iterable<? extends T> a,
Iterable<? extends T> b)
public static <T> Iterable<T> concat(Iterable<? extends T> a,
Iterable<? extends T> b,
Iterable<? extends T> c)
public static <T> Iterable<T> concat(Iterable<? extends T> a,
Iterable<? extends T> b,
Iterable<? extends T> c,
Iterable<? extends T> d)
public static <T> Iterable<T> concat(Iterable<? extends T>... inputs)
public static <T> Iterable<T> concat(Iterable<? extends Iterable<? extends T>> inputs)
-
I'd say look at the code - I suspect that it will perform less well than addAll will.TofuBeer– TofuBeer03/22/2010 18:59:40Commented Mar 22, 2010 at 18:59
-
The methods create a new object that delegates to the original list(s). It's always good to check performance/memory impact, as it depends on the usage.Yardena– Yardena03/22/2010 20:10:47Commented Mar 22, 2010 at 20:10
-
2With the Iterables methods, you won't copy the list elements, resulting in less memory usage and probably a speed improvement compared to addAll.Jared Levy– Jared Levy03/28/2010 01:33:39Commented Mar 28, 2010 at 1:33
-
Using ArrayList I have noticed that it is much faster to simply use addAll and generate a new list when iterator is often accessed. This was happening so often in our code, we had a performance increase of up to 50 % when using addAll. ;-)trevore– trevore09/29/2015 09:48:11Commented Sep 29, 2015 at 9:48
The only solution I see at the moment is to implement List, make a constructor like:
public EnhancedList (List l1, List l2)
and override all methods. In such solution it's actually not important whether you want to concat LinkedLists or any other lists.
-
I actually though there would be an out-of-the-box solution. If I had to roll out my own I would do it via MyLinkedList.addAll(Collection) -> detect LinkedLists and to create the inverse: MyLinkedList.inverse()rocker– rocker03/22/2010 17:21:05Commented Mar 22, 2010 at 17:21
-
3I was just proposing the same solution (so +1 ;)). I think that what Roman is proposing is to implement a List interface such that you can use this implementation as a single list while its methods works on both the single lists from which it has been created. For example the new
get
method will return an element from the first list or the second list:public T get(int i) { return i < list1.size() ? list1.get(i) ? list2.get(i - list1.size()); }
Andrea Zilio– Andrea Zilio03/22/2010 17:34:14Commented Mar 22, 2010 at 17:34
I'd think this wouldn't be too difficult to write with any kind of base list construct since insertion in the middle or the end of a list in a Linked list is O(1).
-
4The problem is that a
LinkedList
is a stand-alone object in Java and doing that operation while also keeping bothLinkedList
objects intact and standalone is not a O(1) operation, as you'll have to copy one list.Joachim Sauer– Joachim Sauer03/22/2010 17:02:01Commented Mar 22, 2010 at 17:02 -
Good point, I failed to dig deep enough into the Java specific objects.Jason M– Jason M03/22/2010 19:08:19Commented Mar 22, 2010 at 19:08
Adapting the LinkedList to get O(1) concat in the jdk will work if:
- the method entry() and the variables header and size and the inner class Entry would be protected
addAll would detect LinkedList and then doing sth. like:
JdkLinkedList secondList = (JdkLinkedList) secondColl; int numNew = secondList.size(); if (numNew == 0) return false; modCount++; Entry<E> successor = (index == size ? header : entry(index)); Entry<E> predecessor = successor.previous; // TODO LATER if reverse // connect the last element of this list with the first element of the second list // linked list is implemented as a 'cycle' => header.next == first element of second list Entry<E> first = secondList.header.next; predecessor.next = first; first.previous = predecessor; // append the last element of the second list with the rest of this list Entry<E> last = secondList.header; successor.previous = last; last.next = successor;
For the concat I would suggest you do the following:
- Make sure all of your parameters/variables are declared as List<...> not LinkedList<...>
- Change the new LinkedList<...>(); to new ArrayList<...>();
- profile the application
- Change the new ArrayList<...> to new LinkedList<...>();
- profile the application
Depending on your usage ArrayList can be significantly faster than LinkedList. Also looking at the profile data you can see how much of a performance hit you have by using addAll - if it isn't that large don't bother "fixing" it.
For some things your experiences with other languages will not hold true. You may find that addAll meets your requirements in Java.
If you were to write your own concatable list make sure it conforms to the List interface then change your code and re-profile it and make sure it is faster. If it isn't then throw it away and stick with the standard List types.
-
I'm sure in my usecase the concat of a linkedlist will be faster then the system.copy of arraylistrocker– rocker03/22/2010 20:47:31Commented Mar 22, 2010 at 20:47
-
More to that... if you are iterating over the list a lot, for example, the cost of iterating over the linkedlist will be more than the cost of iterating over the arraylist. As such the time spent in the addAll may cancel out the time sent iterating. There are other things like that that could make the overall performance of ArrayList faster than a LinkedList. But really, never assume when it comes to performance - always measure.TofuBeer– TofuBeer03/22/2010 23:23:31Commented Mar 22, 2010 at 23:23
-
of course I will test the difference! But the question was about a fast concat method not about overall performance.rocker– rocker03/24/2010 08:50:00Commented Mar 24, 2010 at 8:50
-
but a fast concat may be pointless in terms of performance, which might explain why one doers not exist.TofuBeer– TofuBeer03/24/2010 17:03:32Commented Mar 24, 2010 at 17:03
FastList from javolution is not an out-of-the-box solution but with the tail() and head() quite close to my favourite.
I think trove is the solution, although no convenient method exist for invert or addAll in O(1).
Explore related questions
See similar questions with these tags.
firstList.getLastElement().setNextElement(secondList.getFirstElement())
secondList
.