List 循环
list.forEach(map -> {
//此处可以对map进行操作
});
根据【A属性的值】对List进行分组
//stream()方法不会改变原有tempList
Map<String, List<Map<String, Object>>> tempMap = tempList.stream().collect(Collectors.groupingBy(map -> MapUtils.getString(map, "A")));
List 转 Map
//stream()方法不会改变原有tempList,当key重复时,以最新的为准
Map<String, String> rowMap = tempList.stream().collect(Collectors.toMap(map1 -> MapUtils.getString(map1, "A"), map2 -> MapUtils.getString(map2, "B"), (key1, key2) -> key2));
//Function.identity()应用
List<Person> personList = new ArrayList<>();
Map<String, Person> collect = personList.stream().collect(Collectors.toMap(Person::getName, Function.identity()));
List 根据【A属性的值】 进行排序
//方法一:无stream()方法会对原有tempList进行改变
tempList.sort((Map<String, Object> m1, Map<String, Object> m2) -> MapUtils.getIntValue(m1, "A", 0) - MapUtils.getIntValue(m2, "A", 0));
//方法二:使用 Comparator.comparing 进行排序(map和实体)
tempList.sort(Comparator.comparingLong(m -> MapUtils.getLong(m, "A")));
//默认 升序
tempObject.sort(Comparator.comparing(Object::getA));
//降序
tempObject.sort(Comparator.comparing(Object::getA).reversed());
//其余方式参考:https://my.oschina.net/xinxingegeya/blog/2046405
获取List单列值
//stream()方法不会改变原有tempList
List<String> resultList = tempList.stream().map(map -> MapUtils.getString(map, "A")).collect(Collectors.toList());
List转Array
String[] tempArr= resultList.stream().toArray(String[]::new);
int[] tempArr = resultList.stream().mapToInt(p -> p.intValue()).toArray();
int[] tempArr = resultList.stream().mapToInt(Integer :: intValue).toArray();
List< String>去重
//List<String>进行去重
List<String> list = resultList.stream().distinct().collect(Collectors.toList());
List截取
List<String> list = resultList.stream().skip(start).limit(end).collect(Collectors.toList());