【发布时间】:2018-09-22 06:42:34
【问题描述】:
我有以下地图:
HashMap<Integer, HashMap<Integer, Employee>> hmap = new HashMap<Integer,HashMap<Integer, Employee>>();
HashMap<Integer, Employee> emap = new HashMap<Integer, Employee>();
我正在使用 keyset 将对象从 emap 中取出并比较员工工资。 如果工资低于特定金额,我需要将该映射放入 hmap 中,键为 0,如果高于则键为 1。
for(Integer e: emap.keySet())
{
if(emap.get(e).getSalary()<45000.0)
{
hmap.put(0,//what should i put here );
}
else
{
hmap.put(1,//what should i put here );
}
}
谢谢。
【问题讨论】:
-
为什么
hmap也不能是HashMap<Integer, Employee>?如果你有超过 2 张地图,你会怎么做,因为你只有0和1作为 key? -
hmap 将只有 2 个键,1 和 0。1 表示 emap 的所有有效记录,0 表示所有无效。
-
您需要将其声明为 Map
> 而不是 HashMap。尽可能使用接口类型。 -
只需使用
partitionBy。
标签: java collections hashmap