如何在Java中比较两个相同大小的数组列表,并在它们不相同时打印差异。

huangapple go评论82阅读模式
英文:

How to compare two array list of same size and print difference when both are not same in java

问题

我已经使用下面的程序来比较两个数组列表但我不想错过这样一种情况即当两个数组列表的大小相同但内容不同另外下面的代码可以被简化吗

    ArrayList<String> firstList = new ArrayList<String> ( Arrays.asList("Cake", "pizza", "pasta") );
    ArrayList<String> secondList = new ArrayList<String> ( Arrays.asList("Chocolate", "fruits", "pancake"));
    
    if (!firstList.equals(secondList)) {
        if (firstList.size() > secondList.size()) {
            firstList.removeAll(secondList);
            SOP("差异:" + firstList+ "---------不匹配的总组数----------"+ firstList.size());
        } else if (firstList.size() < secondList.size()) {
            secondList.removeAll(firstList);
            SOP("差异:" + secondList+ "---------不匹配的总组数----------" + secondList.size());
        }
    } else {
        SOP("两者相同");
    }
英文:

I have used below program two compare two Array list, But I don't want to miss the condition when both array list are same in size but content different values.Also is below code can be minimize.

ArrayList&lt;String&gt; firstList = new ArrayList&lt;String&gt; ( Arrays.asList(&quot;Cake&quot;, &quot;pizza&quot;, &quot;pasta&quot;) );
ArrayList&lt;String&gt; secondList = new ArrayList&lt;String&gt; ( Arrays.asList(&quot;Chocolate&quot;, &quot;fruits&quot;, &quot;pancake&quot;));

if (!firstList.equals(secondList)) {
    if (firstList.size() &gt; secondList.size()) {
        firstList.removeAll(secondList);
        SOP(&quot;Differance&quot; + firstList+ &quot;---------Total Number of Group mismatch----------&quot;+ firstList.size());
    } else if (firstList.size() &lt; secondList.size()) {
        secondList.removeAll(firstList);
        SOP(&quot;Differance&quot; + secondList+ &quot;---------Total Number of Group mismatch----------&quot; + secondList.size());
    }
} else {
    SOP(&quot;Both are same&quot;);
}

答案1

得分: 1

如果您想使用第三方工具,可以直接使用以下代码:

System.out.println(CollectionUtils.disjunction(firstList, secondList));

为了最小化使用,您可以像下面这样使用单个第三方列表:

public static void main(String[] args) {
    ArrayList<String> firstList = new ArrayList<String>(Arrays.asList("Cake", "pizza", "pasta", "fruits"));
    ArrayList<String> secondList = new ArrayList<String>(Arrays.asList("Chocolate", "fruits", "pancake"));
    List<String> result = new ArrayList<String>();
    add(secondList, result);
    add(firstList, result);
    System.out.println(result);
}

public static void add(ArrayList<String> list, List<String> result) {
    for (String string : list) {
        if (result.contains(string)) {
            result.remove(string);
        } else {
            result.add(string);
        }
    }
}

或者,如果您愿意修改其中一个列表,可以迭代一个列表并检查另一个列表中的数据:

for (String string : firstList) {
    if (secondList.contains(string)) {
        secondList.remove(string);
    } else {
        secondList.add(string);
    }
}
System.out.println(secondList);

希望这些代码对您有帮助。

英文:

If u want to use a Third party utility u can directly use

System.out.println(CollectionUtils.disjunction(firstList, secondList));

To minimize simple use a single third list like below

public static void main(String[] args) {
		ArrayList&lt;String&gt; firstList = new ArrayList&lt;String&gt; ( Arrays.asList(&quot;Cake&quot;, &quot;pizza&quot;, &quot;pasta&quot;, &quot;fruits&quot;) );
		ArrayList&lt;String&gt; secondList = new ArrayList&lt;String&gt; ( Arrays.asList(&quot;Chocolate&quot;, &quot;fruits&quot;, &quot;pancake&quot;));		 
		List&lt;String&gt; result = new ArrayList&lt;String&gt;();
		add(secondList, result);
		add(firstList, result);
		System.out.println(result);
	}

	public static void add(ArrayList&lt;String&gt; list, List&lt;String&gt; result) {
		for (String string : list) {
			if(result.contains(string)) {
				result.remove(string);
			}else {
				result.add(string);
			}
		}
	}

or if u are okay with modifying ur List (any one )
you can iterate one list and check teh data in another list

for (String string : firstList) {
			if(secondList.contains(string)) {
				secondList.remove(string);
			}else {
				secondList.add(string);
			}
		}
		System.out.println(secondList);

huangapple
  • 本文由 发表于 2020年8月18日 02:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63456356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定