我所有的excel表都包含下面提到的数据类型。
01-Aug-2012 EST213 Sowmya Shivashankar 11 0 11 4 0 LOP
01-Aug-2012 EST101 Prashanth P 12 8 20 5 5.28 0 Half-day
08-Aug-2012 EST213 Sowmya Shivashankar 11 0 11 4 0 LOP
我在这里为一名员工提供一天的数据。像这样我一个月就有大约3千行数据。我需要计算需要为特定员工计算多少LOP或半月。
答案 0 :(得分:1)
我这样做:
HashMap<String, List>
EmployeeName
和(LOP
或Half-day
)Integer
,请创建一个新对,添加LOP
或Half-Day
。如果有List
,请向其添加新记录。或者您可以创建类似
的结构{
Integer lop;
Integer halfDay;
}
而不是List。
答案 1 :(得分:1)
您可以尝试使用员工及其月度条目之间的映射。
注意:这几乎是完整的代码,但有些部分比如解析你需要自己做的行或月号
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;
}
}