How to Calculate the Difference Between Two Arraylists

How can I calculate the difference between two ArrayLists?

In Java, you can use the Collection interface's removeAll method.

// Create a couple ArrayList objects and populate them
// with some delicious fruits.
Collection firstList = new ArrayList() {{
add("apple");
add("orange");
}};

Collection secondList = new ArrayList() {{
add("apple");
add("orange");
add("banana");
add("strawberry");
}};

// Show the "before" lists
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);

// Remove all elements in firstList from secondList
secondList.removeAll(firstList);

// Show the "after" list
System.out.println("Result: " + secondList);

The above code will produce the following output:

First List: [apple, orange]
Second List: [apple, orange, banana, strawberry]
Result: [banana, strawberry]

Handle differences between two arraylists

You should probably use java's set interfaces for this.

Now, one thing that's going to be important is having a good equals method on MyObject to be able to compare whether two MyObjects are the same.

Then you could use that documentation link above to check the intersection of two sets. If the items that are in both sets are the same number of items as in one set, then they're the same set (irrespective of order).

HashSet<MyObject> set1 = new HashSet<MyObject>(array1);
HashSet<MyObject> set2 = new HashSet<MyObject>(array2);

Set<MyObject> intersection = new HashSet<MyObject>(set1);
intersection.retainAll(set2);

if(intersection.size() == set2.size()) {
// They're the same.
} else {
HashSet<MyObject> itemsOnlyInSet1 = intersection;
HashSet<MyObject> itemsOnlyInSet2 = set2.retainAll(set1);
}

How to find the differences between two Array Lists based on a property?

Override equals() and hashcode() methods of your Employee class to only use employeeId when checking for equality (im not sure about why you need the id field. you might what to incorporate it as well). NetBeans / Eclipse IDEs can do this for you. Then you can create a copy of your original lists and use List.removeAll() to calculate difference.

How to find the differences from 2 ArrayLists of Strings?

Use the remove method from ArrayList. It only removes the first occurance.

public static void main(String []args){
//Create ArrayLists
String[] A1 = {"Rettangolo", "Quadrilatero", "Rombo", "Quadrato"};
ArrayList<String> a1=new ArrayList(Arrays.asList(A1));
String[] A2 ={"Rettangolo", "Rettangolo", "Rombo", "Quadrato"};
ArrayList<String> a2=new ArrayList(Arrays.asList(A2));
// Check ArrayLists
System.out.println("a1 = " + a1);
System.out.println("a2 = " + a2);
// Find difference
for( String s : a1)
a2.remove(s);
// Check difference
System.out.println("a1 = " + a1);
System.out.println("a2 = " + a2);
}

Result

a1 = [Rettangolo, Quadrilatero, Rombo, Quadrato]
a2 = [Rettangolo, Rettangolo, Rombo, Quadrato]
a1 = [Rettangolo, Quadrilatero, Rombo, Quadrato]
a2 = [Rettangolo]

How to find the difference between two arraylist without changing the first arraylist value in Java?

fw_diff= fw_week.clone().removeAll(sw_week)
sw_diff=sw_week.clone().removeAll(fw_week)

More efficient one:

fw_diff= fw_week.clone().removeAll(sw_week)
sw_diff=sw_week.clone().removeAll(fw_diff)

Here,fw_diff contains intersaction of both the list. So now , for sw_diff , we need to remove only fw_diff from sw_week . No need to remove all of fw_week .

How to calculate percentage of matching values on two arraylists?

Try this:

List<String> actual = new ArrayList<>();
List<String> required = new ArrayList<>();

List<String> common = new ArrayList<>(actual);
common.retainAll(required);

System.out.println(" 100.0 * common / actual = " + (actual.size() == 0 ? 0 : 100.0 * common.size() / actual.size()));
System.out.println(" 100.0 * common / required = " + (required.size() == 0 ? 0 : 100.0 * common.size() / required.size()));

How can I return the difference between two lists?

You can convert them to Set collections, and perform a set difference operation on them.

Like this:

Set<Date> ad = new HashSet<Date>(a);
Set<Date> bd = new HashSet<Date>(b);
ad.removeAll(bd);

Comparing 2 ArrayLists and returning the common elements

Obviously, matchCriteria.add(test) isn't going to work for List<Loading> matchCriteria and String test.

Instead, you'd need to first create an instance of Loading using whatever test is. That would mean you'd need a constructor like Loading(String data) which is able to build a reasonable Loading instance.

In order to use matchCriteria.contains(Loading) the Loading class needs to have a reasonable implementation of equals(Object) and by contract hashCode() as well.

Finally, I assume you want to only add a Loading instance if it isn't already present in matchCriteria, i.e. you don't want duplicates. That would mean you'd be better off with a set, e.g. like this:

//LinkedHashSet keeps insert order
Set<Loading> matchCriteria = new LinkedHashSet<>(list1);

List<String>testQueryValue = list2.get(0).getTestQueryValues();
for(Loading match: list1) {
for(Sring test: testQueryValue){
matchCriteria.add(new Loading(test));
}
}

return new ArrayList<>(matchCriteria);

If you want to keep using a list, your inner loop would probably need to look like this:

for(Sring test: testQueryValue){
Loading testLoading = new Loading(test);
if( !matchCriteria.contains(testLoading) ) {
matchCriteria.add();
}
}

Or, if you only want to create Loading instances if they are needed, use something like this:

Map<String, Loading> matches = new LinkedHashMap<>();
list1.forEach(loading -> matches.put(loading.getStringRepresentation, loading) );

for(Sring test: testQueryValue){
matches.computeIfAbsent(test, key -> new Loading(key));
}

return new ArrayList<>(matches.values());

How to compare two different size arraylists and check for order of elements

One way to do this is to first create a copy of list 2 called list2Copy, then remove all the elements in list2Copy that does not exist in list1. Now you just need to compare if they are exactly equal to each other.

List<Integer> list1 = Arrays.asList(1,3,5);
List<Integer> list2 = Arrays.asList(1,2,3,4,5);
ArrayList<Integer> list2Copy = new ArrayList<>(list2);
list2Copy.removeIf(x -> !list1.contains(x));
return list1.equals(list2Copy);

Here's another approach with a smaller time complexity:

if (list1.size() > list2.size()) {
// definitely not same order
return false;
}

int list1Index = 0;
for (int i = 0 ; i < list2.size() ; i++) {
if (Objects.equals(list2.get(i), list1.get(list1Index))) {
list1Index++;
if (list1Index == list1.size()) {
return true;
}
}
}
// at the end, list1Index should be the same as list1.size() if list2 is in the same order.
return false;


Related Topics



Leave a reply



Submit