如何从Map <string list <object =“”>&gt;获取值?

时间:2016-01-16 08:40:41

标签: java generics dictionary

我有一个模型类EmployeeBaseData.java看起来像这样(这只是例如):

public class EmployeeBaseData {
public EmployeeBaseData(String sid){
    this.sid = sid;

}
private String sid;
private int actualHours = 10;
private int regularHours = 9;
private int overtime = 1;
public String getSid() {
    return sid;
}
public void setSid(String sid) {
    this.sid = sid;
}
public int getActualHours() {
    return actualHours;
}
public void setActualHours(int actualHours) {
    this.actualHours = actualHours;
}
public int getRegularHours() {
    return regularHours;
}
public void setRegularHours(int regularHours) {
    this.regularHours = regularHours;
}
public int getOvertime() {
    return overtime;
}
public void setOvertime(int overtime) {
    this.overtime = overtime;
}

这是控制器:

public class InputController {

public static void main(String[] args) {

    try {

        Map<String, List<Object>> employeeMap = new HashMap<String, List<Object>>();
        EmployeeBaseData base_data1 = new EmployeeBaseData("e774801");
        EmployeeBaseData base_data2 = new EmployeeBaseData("e774802");
        List<Object> valSetOne = new ArrayList<Object>();
        List<Object> valSetTwo = new ArrayList<Object>();
        valSetOne.add(base_data1);
        valSetTwo.add(base_data2);
        employeeMap.put("e774801", valSetOne);
        employeeMap.put("e774802", valSetTwo);
        for(Map.Entry<String, List<Object>> entry : employeeMap.entrySet()){
            String key = entry.getKey();
            List<Object> value= entry.getValue();

        }

现在问题是我是否需要访问模型的getOverTime()方法,那么我该如何从地图中获取它呢?

3 个答案:

答案 0 :(得分:1)

@epoch对于地图的正确定义有正确的想法。但值得指出的是,Java 8为您提供了一种非常优雅的方式来获取所有加班时间,而无需编写繁琐的循环:

List<Integer> overtimes = 
    employeeMap.values()
               .stream()
               .flatMap(Collection::stream)
               .map(EmployeeBaseData::getOvertime)
               .collect(Collectors.toList());

答案 1 :(得分:0)

您的问题是您使用Object作为ArrayList的通用类型,您应该在List中指定数据类型,即:

    Map<String, List<EmployeeBaseData >> employeeMap = new HashMap<String, List<EmployeeBaseData >>();

    EmployeeBaseData base_data1 = new EmployeeBaseData("e774801");
    EmployeeBaseData base_data2 = new EmployeeBaseData("e774802");

    List<EmployeeBaseData > valSetOne = new ArrayList<>();
    List<EmployeeBaseData > valSetTwo = new ArrayList<>();

    valSetOne.add(base_data1);
    valSetTwo.add(base_data2);

    employeeMap.put("e774801", valSetOne);
    employeeMap.put("e774802", valSetTwo);

    for(Map.Entry<String, List<EmployeeBaseData>> entry : employeeMap.entrySet()){
        String key = entry.getKey();
        List<EmployeeBaseData> value = entry.getValue();

        // access objects here:
        for (EmployeeBaseData a : value) {
            a.getOverTime();
        }
    }

答案 2 :(得分:0)

我同意时代的答案。 (即使用List<EmployeeBaseData>

或者,如果您还想在列表中存储其他类型的对象,那么List<Object>就可以了。在这种情况下,您只需检查类型执行广告

   for(Map.Entry<String, List<Object>> entry : employeeMap.entrySet()){
        String key = entry.getKey();
        List<Object> valueList = entry.getValue();
        for (Object item : valueList)
        {
          // CHECK THE TYPE
          if (item instanceof EmployeeBaseData)
          {
             // PERFORM A CAST
             EmployeeBaseData ebd = (EmployeeBaseData) item;
             int overtime = ebd.getOverTime();
          }
        }
    }