java - Find intersection between two ArrayLists -
java - Find intersection between two ArrayLists -
find intersection of 2 arraylists of strings.
here code:
public arraylist<string> intersection( arraylist<string> al1, arraylist<string> al2){ arraylist<string> empty = new arraylist<string>(); arraylist<string> empty1 = new arraylist<string>(); if (al1.isempty()){ homecoming al1; } else{ string s = al1.get(0); if(al2.contains(s)) empty.add(s); empty1.addall(al1.sublist(1, al1.size())); empty.addall(intersection(empty1, al2)); homecoming empty; } }
i want output this: example,
[a, b, c] intersect [b, c, d, e] = [b, c]
the above code give me output, want know how create code more easier understand.
you create easier understand writing this:
/** * computes intersection of 2 lists of strings, returning new arraylist of strings * * @param list1 1 of lists compute intersection * @param list2 1 of lists compute intersection * * @return new arraylist of strings containing intersection of list1 , list2 */ public arraylist<string> intersection( list<string> list1, list<string> list2) { arraylist<string> result = new arraylist<string>(list1); result.retainall(list2); homecoming result; }
java recursion arraylist
Comments
Post a Comment