I have two Arraylist
and I want to check if one is a subset of the other (ordering is not important in the comparison).
The problem is: Lets say Ar1={e,e,r}
and Ar2={e,r,b,d}
. In my code it says Ar1
is a subset. But I want it to say false, cause Ar2
has only one e. How to do that?
public static void dostuff(String word1,String word2){
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
for (String character : word1.split("")) {
list1.add(character);
}
for (String character : word2.split("")) {
list2.add(character);
}
boolean sub = list1.containsAll(list2) || list2.containsAll(list1);
System.out.println(sub);
}