如何知道java中兩個數組列表之間的不同元素?我需要的確切元素不是一個布爾值,可以使用removeAll()
來檢索。查找Java中兩個ArrayList之間的不同元素
回答
LinkedHashMap table;
for each element e of array A
if table.get(e) != null
table.put(e, table.get(e) + 1)
else
table.put(e, 0)
//Do the same for array B
for each element e of array B
if table.get(e) != null
table.put(e, table.get(e) + 1)
else
table.put(e, 0)
在表中值爲0的for循環元素的末尾是不同的元素。
oooo比我的好多了 – hvgotcodes 2011-05-17 00:34:17
@hvgotcodes謝謝你。 – Enrique 2011-05-17 00:35:54
不,謝謝,我學到了一些東西... – hvgotcodes 2011-05-17 00:37:01
如果我明白你的問題正確,則代碼如下方法nonOverLap
下面應該讓你的是:
<T> Collection<T> union(Collection<T> coll1, Collection<T> coll2) {
Set<T> union = new HashSet<>(coll1);
union.addAll(new HashSet<>(coll2));
return union;
}
<T> Collection<T> intersect(Collection<T> coll1, Collection<T> coll2) {
Set<T> intersection = new HashSet<>(coll1);
intersection.retainAll(new HashSet<>(coll2));
return intersection;
}
<T> Collection<T> nonOverLap(Collection<T> coll1, Collection<T> coll2) {
Collection<T> result = union(coll1, coll2);
result.removeAll(intersect(coll1, coll2));
return result;
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class CompareTwoList {
public CompareTwoList() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
List<String> ls1 = new ArrayList<String>();
ls1.add("a");
ls1.add("b");
ls1.add("c");
ls1.add("d");
List<String> ls2 = new ArrayList<String>();
ls2.add("a");
ls2.add("b");
ls2.add("c");
ls2.add("d");
ls2.add("e");
Set<String> set1 = new HashSet<String>();
set1.addAll(ls1);
Set<String> set2 = new HashSet<String>();
set2.addAll(ls2);
set2.removeAll(set1);
//set.addAll(ls1);
//set.addAll(ls1);
for (String diffElement : set2) {
System.out.println(diffElement.toString());
}
}
}
完善和簡化,適用於所有情況。謝謝 – 2015-03-26 09:14:31
使用Apache Commons Collections(javadoc):
CollectionUtils.disjunction(a, b);
參見:Effective Java,2nd edition,Item 47:知道並使用庫(作者僅提及JDK的內置庫,但我認爲其他庫的推理也可能如此)。
調用傳遞兩個數組列表的方法ReturnArrayListDiffElements。將返回一個數組列表,它是兩個通過的數組列表之間的區別將被返回
public ArrayList ReturnArrayListDiffElements(ArrayList arrList1, ArrayList arrList2){
ArrayList<String> List1 = new ArrayList<String>();
ArrayList<String> List2 = new ArrayList<String>();
ArrayList<String> List3 = new ArrayList<String>();
ArrayList<String> List4 = new ArrayList<String>();
List1.addAll(arrList1);
List2.addAll(arrList2);
List3 = ReturnArrayListCommonElements(List1,List2);
List1.removeAll(List3);
List2.removeAll(List3);
if(List1.size() > 0){
List4.add("Distinct elements in Array List 1");
List4.addAll(List1);
}
if(List2.size() > 0){
List4.add("Distinct elements in Array List 2");
List4.addAll(List2);
}
return List4;
}
public ArrayList ReturnArrayListCommonElements(ArrayList arrList1, ArrayList arrList2){
ArrayList<String> List1 = new ArrayList<String>();
ArrayList<String> List2 = new ArrayList<String>();
ArrayList<String> List1A = new ArrayList<String>();
ArrayList<String> List2A = new ArrayList<String>();
ArrayList<String> List1B = new ArrayList<String>();
ArrayList<String> List3 = new ArrayList<String>();
List1.addAll(arrList1);
List2.addAll(arrList2);
List1A.addAll(arrList1);
List2A.addAll(arrList2);
List1B.addAll(arrList1);
int intList1Size, intList2Size;
List1.removeAll(List2);
intList1Size = List1.size();
List2.removeAll(List1A);
intList2Size = List2.size();
if (intList1Size == 0 && intList2Size ==0) {
List3.addAll(List1B);
return List3;
} else {
List3.addAll(List1B);
List1B.removeAll(List2A);
List3.removeAll(List1B);
return List3;
}
}
這取決於你想要檢查什麼。
如果你想獲得兩個列表中的所有獨特的元素(即是第一列表獨特的全要素即和,所有元素的獨特第二列表)也被稱爲symmetric difference可以使用上面脫節方法如前所述從Apache Commons Collections 4.0:
CollectionUtils.disjunction(a, b);
如果你想只從一個列表得到所有獨特的元素(只存在一個列表,即元素,但在其他不存在)阿爾斯Ø稱爲relative complement你可以從這個名單中減去方法從Apache Commons Collections 4.0使用減去另一個:
CollectionUtils.subtract(a, b); //gives all unique elements of a that don't exist in b
- 1. 刪除兩個不同Arraylist之間的公共元素
- 2. 在兩個雙引號之間的ArrayList中檢查元素
- 3. 查找兩個序列之間的相同元素
- 4. 如何找到jQuery中兩個元素之間的元素?
- 5. 查找兩個元素之間的數組中的所有元素
- 6. 查找Java中N個列表之間的通用元素
- 7. Java很多arraylist - 尋找共同元素
- 8. 如何在java中找到兩個arraylist之間的差異
- 9. 找出兩個陣列之間的共同/罕見元素PHP
- 10. 查找位於兩個值之間的數組的元素
- 11. 查找Linq中兩個集合中不同的子元素
- 12. 用jquery查找兩個元素之間的節點數量?
- 13. 查找兩個高維numpy陣列之間的相互元素
- 14. 查找兩個數組之間的重複元素
- 15. 在ArrayList的最後一個元素中查找對象Java
- 16. 查找兩個不同陣列的相同元素
- 17. 查找兩個不同列中匹配元素的實例(MySQL)
- 18. Xpath查找兩個不同元素的第一個匹配項
- 19. 查找不同行中兩次之間的時間差
- 20. 兩組元素之間的SQLite查詢
- 21. 找到兩個標籤之間的元素列表中的
- 22. 在Java中的ArrayList中查找最常見/頻繁的元素
- 23. 如何在Java中的ArrayList中查找元素的位置?
- 24. 檢查兩個arraylist是否包含相同的元素
- 25. 試圖比較兩個不同ArrayList的每個元素
- 26. 如何找到兩個時間向量的不同元素?
- 27. 比較兩個元組列表中元素之間的元素
- 28. 查找分鐘,不能在一個ArrayList中的元素比較
- 29. 如何在不改變Java中的第一個arraylist值的情況下找到兩個arraylist之間的區別?
- 30. 如何在Java中共享ArrayList和TreeSet之間的元素?
請注意,'的removeAll()'** **變化的列表,你」重新調用該方法。所以,如果它返回「true」,那麼這意味着列表已被更改。所以這個方法並不是完全沒有用的或者什麼的。 – BalusC 2011-05-17 00:54:20