{"id":65126,"date":"2020-08-31T06:47:48","date_gmt":"2020-08-31T06:47:48","guid":{"rendered":"https:\/\/www.baeldung.com\/?p=86396"},"modified":"2020-08-31T06:47:48","modified_gmt":"2020-08-31T06:47:48","slug":"assert-two-lists-for-equality-ignoring-order-in-java","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2020\/08\/31\/assert-two-lists-for-equality-ignoring-order-in-java\/","title":{"rendered":"Assert Two Lists for Equality Ignoring Order in Java"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<h2 data-id=\"overview\">1. Overview<\/h2>\n<div class=\"bd-anchor\" id=\"overview\"><\/div>\n<p>Sometimes when writing unit tests, we need to make order agnostic comparison of lists. In this short tutorial, we&#8217;ll take a look at different examples of how we can write such unit tests.<\/p>\n<h2 data-id=\"setup\">2. Setup<\/h2>\n<div class=\"bd-anchor\" id=\"setup\"><\/div>\n<p>As per the <em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/List.html#equals-java.lang.Object-\"  rel=\"noopener noreferrer\">List#equals<\/a><\/em> Java documentation, two lists are equal if they contain the same elements in the same order. Therefore we can&#8217;t merely use the <em>equals<\/em> method as we want to do order agnostic comparison.<\/p>\n<p>Throughout this tutorial, we&#8217;ll use these three lists as example inputs for our tests:<\/p>\n<pre><code class=\"language-java\">List first = Arrays.asList(1, 3, 4, 6, 8);\r\nList second = Arrays.asList(8, 1, 6, 3, 4);\r\nList third = Arrays.asList(1, 3, 3, 6, 6);<\/code><\/pre>\n<p>There are different ways to do order agnostic comparison. Let&#8217;s take a look at them one by one.<\/p>\n<h2 data-id=\"using-junit\">3. Using JUnit<\/h2>\n<div class=\"bd-anchor\" id=\"using-junit\"><\/div>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/junit\">JUnit<\/a> is a well-know framework used for unit testing in the Java ecosystem.<\/p>\n<p>We can use the logic below to compare the equality of two lists using the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/junit-assertions#junit5-condition\"><em>assertTrue<\/em> and <em>assertFalse<\/em><\/a> methods.<\/p>\n<p>Here we check the size of both lists and check if the first list contains all elements of the second list and vice versa. Although this solution works, it&#8217;s not very readable. So now let&#8217;s look at some alternatives:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void whenTestingForOrderAgnosticEquality_ShouldBeTrue() {\r\n    assertTrue(first.size() == second.size() && first.containsAll(second) && second.containsAll(first));\r\n}<\/code><\/pre>\n<p>In this first test, the size of both lists is compared before we check if the elements in both lists are the same. As both of these conditions return <em>true, <\/em>our test will pass.<\/p>\n<p>Let&#8217;s now take a look at a failing test:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void whenTestingForOrderAgnosticEquality_ShouldBeFalse() {\r\n    assertFalse(first.size() == third.size() && first.containsAll(third) && third.containsAll(first));\r\n}<\/code><\/pre>\n<p>Contrastingly, in this version of the test, although the size of both lists is the same, all elements don&#8217;t match.<\/p>\n<h2 data-id=\"using-assertj\">4. Using AssertJ<\/h2>\n<div class=\"bd-anchor\" id=\"using-assertj\"><\/div>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/introduction-to-assertj\"  rel=\"noopener noreferrer\">AssertJ<\/a> is an opensource community-driven library used for writing fluent and rich assertions in Java tests.<\/p>\n<p>To use it in our maven project, let&#8217;s add the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/search.maven.org\/classic\/#search%7Cga%7C1%7Ca%3A%22assertj-core%22\"><em>assertj-core<\/em><\/a> dependency in the <em>pom.xml<\/em> file:<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\r\n    &lt;groupId&gt;org.assertj&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;assertj-core&lt;\/artifactId&gt;\r\n    &lt;version&gt;3.16.1&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>Let&#8217;s write a test to compare the equality of two list instance of the same element and same size:<\/p>\n<pre><code class=\"language-java\">@Test\r\nvoid whenTestingForOrderAgnosticEqualityBothList_ShouldBeEqual() {\r\n    assertThat(first).hasSameElementsAs(second);\r\n}<\/code><\/pre>\n<p>In this example, we verify <em>first<\/em> contains all the elements of the given iterable and nothing else, in any order. The main limitation of this approach is the <em>hasSameElementsAs<\/em> method ignores duplicates.<\/p>\n<p>Let&#8217;s look at this in practice to see what we mean:<\/p>\n<pre><code class=\"language-java\">@Test\r\nvoid whenTestingForOrderAgnosticEqualityBothList_ShouldNotBeEqual() {\r\n    List a = Arrays.asList(\"a\", \"a\", \"b\", \"c\");\r\n    List b = Arrays.asList(\"a\", \"b\", \"c\");\r\n    assertThat(a).hasSameElementsAs(b);\r\n}<\/code><\/pre>\n<p>In this test, although we have the same elements, the size of both lists is not equal, but the assertion will still be true, as it ignores the duplicates. To make it work we need to add a size check for both lists:<\/p>\n<pre><code class=\"language-java\">assertThat(a).hasSize(b.size()).hasSameElementsAs(b);<\/code><\/pre>\n<p>Adding a check for the size of both our lists followed by the method <em>hasSameElementsAs<\/em> will indeed fail as expected.<\/p>\n<h2 data-id=\"using-hamcrest\">5. Using Hamcrest<\/h2>\n<div class=\"bd-anchor\" id=\"using-hamcrest\"><\/div>\n<p>If we are already using Hamcrest or want to use it for writing unit tests, here is how we can use the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/hamcrest-collections-arrays\"><em>Matchers#containsInAnyOrder<\/em><\/a> method for order agnostic comparison.<\/p>\n<p>To use Hamcrest in our maven project, let&#8217;s add the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/search.maven.org\/classic\/#search%7Cga%7C1%7Ca%3A%22hamcrest-all%22\"><em>hamcrest-all<\/em><\/a> dependency in <em>pom.xml<\/em> file:<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\r\n    &lt;groupId&gt;org.hamcrest&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;hamcrest-all&lt;\/artifactId&gt;\r\n    &lt;version&gt;1.3&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>Let&#8217;s look at the test:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void whenTestingForOrderAgnosticEquality_ShouldBeEqual() {\r\n    assertThat(first, Matchers.containsInAnyOrder(second.toArray()));\r\n}<\/code><\/pre>\n<p>Here the method <em>containsInAnyOrder<\/em> creates an order agnostic matcher for <em>Iterables<\/em>, which does matching with examined <em>Iterable <\/em>elements. This test matches the elements of two lists, ignoring the order of elements in the list.<\/p>\n<p>Thankfully this solution doesn&#8217;t suffer from the same problem as explained in the previous section, so we don&#8217;t need to compare the sizes explicitly.<\/p>\n<h2 data-id=\"using-apache-commons\">6. Using Apache Commons<\/h2>\n<div class=\"bd-anchor\" id=\"using-apache-commons\"><\/div>\n<p>Another library or framework apart from JUnit, Hamcrest, or AssertJ, we can use is <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/apache-commons-collection-utils\"><em>Apache<\/em>\u00a0<em>CollectionUtils<\/em><\/a>. It provides utility methods for common operations that cover a wide range of use cases and helps us avoid writing boilerplate code.<\/p>\n<p>To use it in our maven project, let&#8217;s add the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/search.maven.org\/classic\/#search%7Cga%7C1%7Ca%3A%22commons-collections4%22\"><em>commons-collections4<\/em><\/a> dependency in <em>pom.xml<\/em> file:<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\r\n    &lt;groupId&gt;org.apache.commons&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;commons-collections4&lt;\/artifactId&gt;\r\n    &lt;version&gt;4.4&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>Here is a test using <em>CollectionUtils<\/em>:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void whenTestingForOrderAgnosticEquality_ShouldBeTrueIfEqualOtherwiseFalse() {\r\n    assertTrue(CollectionUtils.isEqualCollection(first, second));\r\n    assertFalse(CollectionUtils.isEqualCollection(first, third));\r\n}<\/code><\/pre>\n<p>The <em>isEqualCollection <\/em>method returns <em>true<\/em> if the given collections contain precisely the same elements with the same <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/simple.wikipedia.org\/wiki\/Cardinality#:~:text=In%20mathematics%2C%20the%20cardinality%20of,the%20same%20number%20of%20elements.\">cardinalities<\/a>. Otherwise, it returns <em>false<\/em>.<\/p>\n<h2 data-id=\"conclusion\">7. Conclusion<\/h2>\n<div class=\"bd-anchor\" id=\"conclusion\"><\/div>\n<p>In this article, we have explored how to check the equality of two <em>List\u00a0<\/em>instances, where the elements of both lists are ordered differently.<\/p>\n<p>All these examples can be found <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/testing-modules\/testing-assertions\">over on GitHub<\/a>.<\/p>\n<p>The post <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-assert-lists-equality-ignore-order\/\" >Assert Two Lists for Equality Ignoring Order in Java<\/a> first appeared on <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/\" >Baeldung<\/a>.<\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/634824880\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/634824880\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/634824880\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/634824880\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/634824880\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/634824880\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/634824880\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-assert-lists-equality-ignore-order#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-assert-lists-equality-ignore-order\/feed\/\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Learn how to make order agnostic comparison of lists when writing unit tests.<\/p>\n<p>The post <a rel=\"NOFOLLOW noopener noreferrer\" href=\"https:\/\/feeds.feedblitz.com\/~\/634824880\/0\/baeldung~Assert-Two-Lists-for-Equality-Ignoring-Order-in-Java\/\" target=\"_blank\">Assert Two Lists for Equality Ignoring Order in Java<\/a> first appeared on <a rel=\"NOFOLLOW noopener noreferrer\" href=\"https:\/\/www.baeldung.com\/\" target=\"_blank\">Baeldung<\/a>.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/634824880\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>&nbsp;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/634824880\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\"><\/a>&nbsp;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/634824880\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>&nbsp;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/634824880\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\"><\/a>&nbsp;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/634824880\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>&nbsp;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/634824880\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>&nbsp;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-assert-lists-equality-ignore-order#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&nbsp;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-assert-lists-equality-ignore-order\/feed\/\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&nbsp;<\/div>\n<\/div>","protected":false},"author":214,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Assert Two Lists for Equality Ignoring Order in Java - ITTeacherITFreelance.hk","description":"Learn how to make order agnostic comparison of lists when writing unit tests. The post Assert Two Lists for Equality Ignoring Order in Java first appeared on Ba"},"footnotes":""},"categories":[6],"tags":[4954,4953],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/65126"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/214"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=65126"}],"version-history":[{"count":9,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/65126\/revisions"}],"predecessor-version":[{"id":68705,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/65126\/revisions\/68705"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=65126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=65126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=65126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}