区别:
LinkedHashMap有序
HashMap无序
场景:将列表中的所有对象进行遍历,对象的某一属性相同的进行归类
代码语言:javascript复制Map<String, List<RecommendationListBO>> hashMap = new LinkedHashMap<>();
/*包装结构*/
for(RecommendationListBO recommendationListBO : recommendationList){
/*map中存在此id,将数据存放当前key的map中*/
if(hashMap.containsKey(recommendationListBO.getInsurederName())){
hashMap.get(recommendationListBO.getInsurederName()).add(recommendationListBO);
}else{//map中不存在,新建key,用来存放数据
List<RecommendationListBO> recommendationListBOList = new ArrayList<>();
recommendationListBOList.add(recommendationListBO);
hashMap.put(recommendationListBO.getInsurederName(), recommendationListBOList);
}
}
若此处无序,recommendationListBO放入hashMap后将不再按照recommendationListBO存放在recommendationList里面的顺序排序,(recommendationList的结果是调用sql排序的)的用处便不存在了。