Merge two lists in constant time in Java

前端 未结 4 1711
再見小時候
再見小時候 2020-12-11 01:35

Does anyone know if it\'s possible to merge two lists (or any collection) in constant time in Java ?

http://www.cppreference.com/wiki/stl/list/splice

It\'s s

4条回答
  •  既然无缘
    2020-12-11 02:34

    If it's easy to do with a linked list in C, then I'd guess that the LinkedList class offers the same performance

    All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

    List list1 = new LinkedList();
    list1.add(...);
    List list2 = new LinkedList();
    list2.add(...);
    
    list1.addAll(list2);
    

    edit: Nevermind. Looks like LinkedList.addAll(Collection) invokes LinkedList.addAll(int, Collection) which iterates through the new collection.

提交回复
热议问题