Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7764221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:51:20+00:00 2026-06-01T14:51:20+00:00

The implementation of the addAll method in java.util.Collections simply loops through the source collection

  • 0

The implementation of the addAll method in java.util.Collections simply loops through the source collection and calls the add method of the receiving collection for each element in the source collection.

Thus, resizing of the receiving collection’s underlying data structure could occur multiple times if the capacity of the receiving collection is small and we are adding many elements to it. Each resize will be an O(n) operation.

It seems that a good collections addAll method should exist that will check the number of elements we will be adding and set the capacity of the receiving collection once at the beginning (if necessary). Does such a utility method exist? And if not, why not?

Clarification: I realize that there are implementation specific addAll methods (as in ArrayList) that have this desired behavior. I am wondering if there are Collection utility classes that will get me this behavior in a way that works across all Collection implementation classes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:51:22+00:00Added an answer on June 1, 2026 at 2:51 pm

    Such a utility method cannot exists for a generic Collection, because the necessary method ensureCapacity(int) is not in any interface but only on implementations which have some benefit for that. Currently this are ArrayList, BitSet and Vector. Both ArrayList and Vector have adjusted addAll methods. BitSet is something completely different 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The implementation of Enumerable.AsEnumerable<T>(this IEnumerable<T> source) simply returns source . However Observable.AsObservable<T>(this IObservable<T> source)
The Java API docs say the following about Collections.addAll The behavior of this convenience
I am getting java.util.concurrentmodificationexception for my implementation below try{ Date fromDate = new Date().parse(yyyy-MM-dd,
I need to implement a bag data structure using the interface java.util.Collection . I'm
My implementation of a getHandler method, which is defined on a Java interface, has
I'm trying to write a method which takes a Map[K, Collection[V]] and converts it
The implementation is through a linked list. Would something like this be legal? int
Which implementation from scala.collection.mutable package should I take if I intend to do lots
Does java's TrustManager implementation ignore if a certificate has expired? I tried the following:
This is a follow up question to Is there a basic Java Set implementation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.