无法实例化类型Map<String,Boolean>

本文详细介绍了在Java编程中遇到无法实例化抽象接口Map<String, Boolean>的问题,并提供了正确的实现方式,即使用其具体实现类HashMap。通过实例演示,帮助开发者避免常见错误并提高代码效率。

无法实例化类型Map<String,Boolean>

错误代码:

[java]  view plain copy
  1. Map<String, Boolean> modelPropertys = new Map<String, Boolean>();  

看了一下Map的定义,public abstract interface java.util.Map

原来是抽象接口,不能直接实例化,需要使用它的实现类;

正确写法如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Map<String, Boolean> modelPropertys = new HashMap<String, Boolean>();  
/** * 快照类:保存排程状态副本 */ public static class ScheduleSnapshot { private final Map<String, Map<Date, BigDecimal>> remainingTimeMap; private final Set<String> filledClasses; private final Map<String, Set<String>> filledClasses2; private final Map<String, Set<String>> deviceOccupyMap; private final Map<String, List<PpMachiningOrderList>> assignedCache; private final Map<String, Map<Date, List<String>>> tem; public ScheduleSnapshot( Map<String, Map<Date, BigDecimal>> remainingTimeMap, Set<String> filledClasses, Map<String, Set<String>> filledClasses2, Map<String, Set<String>> deviceOccupyMap, Map<String, List<PpMachiningOrderList>> assignedCache, Map<String, Map<Date, List<String>>> tem) { this.remainingTimeMap = deepCopyRemainingTimeMap(remainingTimeMap); this.filledClasses = new HashSet<>(filledClasses); this.filledClasses2 = deepCopyFilledClasses2(filledClasses2); this.deviceOccupyMap = deepCopyFilledClasses2(deviceOccupyMap); this.assignedCache = deepCopyAssignedCache(assignedCache); this.tem = deepCopyTem(tem); } private Map<String, Map<Date, BigDecimal>> deepCopyRemainingTimeMap(Map<String, Map<Date, BigDecimal>> src) { return src.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> new HashMap<>(e.getValue()))); } private Map<String, Set<String>> deepCopyFilledClasses2(Map<String, Set<String>> src) { return src.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> new HashSet<>(e.getValue()))); } private Map<String, Map<Date, List<String>>> deepCopyTem(Map<String, Map<Date, List<String>>> src) { return src.entrySet().stream() .collect(Collectors.toMap( Map.Entry::getKey, e -> e.getValue().entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, innerE -> new ArrayList<>(innerE.getValue()))) )); } private Map<String, List<PpMachiningOrderList>> deepCopyAssignedCache(Map<String, List<PpMachiningOrderList>> src) { return src.entrySet().stream() .collect(Collectors.toMap( Map.Entry::getKey, e -> new ArrayList<>(e.getValue()) // 浅拷贝:新建 list,但对象引用相同 )); } /** * 恢复所有状态 */ public void restore( Map<String, Map<Date, BigDecimal>> remainingTimeMap, Set<String> filledClasses, Map<String, Set<String>> filledClasses2, Map<String, Map<Date, List<String>>> tem, Map<String, Set<String>> deviceOccupyMap, Map<String, List<PpMachiningOrderList>> assignedCache) { remainingTimeMap.clear(); remainingTimeMap.putAll(this.remainingTimeMap); filledClasses.clear(); filledClasses.addAll(this.filledClasses); filledClasses2.clear(); filledClasses2.putAll(this.filledClasses2); tem.clear(); tem.putAll(this.tem); deviceOccupyMap.clear(); deviceOccupyMap.putAll(this.deviceOccupyMap); assignedCache.clear(); assignedCache.putAll(this.assignedCache); } }解析
最新发布
12-17
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值