/**
最常见也是大多数情况下用的最多的,一般在键值对都需要使用(map遍历效率第三高) */ Map <String,String>map = new HashMap<String,String>(); map.put(“熊大”, “棕色”); map.put(“熊二”, “黄色”); for(Map.Entry<String, String> entry : map.entrySet()){ String mapKey = entry.getKey(); String mapValue = entry.getValue(); System.out.println(mapKey+":"+mapValue); }Map<String, Integer> map1 = new HashMap<String, Integer>(); map1.put(“jack”, 20); map1.put(“rose”, 18); map1.put(“lucy”, 17); map1.put(“java”, 25);
//第四种Iterator遍历获取,然后获取到Map.Entry<String, String>,再得到getKey()和getValue()(map遍历效率最高) Iterator<Map.Entry<String, Integer>> it=map1.entrySet().iterator(); while(it.hasNext()){ Map.Entry<String, Integer> entry=it.next(); System.out.println(“键key :”+entry.getKey()+" value :"+entry.getValue()); }
Java8
//品类分类及占比赋值(map遍历效率次高) map.forEach((key, value) -> { manageGoodsCategoryTypeViewList.add(new ManageGoodsCategoryTypeView(key, value, finalTotalSales)); });
//按销售额降序排序 if (!manageGoodsCategoryTypeViewList.isEmpty()) { manageGoodsCategoryTypeViewSortList = manageGoodsCategoryTypeViewList.stream().sorted(Comparator.comparing(ManageGoodsCategoryTypeView::getSales).reversed()).collect(Collectors.toList()); }
int ageSum = userList.stream().collect(Collectors.summingInt(User::getAge)); int wholeCountryNum = newUserList.parallelStream().mapToInt(NewUser::getTotalNum).sum(); double amount = userInfoTicketDetailTotalList.parallelStream().mapToDouble(UserInfoTicketDetailTotal::getAmount).sum();
用lambda 表达式的链式操作一次获取结果,相同分类id的商品数量求和: Integer sum = list.stream().filter(item -> item.getCategoryId().equals(1)).collect(Collectors.toList()).stream().mapToInt(Cart::getCount).sum();
java8取出list中的对象的某一属性 List courseIds= users.stream().map(UserEntity::getUserName).collect(Collectors.toList());
