2020-06-02

How to get uncommon element from two list Without removing element using java?

To get the list of uncommon element while comparing two lists .
ex:-

List<String> readAllName = {"aaa","bbb","ddd"};
List<String> selectedName = {"bbb","ccc"};

Uncommon elements from readAllName list ("aaa","ccc","ddd") in another list. Without Using remove()and removeAll().


Ans:

Simply run two loops:

List<String> uncommon = new ArrayList<> ();
for (String s : name) {
    if (!name2.contains(s))
uncommon.add(s);
}
for (String s : name2) {
    if (!name.contains(s))
uncommon.add(s);
}

System.out.println(uncommon )

No comments:

Post a Comment