英文:
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<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("Differance" + firstList+ "---------Total Number of Group mismatch----------"+ firstList.size());
    } else if (firstList.size() < secondList.size()) {
        secondList.removeAll(firstList);
        SOP("Differance" + secondList+ "---------Total Number of Group mismatch----------" + secondList.size());
    }
} else {
    SOP("Both are same");
}
答案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<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);
			}
		}
	}
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论