Search code examples
javaperformanceout-of-memorytime-complexityspace-complexity

Arraylist Intersection performance (in time and space ) in Java


I need to make million of intersections of ArrayList in java, and for this purpose I use this method:

public static ArrayList<Integer> intersection(ArrayList<Integer> a, ArrayList<Integer> b) {
        Set<Integer> aSet = new HashSet<Integer>(a);
        Set<Integer> bSet = new HashSet<Integer>(b);

        for(Iterator<Integer> it = aSet.iterator(); it.hasNext();) {
            if(!bSet.contains(it.next())) it.remove();
        }
        return new ArrayList<Integer>(aSet);
    }

In terms of time It's performant But I have a lot of memory leaks and I often go out of memory. How can I improve the function in order to be performant both in time and in space?

UPDATE

The arraylists given in input must remain unchanged.


Solution

  • One solution (for performance) would be to use a SortedSet like so

    public static List<Integer> intersection2(List<Integer> a, List<Integer> b) {
        SortedSet<Integer> aSet = new TreeSet<Integer>(a);
        aSet.retainAll(b);
        return new ArrayList<Integer>(aSet);
    }
    

    Another solution (for space) would be use the passed in List(s) like so (EDITED with the "new requirement" that the passed in List(s) be unchanged),

    public static List<Integer> intersection3(List<Integer> a, List<Integer> b) {
        List<Integer> c = new ArrayList<Integer>(a); // <-- new requirement.
        c.retainAll(b);
        return c;
    }