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 3403788
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:18:12+00:00 2026-05-18T05:18:12+00:00

I am brand new to Java :) I have 2 String lists and I

  • 0

I am brand new to Java 🙂

I have 2 String lists and I was wondering what would be the most efficient way to compare the two and have a resulting array which contains strings that are not in the other. For example, I have a list called oldStrings and one called Strings. I have seen the Comparator function but don’t fully understand how it works, right now I was thinking I could create a for loop, loop through each string and then save that string:

for (final String str : oldStrings) {
  if(!strings.contains(str))
  {                     
    getLogger().info(str + " is not in strings list ");
  }
}

There’s going to be up to 200 strings in this list. Would this be the best way to go about this? Thank you!

  • 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-18T05:18:13+00:00Added an answer on May 18, 2026 at 5:18 am
    Collection firstList = new ArrayList() {{
        add("str1");
        add("str2");
    }};
    
    Collection secondList = new ArrayList() {{
        add("str1");
        add("str3");
        add("str4");
    }};
    
    
    System.out.println("First List: " + firstList);
    System.out.println("Second List: " + secondList);
    
    // Here is main part
    secondList.removeAll(firstList);
    
    System.out.println("Result: " + secondList);  
    

    Update:
    More sophisticated version of code

    Collection<String> firstList = new ArrayList<String>();
    firstList.add("str1");
    firstList.add("str2");
    
    Collection<String> secondList = new ArrayList<String>();
    secondList.add("str1");
    secondList.add("str2");
    secondList.add("str3");
    
    
    System.out.println("First List: " + firstList);
    System.out.println("Second List: " + secondList);
    
    // Here is main part
    secondList.removeAll(firstList);  
    

    Update:

    To Get acctual difference between both String list go for this.

        Set<String> setOne = new HashSet<String>();        
        Set<String> setTwo = new HashSet<String>();
        setOne.add("1");
        setOne.add("2");
        setOne.add("5");
        setTwo.add("1");
        setTwo.add("3");
        setTwo.add("4");
        Set<String> setTwoDummy = new HashSet<String>(setTwo);
        setTwo.retainAll(setOne);        
        setTwoDummy.addAll(setOne);
        setTwoDummy.removeAll(setTwo);
        System.out.println(""+setTwoDummy);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a brand new Java developer (I have been working in asp.net) and
new to java and brand new to the site. I have a JLabel added
First of all i am brand new to Java : / I have been
I'm brand new to java, coming from a ruby world. One thing I love
Brand new to the Log4Net library and there is something I have not been
I have the following code: InputStream reportFile = MyPage.this.getClass().getResourceAsStream(test.jrxml); HashMap<String, String> parameters = new
I am brand new to developing for android and have hit something of a
I'm brand new to Java, programming, and StackOverflow. I need to use a list
I am brand new to WCF, and have limited experience coding against web services.
I'm brand new to JAva/Eclipse/Android but gearing up! I've got everything installed and working

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.