Consider the case of a list of strings example : list=['apple','bat','cow,'dog','applebat','cowbat','dogbark','help']
The java code must check if any element of string is a subset of another element and if it is then larger string element must be removed.
so in this case strings 'applebat','cowbat','dogbark, are removed.
The approach I have taken was to take two lists and iterate over them in the following way,
ArrayList<String> list1 = new ArrayList<String>(strings);
ArrayList<String> list2 = new ArrayList<String>(strings);
for(int i = 0; i<list1.size();i++)
{
String curr1 = list1.get(i);
for(int j = 0;j<list2.size();j++)
{
String curr2 = list2.get(j);
if(curr2.contains(curr1)&&!curr2.equals(curr1))
{
list2.remove(j);
j--;
}
}
}
IMPORTANT I have lists with the sizes of 200K to 400K elements.I would like to find a way to improve performance. I even tried hashsets but they were not much help.I am facing issues with the time taken by the program.
Can any one suggest any improvements to my code or any other approaches in java to improve performance??
HashSetand check withcontainsand to remove useremovemethods..Setwill give usO(1)(hypothetical) runtime.