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

  • SEARCH
  • Home
  • 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 823345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:56:04+00:00 2026-05-15T02:56:04+00:00

I have created a method that takes two Collection<String> as input and copies one

  • 0

I have created a method that takes two Collection<String> as input and copies one to the other.

However, I am not sure if I should check if the collections contain the same elements before I start copying, or if I should just copy regardless. This is the method:

 /**
  * Copies from one collection to the other. Does not allow empty string. 
  * Removes duplicates.
  * Clears the too Collection first
  * @param src
  * @param dest
  */
 public static void copyStringCollectionAndRemoveDuplicates(Collection<String> src, Collection<String> dest) {
  if(src == null || dest == null)
   return;

  //Is this faster to do? Or should I just comment this block out
  if(src.containsAll(dest))
   return;

  dest.clear();
  Set<String> uniqueSet = new LinkedHashSet<String>(src.size());
  for(String f : src) 
   if(!"".equals(f)) 
    uniqueSet.add(f);

  dest.addAll(uniqueSet);
 }

Maybe it is faster to just remove the

if(src.containsAll(dest))
    return;

Because this method will iterate over the entire collection anyways.

  • 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-05-15T02:56:04+00:00Added an answer on May 15, 2026 at 2:56 am

    I’d say: Remove it! It’s duplicate ‘code’, the Set is doing the same ‘contains()’ operation so there is no need to preprocess it here. Unless you have a huge input collection and a brilliant O(1) test for the containsAll() 😉

    The Set is fast enough. It has a O(n) complexity based on the size of the input (one contains() and (maybe) one add() operation for every String) and if the target.containsAll() test fails, contains() is done twice for each String -> less performant.

    EDIT

    Some pseudo code to visualize my answer

    void copy(source, dest) {
      bool:containsAll = true;
      foreach(String s in source) {  // iteration 1
        if (not s in dest) {         // contains() test
           containsAll=false
           break
        }
      }
      if (not containsAll) {
        foreach(String s in source) { // iteration 2
          if (not s in dest) {        // contains() test
            add s to dest
          }
        }
      }
    }
    

    If all source elements are in dest, then contains() is called once for each source element. If all but the last source elements are in dest (worst case), then contains() is called (2n-1) times (n=size of source collection).
    But the total number of contains() test with the extra test is always equal or greater then the same code without the extra test.

    EDIT 2
    Lets assume, we have the following collections:

    source = {"", "a", "b", "c", "c"}
    dest = {"a", "b"}
    

    First, the containsAll test fails, because the empty String in source is not in dest (this is a small design flaw in your code ;)). Then you create an temporary set which will be {"a", "b", "c"} (empty String and second “c” ignored). Finally you add everthing to dest and assuming, dest is a simple ArrayList, the result is {"a", "b", "a", "b", "c"}. Is that the intention? A shorter alternative:

    void copy(Collection<String> in, Collection<String> out) {
      Set<String> unique = new HashSet<String>(in);
      in.remove("");
      out.addAll(unique);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer TRY THIS... function create_img($file_name,$dir,$dest_dir,$maxwidth,$maxheight){ /* adjust server memory size, file… May 17, 2026 at 1:38 am
  • Editorial Team
    Editorial Team added an answer The Python Imaging Library (PIL) is the de facto image… May 17, 2026 at 1:37 am
  • Editorial Team
    Editorial Team added an answer a += 2 gets translated to a = a +… May 17, 2026 at 1:37 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Like many other developers out there, I've created iPhone projects that use a UITabBarController
I have the following two table (which are tied in with Spring security -
I have a client application that should be able to handle the following: 1)
I have created a rather complicated piece of (Java) code for a game I
Outline My simple application takes text from a text box and calls an asp.net
In a C++ application that can use just about any relational database, what would
Should a collection of objects also be responsible for creating new objects? Let me
I'm writing a class library that will offer some asynchronous processing, and want to
I have an ASP.Net MVC application with a model which is several layers deep
The ASP.NET web application I am developing needs to support two different types of

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.