Question

I have 2 Strings:

A1=[Rettangolo, Quadrilatero, Rombo, Quadrato]
A2=[Rettangolo, Rettangolo, Rombo, Quadrato]

I want to obtain this: "I have found "Quadrilatero", instead of "Rettangolo" ". If I use removeAll() or retainAll() it doesn't work because I have 2 instances of "Rettangolo". In fact, if I use a1.containsAll(a2), I get true and I want false.

Thanks all for considering my request.

Was it helpful?

Solution

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]

OTHER TIPS

These two classes might help. Let me know how I can improve this further. Feel free to use the code below in your own work. I must point out that the current code does not take care of repeated list elements.

import java.util.List;

public class ListDiff<T> {

    private List<T> removed;
    private List<T> added;

    public ListDiff(List<T> removed, List<T> added) {
        super();
        this.removed = removed;
        this.added = added;
    }

    public ListDiff() {
        super();
    }

    public List<T> getRemoved() {
        return removed;
    }

    public List<T> getAdded() {
        return added;
    }

}

Util class.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ListUtil {

    public static <T> ListDiff<T> diff(List<T> one, List<T> two) {

        List<T> removed = new ArrayList<T>();
        List<T> added = new ArrayList<T>();

        for (int i = 0; i < one.size(); i++) {
            T elementOne = one.get(i);
            if (!two.contains(elementOne)) {
                //element in one is removed from two
                removed.add(elementOne);
            }
        }

        for (int i = 0; i < two.size(); i++) {
            T elementTwo = two.get(i);
            if (!one.contains(elementTwo)) {
                //element in two is added.
                added.add(elementTwo);
            }
        }

        return new ListDiff<T>(removed, added);
    }

    public static <T> ListDiff<T> diff(List<T> one, List<T> two, Comparator<T> comparator) {
        List<T> removed = new ArrayList<T>();
        List<T> added = new ArrayList<T>();

        for (int i = 0; i < one.size(); i++) {
            T elementOne = one.get(i);
            boolean found = false;

            //loop checks if element in one is found in two.
            for (int j = 0; j < two.size(); j++) {
                T elementTwo = two.get(j);
                if (comparator.compare(elementOne, elementTwo) == 0) {
                    found = true;
                    break;
                }
            }
            if (found == false) {
                //element is not found in list two. it is removed.
                removed.add(elementOne);
            }
        }

        for (int i = 0; i < two.size(); i++) {
            T elementTwo = two.get(i);
            boolean found = false;

            //loop checks if element in two is found in one.
            for (int j = 0; j < one.size(); j++) {
                T elementOne = one.get(j);
                if (comparator.compare(elementTwo, elementOne) == 0) {
                    found = true;
                    break;
                }
            }
            if (found == false) {
                //it means element has been added to list two. 
                added.add(elementTwo);
            }

        }

        return new ListDiff<T>(removed, added);
    }

    public static void main(String args[]) {
        String[] arr1 = { "london", "newyork", "delhi", "singapore", "tokyo", "amsterdam" };
        String[] arr2 = { "london", "newyork", "delhi", "singapore", "seoul", "bangalore", "oslo" };

        ListDiff<String> ld = ListUtil.diff(Arrays.asList(arr1), Arrays.asList(arr2));
        System.out.println(ld.getRemoved());
        System.out.println(ld.getAdded());

        ld = ListUtil.diff(Arrays.asList(arr1), Arrays.asList(arr2), new Comparator<String>() {
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        }); //sample for using custom comparator
        System.out.println(ld.getRemoved());
        System.out.println(ld.getAdded());
    }
}

Here are three solutions.

An implementation that uses a remove method.

public static boolean same(List<String> list1, List<String> list2){
    if (list1.size() != list2.size())
        return false;
    List<String> temp = new ArrayList<String>(list1);
    temp.removeAll(list2);
    return temp.size() == 0;
}

A solution that sorts then compares.

public static boolean same(List<String> list1, List<String> list2){
    if (list1.size() != list2.size())
        return false;
    Collections.sort(list1);
    Collections.sort(list2);
    for (int i=0;i<list1.size();i++){
        if (!list1.get(i).equals(list2.get(i)))
            return false;
    }
    return true;
}

And, just for fun, you could do this by doing a word count difference between the two arrays. It wouldn't be the most efficient, but it works and possibly could be useful.

public static boolean same(List<String> list1, List<String> list2){
    Map<String,Integer> counts = new HashMap<String,Integer>();
    for (String str : list1){
        Integer i = counts.get(str);
        if (i==null)
            counts.put(str, 1);
        else
            counts.put(str, i+1);
    }
    for (String str : list2){
        Integer i = counts.get(str);
        if (i==null)
            return false; /// found an element that's not in the other
        else
            counts.put(str, i-1);
    }
    for (Entry<String,Integer> entry : counts.entrySet()){
        if (entry.getValue() != 0)
            return false;
    }
    return true;
}

This will find the intersection between two arrays for this specific case you have explained.

String[] A1 = { "Rettangolo", "Quadrilatero", "Rombo", "Quadrato" };
String[] A2 = { "Rettangolo", "Rettangolo", "Rombo", "Quadrato" };
ArrayList<String> a1 = new ArrayList<String>(Arrays.asList(A1));
ArrayList<String> a2 = new ArrayList<String>(Arrays.asList(A2));
a1.removeAll(a2);
System.out.println("I have found " + a1);

I hope this will help you

    String[] A1 =  {"Rettangolo", "Quadrilatero", "Rombo", "Quadrato"};
    String[] A2 ={"Rettangolo", "Rettangolo", "Rombo", "Quadrato"};

    Set<String> set1 = new HashSet<String>();
    Set<String> set2 = new HashSet<String>();

    set1.addAll(Arrays.asList(A1));
    set2.addAll(Arrays.asList(A2));

    set1.removeAll(set2);
    System.out.println(set1);// ==> [Quadrilatero]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top