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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:36:45+00:00 2026-05-12T00:36:45+00:00

What is the simplest way to find if two Lists contain exactly the same

  • 0

What is the simplest way to find if two Lists contain exactly the same elements, in the standard Java libraries?

It shouldn’t matter if the two Lists are the same instance or not, and it shouldn’t matter if the type parameter of the Lists are different.

e.g.

List list1
List<String> list2; 
// ... construct etc

list1.add("A");
list2.add("A"); 
// the function, given these two lists, should return true

There’s probably something staring me in the face I know 🙂


EDIT: To clarify, I was looking for the EXACT same elements and number of elements, in order.

  • 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-12T00:36:45+00:00Added an answer on May 12, 2026 at 12:36 am

    If you care about order, then just use the equals method:

    list1.equals(list2)
    

    From the javadoc:

    Compares the specified object with
    this list for equality. Returns true
    if and only if the specified object is
    also a list, both lists have the same
    size, and all corresponding pairs of
    elements in the two lists are equal.
    (Two elements e1 and e2 are equal if
    (e1==null ? e2==null :
    e1.equals(e2)).) In other words, two
    lists are defined to be equal if they
    contain the same elements in the same
    order. This definition ensures that
    the equals method works properly
    across different implementations of
    the List interface.

    If you want to check independent of order, you could copy all of the elements to Sets and use equals on the resulting Sets:

    public static <T> boolean listEqualsIgnoreOrder(List<T> list1, List<T> list2) {
        return new HashSet<>(list1).equals(new HashSet<>(list2));
    }
    

    A limitation of this approach is that it not only ignores order, but also frequency of duplicate elements. For example, if list1 was ["A", "B", "A"] and list2 was ["A", "B", "B"] the Set approach would consider them to be equal.

    If you need to be insensitive to order but sensitive to the frequency of duplicates you can either:

    • sort both lists (or copies) before comparing them, as done in this answer to another question
    • or copy all elements to a Multiset
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to find simplest way to merge two arrays into the third one,
What is the simplest way to find the Public-Key-Token of an assembly? The simplest
What's the simplest way to do a find and replace for a given input
I am trying my hardest to find the simplest way to create a basic
What is the simplest most basic way to find out if a number/variable is
I'm trying to find the simplest way to query Active Directory, in one of
What's the simplest way in actionscript to find if number is between -20 and
I know this is basic, but I couldn't find the simplest way to iterate
I am very new to ET, but I can't find the simplest way to
I'm trying to find the simplest way to fix my live wallpaper in portrait

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.