【发布时间】:2019-02-01 05:47:49
【问题描述】:
需要将 for 循环 (Java 6) 转换为 foreach (Java 8)
List<CustomFormAttributeLite> custFormAttrLiteList = ArrayList ... ;
Map<String,Map<Long,Long>> customNameId = new HashMap<String, Map<Long,Long>>();
Map<Long,Long> custNameAndType = null;
for(CustomFormAttributeLite customFormAttributeLite:custFormAttrLiteList) {
custNameAndType = new HashMap<Long,Long>();
custNameAndType.put(customFormAttributeLite.getId(), customFormAttributeLite.getFieldType());
customNameId.put(customFormAttributeLite.getName(), custNameAndType);
}
我正在尝试类似的东西..但不知道该怎么做
custFormAttrLiteList.forEach((customFormAttributeLite)->
custNameAndType = new HashMap<Long,Long>();
custNameAndType.put(customFormAttributeLite.getId(), customFormAttributeLite.getFieldType());
customNameId.put(customFormAttributeLite.getName(), custNameAndType);
);
【问题讨论】:
-
这些是您正在使用的 lambda,这就是您应该寻找的,您几乎得到了每个权利。
-
最好使用
groupingBy。