Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8193031

Collections.addAll is likely to perform worse than Collection.addAll

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 17
    • None
    • core-libs

      The static method Collections.addAll claims to
      """to run significantly faster under most implementations"""
      but this seems dubious, since all it does it optimize away one call to Arrays.asList, but at the cost of not taking advantage of any "bulk add" optimizations made by the collection in question. Concurrent collections like ConcurrentLinkedQueue can add an entire chain of elements with a single CAS. CopyOnWriteArrayList makes a copy of the backing array on each call to add. Even for ArrayList, sublists have O(N + M) addAll while a call to add in a loop is O(N * M).

            martin Martin Buchholz
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: