For a list of treeDataGroups
with 5 TreeDataGroup
objects, each treeDataGroup
object contains a maximum 100 dataAndVar
and 100 dataNotVar
. The execution of removeFullyUnexpectedData
takes 11466 ms. I would like to reduce that and I know the only way to reduce that the adopted algorithm and the way that I write the code. The treeDataGroups
parameter contains sorted elements. Can someone suggest how I can improve this algorithm? My goal is to reduce this execution time even just for few seconds only.
What I am trying to do is:
I have list of VG object (it is a TreeDataGroup
object), let us say:
I have 5 elements VG in my lTree
list: VG0
, VG1
, VG2
, VG3
, VG4
objects. Each object contains an andVar
String list and a notVar
String list. The firstListIsCompletelyContainedInMainList
method allows to check if list2
(the second parameter of firstListIsCompletelyContainedInMainList function
) contains list1
, which is the first parameter of the firstListIsCompletelyContainedInMainList
function.
Know that list1
(the first parameter of the firstListIsCompletelyContainedInMainList
function) can be null or empty and list2
(the second parameter of the firstListIsCompletelyContainedInMainList
function) as well can be null or empty. So if list1
is null or empty, method 1 returns true, else if list2
is null or empty, we return false. Otherwise, I need to check if list2
contains all elements of list1
. I need to compare each element of my list, with all other elements of my list except to the current element itself.
And if the current element if different from the other element:
If I start from the end of my list:
Let us say, for VG4
and VG3
:
- For x equal to 4, I need to compare
VG4
andVG3
--> check ifVG4.andVar
containsVG3.andVar
, then check ifVG3.notVar
containsVG4.notVar
. If both conditions are true, then I removeVG4
from thetreeDataGroups
list. - If the current element is
VG4
, I need to check if theVG4.andVar
string list contains allVG3.andVar
string list (for that I need to check if both lists are not null or empty as I explained above)). If yes (true), I will need to check if theVG3.notVar
string list contains allVG4.notVar
string lists. For that, I need to check if both lists are not null or empty as I explained above)). If yes (true), I need to removeVG4
from mytreeDataGroups
list. - Then x equal to 3, I need to compare
VG3
andVG4
--> check ifVG3.andVar
containsVG4.andVar
then check ifVG4.notVar
containsVG3.notVar
. If both conditions are true, then I removeVG3
fromtreeDataGroupslist
.
Then x equal to 2 I need to compare (VG2 and VG3).....
x equal to 2 I need to compare (VG2 and VG4)
Then x equal to 1 I need to compare (VG1 and VG2)
x equal to 1 I need to compare (VG1 and VG3)
x equal to 1 I need to compare (VG1 and VG4)
Then x equal to 0 I need to compare (VG0 and VG1)
x equal to 0 I need to compare (VG0 and VG2)
x equal to 0 I need to compare (VG0 and VG3)
x equal to 0 I need to compare (VG0 and VG4)
public List<TreeDataGroup> removeFullyUnexpectedData(List<TreeDataGroup> treeDataGroups) {
for (int x = treeDataGroups.size()-1; x >= 0; x--) {
TreeDataGroup treeDg1 = treeDataGroups.get(x);
for (int y = treeDataGroups.size()-1; y >= 0; y--) {
if (y != x) {
TreeDataGroup treeDg2 = treeDataGroups.get(y);
//treeDg1.getDataAndVar and treeDg1.getDataNotVar are a list of String
if (firstListIsCompletelyContainedInMainList(treeDg2.getDataAndVar(), treeDg1.getDataAndVar) {
if (firstListIsCompletelyContainedInMainList(treeDg1.getDataNotVar(), treeDg2.getDataNotVar) {
treeDataGroups.remove(x);
break;
}
}
}
}
}
return treeDataGroups;
}
// Are the items in the search list completely contained in the main list...
public static boolean firstListIsCompletelyContainedInMainList(List<String> list1, List<String> list2) {
if (isStringListNullOrEmpty(list1)) {
return true;
}
if (isStringListNullOrEmpty(list2)) {
return false;
}
for (String item : list1) {
if (!list2.contains(item)) {
return false;
}
}
return true;
}
public static boolean isStringListNullOrEmpty(List<String> stringList) {
if ((stringList == null || stringList.isEmpty())) {
return true;
} else {
return false;
}
}