I have a SortedMap which has a set of student IDs and their pass result as a Boolean value with the following data:
1000 FALSE
1001 FALSE
1002 FALSE
1003 TRUE
1004 TRUE
1005 TRUE
1006 FALSE
1007 TRUE
1008 FALSE
1009 FALSE
I have a pojo (StudentInfo.java) with the following variables
String startRange, endRange; Boolean isPass
with their respective getters and setters.
I need to traverse through the TreeMap and store the studentID range and the boolean result in the pojo depending on the boolean value in the following format.
startRange: 1000
endRange: 1002
isPass: FALSE
startRange: 1003
endRange: 1005
isPass: TRUE
startRange: 1006
endRange: 1006
isPass: FALSE
startRange: 1007
endRange: 1007
isPass: TRUE
startRange: 1008
sendRange: 1009
isPass: FALSE
For this, I've initialized a new object of type StudentInfo for the above 5 ranges to store it as an ArrayList<StudentInfo>. I've tried out the below logic but I don't seem to get it though.
private void createStudentInfo(SortedMap<String, Boolean> studentDetails, ArrayList<StudentInfo> list) {
StudentInfo studentInfo;
List<String> pass = new ArrayList<String>();
List<String> fail = new ArrayList<String>();
for(Map.Entry<String, Boolean> entry : studentDetails.entrySet()) {
studentInfo = new StudentInfo();
if(!entry.getValue()) {
fail.add(entry.getKey());
if(pass.size() > 0) {
studentInfo.setStartRange(pass.get(0));
studentInfo.setEndRange(pass.get(pass.size()-1));
studentInfo.setPass(true);
list.add(studentInfo);
pass.clear();
}
} else {
pass.add(entry.getKey());
if(fail.size() > 0) {
studentInfo.setStartRange(fail.get(0));
studentInfo.setEndRange(fail.get(fail.size()-1));
studentInfo.setPass(false);
list.add(studentInfo);
fail.clear();
}
}
}
}
How do I go about iterating and forming the above range? Please help.
I tested the code you posted and it almost worked. Except the last group wasn't added.
Here is the improved code I tested that worked:
The output was: