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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:08:19+00:00 2026-06-15T14:08:19+00:00

I have multiple Integer ArrayList , which contains some duplicate elements. I want to

  • 0

I have multiple Integer ArrayList, which contains some duplicate elements. I want to get the unique elements from them. But how?
java.util.ArrayList.removeAll() is not serving my purpose completely. See the below test code-

ArrayList<Integer> d = new ArrayList<Integer>();
d.add(2);
d.add(4);
d.add(5);
d.add(7);
d.add(8);
d.add(9);

ArrayList<Integer> e = new ArrayList<Integer>();
e.add(3);
e.add(7);

d.removeAll(e);

for (int t : d) {
    System.out.print(t+", ");
}

In output, i am getting 2, 4, 5, 8, 9, . Clearly 3 is missing. Also just to keep it simple, I am using only two ArrayList here but in my code, i have more than two ArrayList.

How I can find unique elements in multiple ArrayList in Java

  • 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-15T14:08:20+00:00Added an answer on June 15, 2026 at 2:08 pm

    Create a Set<Integer> from your List<Integer>. The set will contain no duplicate objects:

    List<Integer> lstNumbers = new ArrayList<Integer>();
    //fill the list of integers...
    Set<Integer> setNumbers = new HashSet<Integer>(lstNumbers);
    //the set will contain no duplicate values...
    for (int t : setNumbers) {
        System.out.print(t+", ");
    }
    

    Note that you can add more List<Integer> in the Set by using the Set#addAll method (as shown in rahulroc answer) to add more integers in your not duplicated elements collection:

    //assuming setNumbers has been initialized before
    setNumbers.addAll(anotherListOfNumbers);
    setNumbers.addAll(andAnotherListOfNumbers);
    

    Also, as best practice, try to program to interfaces (List, Set, etc), not to class implementations (ArrayList, HashSet), as shown here: What does it mean to “program to an interface”?

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

Sidebar

Related Questions

I have multiple domains on my server. I want to redirect all of them
i have an array of names which have multiple files and folders...now i want
I have 2 arraylists. One is principleList which contains integer values that denotes roles
I have multiple projects which are to be hosted together in a Tomcat container,
I have multiple forms for lots of products on my page and want to
I have multiple input fields on my page. I Regex them with a css
I'm witnessing some strange behaviour from Fluent nHibernate. I have two objects involved in
I have two tables, charts and chart_entries and I want to get the current
Trying to efficiently extract some numbers from a string and have tried java.util.regex.Matcher com.google.common.base.Splitter
Possible Duplicate: Adding multiple JProgressBar to TableColumn of JTable i have a jTable with

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.