## Review Algorithm

I want to display data in serpentine order .

Serpentine Order:
In an experiment there are Replication,range,plot . Replications
contain range,plot.A range contains plot. If there is 2 replication, 4
ranges and 8 plots , then each replication contains 2 ranges.Each range
has 2 plot so ,data to displayed in serpentine order will be like this:
Code:
```Replication     Range Plot
1                  1    1
1                  1    2
1                  2    2
1                  2    1
2                  3    1
2                  3    2
2                  4    2
2                  4    1```
lets i have this information or in db in scattered format hence i will
store this inforamtion in a following data structure.
TreeMap<Integer, TreeMap<Integer, ArrayList<Integer>>> serpentine.
ArrayList will have list of plot for range which is represented in this
data structure TreeMap<Integer, ArrayList<Integer>>.A treemap is key
value pair data structure here key in range value is list of plots.
Now each Replication will have range which have plot so final data
structure will be this TreeMap<Integer, TreeMap<Integer, ArrayList<Integer>>>.

Now the acutall data i have to display is this.

Code:
``` Replication     Range Plot   Hybrid pedigree qty
1                  1    1    HH1    HH1/HH1   1
1                  1    2    HH2    HH2/HH2   1
1                  2    2    HH3    HH3/HH3   3
1                  2    1    HH4    HH4/HH4   4
2                  3    1    HH1    HH1/HH1   1
2                  3    2    HH2    HH2/HH2   1
2                  4    2    HH3    HH3/HH3   3
2                  4    1    HH4    HH4/HH4   4```
Here as you can i am displaying some set of hybrid which information ,
the information of hybrid along with range,repliacation ,pot pedigreem
qty will be avaialbe in db or text file.I will collect this information
in class which will have all fields as the instance variable it will
finally be collected in List i.e List<HybridVo>.

Now below is the code to display data in serpentine order

Code:
```import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

public class DisplayLogic {
public static void main(String[] args) {
// Lets assume that both the below variable are filled with data.
TreeMap<Integer, TreeMap<Integer, ArrayList<Integer>>> serpentine = new TreeMap<Integer, TreeMap<Integer, ArrayList<Integer>>>();
List<HybridVo>  hybridList = new ArrayList<HybridVo>();

// first iterate over the  serpentine  variable to diplay serpentine row.
Iterator<Integer> serIt = serpentine.keySet().iterator();
while (serIt.hasNext()) {
Integer replication = (Integer) serIt.next();
TreeMap<Integer, ArrayList<Integer>> range = (TreeMap<Integer, ArrayList<Integer>>) serpentine
.get(replication);
Iterator ranIt = range.keySet().iterator();
int z=0;
while (ranIt.hasNext()) {
z++;
List mapKeys = null;
Integer rangeVal = (Integer) ranIt.next();
ArrayList<Integer> intData = range.get(rangeVal);
mapKeys = intData;
/*
* the following is the code to display hybrid in serpentine order
*/
if (z % 2 == 0) {
Collections.sort(mapKeys);
Collections.reverse(mapKeys);
} else {
Collections.sort(mapKeys);

}

Iterator mainIt = mapKeys.iterator();
while (mainIt.hasNext()) {
Integer plot = (Integer) mainIt.next();
// we have to repeat this  iteration for each row of serpentine
//  which to costly
Iterator<HybridVo> hybridVoIt = hybridList.iterator();
while (hybridVoIt.hasNext()) {
HybridVo hybridVo = (HybridVo) hybridVoIt.next();
// this is logic where we get hybrid value which matches as per
// serpentine order
if (hybridVo.getReplicaiton() == replication
&& hybridVo.getPlot() == plot
&& hybridVo.getRange() == rangeVal) {
}
}

}

}
}

}
}```

As you can see that the display algorithm will result into too many iterations which too bad specifically the hybrid iterator has to be iterated for each iteration of serpentine row.Can anybody think of better way of doing same thing