1、抽离List中Bean的属性值
List<String> rateIdList = stationList.stream().map(CdzStation::getRates).collect(Collectors.toList());
2、List to Map(val:Bean),相同key、覆盖
Map<String, PlannedCost> dbSubCodeMap = dbTotalList.stream().collect(Collectors.toMap(PlannedCost::getSubjectCode, v1 -> v1, (v1, v2) -> v2));
3、List to Map(val:Bean.prop)
Map<String, String> ratesNameMap = ratesList.stream().collect(Collectors.toMap(CdzRates::getId, CdzRates::getName, (v1, v2) -> v2));
4、Map,key 与 val 值互换
Map<String, String> reverseMap = dataMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
4.1、Map的value值求和
levelMap.values().stream().mapToLong(Long::longValue).sum();
~~
5、List to Set
List<String> dataList = new ArrayList<>();
Set<String> set = dataList.stream().collect(Collectors.toSet());
6、Set to List
Set<String> dataSet = new HashSet<>();
List<String> dataList = dataSet.stream().collect(Collectors.toList());
7、List to Array
List<String> dataList = new ArrayList<>();
String[] dataArray = dataList.stream().toArray(String[]::new);
8、Array to List
String[] dataArray = new String[]{"a", "b", "c"};
List<String> dataList = Arrays.asList(dataArray);
9、List 求交集
List<String> dataListFrom = new ArrayList<>();
List<String> dataListTo = new ArrayList<>();
List<String> intersectionList = dataListFrom.stream().filter(item -> dataListTo.contains(item)).collect(toList());
10、List<Map> to Map
static Map<String, Map<String, Object>> toMap(List<Map<String, Object>> dataList, String uniKey) {
Map<String, Map<String, Object>> dataMap = MapUtil.newHashMap();
String id = null;
for (Map<String, Object> data : dataList){
id = StringUtils.val(data.get(uniKey));
if (StrUtil.isEmpty(id)) {
// 无此字段,不处理
continue;
}
dataMap.put(id, data);
}
return dataMap;
}
/**
* 空串
*/
private static final String EMPTY_STR = "";
/**
* 获取值
* @param obj 请求对象
* @return
*/
public static String val(Object obj) {
if (null == obj) {
return EMPTY_STR;
} else {
String str = obj.toString().trim();
return (StrUtil.isEmpty(str) ? EMPTY_STR : str);
}
}
11、Map.values to List
Map<String, Map<String, Object>> fromDataMap = new HashMap<>();
List<Map<String, Object>> insertDataList = fromDataMap.values().stream().collect(Collectors.toList());
12、List 删除item空格
List<String> dataList = CollUtil.newArrayList();
dataList.add(" a 1");
dataList.add("b 1");
dataList.add(" ");
System.out.println("dataList.before:" + dataList);
int size = dataList.size();
for (int i = 0; i < size; i++) {
dataList.set(i, dataList.get(i).replaceAll(" ", ""));
}
dataList.remove("");
//dataList = dataList.stream().filter(item -> StrUtil.isNotEmpty(item.replaceAll(" ", ""))).collect(Collectors.toList());
System.out.println("dataList.after:" + dataList);
System.out.println("---------------------------");
System.out.println("ok");
13、List,最大/最小bean
Student max = stuList.stream().max(Comparator.comparing(Student::getCourseScoreDouble)).get();
Student min = stuList.stream().min(Comparator.comparing(Student::getCourseScoreDouble)).get();
14、List,Bean排序
// 升序
curStuList = curStuList.stream().sorted(Comparator.comparing(Student::getStuId)).collect(Collectors.toList());
// 降序
curStuList = curStuList.stream().sorted(Comparator.comparing(Student::getStuId).reversed()).collect(Collectors.toList());
15、List<Map<>>> 排序
/**
*
* @param list
* @param sortKey
* @param asc true,升序
*/
public static void sort(List<Map<String, Object>> list, String sortKey, boolean asc) {
Collections.sort(list, new Comparator<Map<String, Object>>() {
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
Object obj1 = o1.get(sortKey);
Object obj2 = o2.get(sortKey);
if (obj1 instanceof Integer) {
if (asc) {
return IntegerUtils.valDefaultZero(obj1.toString()).compareTo(Integer.parseInt(obj2.toString()));
} else {
return IntegerUtils.valDefaultZero(obj2.toString()).compareTo(Integer.parseInt(obj1.toString()));
}
} else {
if (asc) {
return StringUtils.val(obj1).compareTo(StringUtils.val(obj2));
} else {
return StringUtils.val(obj2).compareTo(StringUtils.val(obj1));
}
}
}
});
}
16、List<Bean> to TreeSet
CollUtil.toTreeSet(dataList, Comparator.comparing(Pro::getCode));
17、List Filter过滤null
dataList.stream().filter(i -> null != i).collect(Collectors.toList());
18、Map根据Key排序
// WorkOrderMonthNum为实体,可忽略
Map<Integer, List<WorkOrderMonthNum>> monthMap = MapUtil.newHashMap();
// sortMap用于存放排序后的结果集
Map<Integer, List<WorkOrderMonthNum>> sortMap = new LinkedHashMap<>();
// monthMap排序前的结果集,put过程忽略
monthMap.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByKey())).forEachOrdered(x -> sortMap.put(x.getKey(), x.getValue()));
// 升序
.sorted(Map.Entry.comparingByKey())
// 降序
.sorted(Collections.reverseOrder(Map.Entry.comparingByKey()))
19、Map根据Value排序
1)升序
Map<String, Long> numMap = MapUtil.newHashMap(); Map<String, Long> sortedMap = new LinkedHashMap<>(); //ASC numMap.entrySet().stream() .sorted(Map.Entry.comparingByValue()) .forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
2)降序
Map<String, Long> numMap = MapUtil.newHashMap(); Map<String, Long> sortedMap = new LinkedHashMap<>(); //DESC numMap.entrySet().stream() .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())) .forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));