您可以嘗試使用員工和他們每月的條目之間的映射。
注:這幾乎是完整的代碼,但像解析一行或數月的某些部分,你需要做自己
class EmployeeEntry {
String date;
String name;
String surname;
// other fields representing excel columns
TYPE_OF_DAY typeOfday;
public static enum TYPE_OF_DAY {
LOP, HALF_DAY, OTHER
}
public String getEmployeeID() {
// you may return name+surname or some unique ID
return name + " " + surname;
}
public Integer getMonth() {
String monthStr = date.split("-")[1];
return asMonthNumber(monthStr);
// implement asMonthNumber to convert Aug --> 08
}
}
public class A {
private Map<String, Map<Integer, List<EmployeeEntry>>> entries;
public void parseExcel(HSSFSheet sheet) {
entries = new HashMap<String, Map<Integer, List<EmployeeEntry>>>();
Iterator<Row> iter = sheet.iterator();
// for every row
while (iter.hasNext()) {
Row row = iter.next();
// parse will call getString, getNumber etc of the current row
EmployeeEntry entry = parse(row);
Map<Integer, List<EmployeeEntry>> userMonthlyEntriesMap = getOrCreate(entry
.getEmployeeID());
List<EmployeeEntry> monthlyEntries = getOrCreate(
userMonthlyEntriesMap, entry.getMonth());
monthlyEntries.add(entry);
}
}
public int countLOP(String employeeID, Integer monthNum) {
int counter=0;
Map<Integer, List<EmployeeEntry>> map = entries.get(employeeID);
if (map != null) {
List<EmployeeEntry> list = map.get(monthNum);
if (list != null) {
for (EmployeeEntry entry : list) {
if (entry.typeOfday == EmployeeEntry.TYPE_OF_DAY.LOP) {
counter++;
}
}
}
}
return counter;
}
private List<EmployeeEntry> getOrCreate(
Map<Integer, List<EmployeeEntry>> userMonthlyEntriesMap,
Integer month) {
List<EmployeeEntry> monthlyEntries = userMonthlyEntriesMap.get(month);
if (monthlyEntries == null) {
monthlyEntries = new LinkedList<EmployeeEntry>();
userMonthlyEntriesMap.put(month, monthlyEntries);
}
return monthlyEntries;
}
public Map<Integer, List<EmployeeEntry>> getOrCreate(String emplID) {
Map<Integer, List<EmployeeEntry>> entryList = entries.get(emplID);
if (entryList == null) {
entryList = new HashMap<Integer, List<EmployeeEntry>>();
entries.put(emplID, entryList);
}
return entryList;
}
}
我給你下面幾乎正確的解決方案,但沒有你的迴應。所以我給你-1無用的問題。 –
對不起...延遲迴應...雅我幾乎是正確的解決方案,我正在尋找那個時間。非常感謝你@PawelSolarski – Amar