<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>assert list &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/assert-list/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Sat, 10 Apr 2021 09:56:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>How to check if two lists are equal in Java</title>
		<link>https://codepills.com/how-to-check-if-two-lists-are-equal-in-java/</link>
					<comments>https://codepills.com/how-to-check-if-two-lists-are-equal-in-java/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Thu, 01 Apr 2021 09:55:26 +0000</pubDate>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[assert list]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Collections]]></category>
		<category><![CDATA[Java List]]></category>
		<category><![CDATA[testing]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1202</guid>

					<description><![CDATA[A short article focused on a widespread problem in testing if two list instances are identical, or rather say if they contain the same elements in the same order. <a href="https://codepills.com/how-to-check-if-two-lists-are-equal-in-java/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>This short article will explain to you what List equality means and how to compare two <strong>Java lists</strong> against each other.</p>
<p><span id="more-1202"></span></p>
<h2>Introduction</h2>
<p>Checking list equality is a widespread problem; we often need to compare the list elements against each other. Comparison and assertion occur many times during the testing when we need to assert results with predefined expectations.</p>
<h2>Two lists equality</h2>
<p>At first, some might think, that for equality of two lists is conditioned by lists containing the same elements. This way of thinking is not entirely true. Single look into the Java documentations for <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html#equals-java.lang.Object-" title="List equals" target="_blank" rel="nofollow noopener">List.equals()</a> method will uncover us true meaning of list equality:</p>
<blockquote><p>
    &#8230; two lists are defined to be equal if they contain the same elements in the same order.
</p></blockquote>
<p>Therefore, we need to compare the lists&#8217; elements mutually, but we also need to check the order of the elements in the lists reciprocally.</p>
<p>So first, let&#8217;s take a good look at how not to do a list equality check.</p>
<h2>Compare two list with the bruteforce</h2>
<p>Following lines of code are placed here only for demonstration purposes. For all cost, you must avoid such hideous brute force solution in your code.</p>
<pre><code class="language-java">private boolean compareLists(final List list1,final List list2) {
    if(list1.size() == list2.size()) {
        if(list1.isEmpty() && list2.isEmpty()) {
            return true;
        }
        for(int i = 0; i < list1.size(); i++) {
            if(!list1.get(i).equals(list2.get(i))) {
                return false;
            };
        }
        return true;
    }
    return false;
}</code></pre>
<p>The reason why to not use it in your code is that this code is basically <code>List</code> interface implementation of the <code>equal()</code> method. Therefore all you need to use for comparing two lists is to call <code>equal()</code> method.</p>
<pre><code class="language-java">private boolean compareLists(final List list1,final List list2) {
    return list1.equals(list2);
}</code></pre>
<p><b>Interface method definition ensures</b> that the equals method works through different implementations of the <strong>List</strong> interface in a same way.</p>
<h2>Framework usage</h2>
<p>For testing purposes, it is probably better to use some of the available <strong>Java frameworks for testing</strong>, which can give us more options in List testing.</p>
<p>In our examples, we will play with the following code snippet based on user entities belonging to different groups. We will compare lists are figure out if all users are members of all groups.</p>
<pre><code class="language-java">List<String> users = Arrays.asList("John Doe", "Tom Smith", "Paul Iron", "George Silver");
List<String> moderators = Arrays.asList("John Doe", "Tom Smith", "Paul Iron", "George Silver");
List<String> admins = Arrays.asList("John Doe", "Tom Smith", "Paul Iron", "George Silver");</code></pre>
<h3>JUnit</h3>
<p>Rather than using and printing equals for tests, it is better to use assert methods. <strong>JUnit</strong> testing framework offers us three suitable ways for list equality assertion.</p>
<ul>
<li><code>assertEquals(expected, actual)</code> - Asserts that two objects are equal. If they are not, an <code>AssertionError</code> without a message is thrown. If <i>expected</i> and <i>actual</i> are <code>null</code>, they are considered equal.</li>
<li><code>assertNotSame(unexpected, actual)</code> - Asserts that two objects do not refer to the same object. If they do refer to the same object, an <code>AssertionError</code> without a message is thrown.</li>
<li><code>assertNotEquals(unexpected, actual)</code> - Asserts that two objects are not equals. If they are, an <code>AssertionError</code> without a message is thrown. If <i>unexpected</i> and <i>actual</i> are <code>null</code>, they are considered equal.</li>
</ul>
<pre><code class="language-java">@Test
public void testJUnitListAsserts() throws Exception {
    Assert.assertEquals(users, moderators);
    Assert.assertNotSame(users, moderators);
    Assert.assertNotEquals(users, admins);
}</code></pre>
<h2>Conclusion</h2>
<p>This article has shown you how not to do list assertion and explain how to check and assert two lists equality.</p>
<p>As always, you can find all our examples on our <a href="https://github.com/codekopf/tutorials-jvm/tree/master/testing-modules/junit-basics/" title="Tutorial JVM - Testing Modules - JUnit Basics" target="_blank" rel="nofollow noopener">GitHub project</a>!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/how-to-check-if-two-lists-are-equal-in-java/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
