2

Consider the following piece of code in Java:

final LinkedHashSet<String> alldata = new LinkedHashSet<String>();
for (final String folder: folders) {
    for (final String d: data.get(folder)) {
        allData.add(d);
    }
}

when folders is List<String> and data is Map<String, List<String>>.

Example of data:

{data1=[prepare, echo2, echo1], data2=[prepare, sleep2, check]}

Until now, I didn't care about the order of the allData and it would look:

[prepare, echo1, echo2, sleep2, check]

I have a list dataList which contains the right order of the data (and of course all data):

[prepare, sleep1, echo1, sleep2, echo2, check]

I want to iterate through the data and add into the allData those that are defined in data in the right order.

The output for the previous example should be:

[prepare, echo1, sleep2, echo2, check]

Another example:

data = {data1=[prepare], data2=[prepare, sleep2,sleep1,echo2]}
dataList = [prepare, sleep1, echo1, sleep2, echo2, check]

expected output:

allData = [prepare,sleep1,sleep2,echo2]

Of course, there could be more than two inner data (data1,data2,...,dataN). I'm not sure that its the right thing to add code into the inner for loop, because by doing I'll have to add another loop to iterate through the dataList, and it does not feel efficient (3 loops).

What good, clean and efficient way should I use?

EDIT: I don't try to sort the collection. But I thought of doing the following algorithm:

Creating a Set of all data and then iterate through the dataList and insert into the LinkedHashSet.

Is it efficient?

EDIT2: What I tried to do:

final Set<String> activeData = new HashSet<String>();
for (final String folder: folders) {
    for (final String d: fubsAndSteps.get(folder)) {
        activeData.add(d);
    }
}

final LinkedHashSet<String> allData = new LinkedHashSet<String>();
for (final String main_data : dataList) {
    for (final String active_data: activeData) {
        if (main_data.equals(active_data)) {
            allData.add(active_data);
        }
    }
}

That works for me, but it does not look really good, I mean 4 for loops. It does not feel clean and efficient. Is there a better algorithm?

TTaJTa4
  • 810
  • 1
  • 8
  • 22

1 Answers1

2

One of the solution is Iterate over datalist and check the element exists in data, then add it to allData which keeps order

    Set<String> tempSet = new HashSet<>();
    folders.forEach(folder -> tempSet.addAll(data.get(folder))); // flattening all the data elements into single set

    //Iterate over dataList where you are already maintaining order. If element exists, add it to allData
    dataList.forEach(item -> {
        if(tempSet.contains(item)){
            allData.add(item);
        }
    });

If you are using Java 7 or below

    Set<String> tempSet = new HashSet<>();
    // flattening all the data elements into single set
    for(String folder:folders) {
        tempSet.addAll(data.get(folder));
    }

    //Iterate over dataList where you are already maintaining order. If element exists, add it to allData
    for(String item:dataList){
        if(tempSet.contains(item)){
            allData.add(item);
        }
    }
Naghaveer R
  • 2,890
  • 4
  • 30
  • 52