内部类方式为HashMap赋初值
程序员文章站
2024-02-02 15:46:46
...
Map map = new HashMap(){
{
put("test",new TestClass());
}
}
第一层花括号表示声明了一个匿名内部类,new 关键词将此类实例化
第二层花括号表示实例初始化块,可以使用HashMap的各种方法
实例初始化块就是对构造器的补充,不能接收任何参数,类实例化时执行
tradeSubPlatforms = baseDAO.getPageBySql(queryTradeSubPlatformInfoSql(), TradeSubPlatformVO.class,params).getResult();
HashMap<String, TradeSubPlatformVO> areaMap = new HashMap<String, TradeSubPlatformVO>(){{//为城14区赋初值
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2016);
calendar.set(Calendar.MONTH, 1);
calendar.set(Calendar.DATE, 2);
Date date = calendar.getTime();
put("2Z006",new TradeSubPlatformVO(date,"2Z006","0"));//东城
put("2Z007",new TradeSubPlatformVO(date,"2Z007","0"));//西城
put("2Z008",new TradeSubPlatformVO(date,"2Z008","0"));//朝阳
put("2Z009",new TradeSubPlatformVO(date,"2Z009","0"));//海淀
put("2Z010",new TradeSubPlatformVO(date,"2Z010","0"));//丰台
put("2Z011",new TradeSubPlatformVO(date,"2Z011","0"));//石景山
put("2Z012",new TradeSubPlatformVO(date,"2Z012","0"));//门头沟
put("2Z013",new TradeSubPlatformVO(date,"2Z013","0"));//房山
put("2Z014",new TradeSubPlatformVO(date,"2Z014","0"));//通州
put("2Z015",new TradeSubPlatformVO(date,"2Z015","0"));//顺义
put("2Z016",new TradeSubPlatformVO(date,"2Z016","0"));//大兴
put("2Z017",new TradeSubPlatformVO(date,"2Z017","0"));//昌平
put("2Z018",new TradeSubPlatformVO(date,"2Z018","0"));//平谷
put("2Z019",new TradeSubPlatformVO(date,"2Z019","0"));//怀柔
put("2Z020",new TradeSubPlatformVO(date,"2Z020","0"));//密云
put("2Z021",new TradeSubPlatformVO(date,"2Z021","0"));//延庆
}
};
for(TradeSubPlatformVO vo : tradeSubPlatforms){
areaMap.put(vo.getPlatform(),vo);//为areaMap二次赋值
}
上一篇: 栈的操作(顺序栈,链栈)
下一篇: 求一个数的绝对值
推荐阅读