1.对List<Map<String,Object>>对象中Map的某个key进行排序
import java.util.*;
import java.util.stream.Collectors;
/*
* 升序
*/
public class Test{
public static void main(String[] args) {
List<Map<String,Object>> list= new ArrayList<>();
Map<String,Object> mapOne= new HashMap<>();
mapOne.put("sum", 100);
Map<String,Object> mapTwo= new HashMap<>();
mapTwo.put("sum", 50);
Map<String,Object> mapThree= new HashMap<>();
mapThree.put("sum", 150);
list.add(mapOne);
list.add(mapTwo);
list.add(mapThree);
System.out.println("排序前:"+list.toString());
List<Map<String,Object>> listSort = list.stream().sorted(Comparator
.comparing(Test::comparingBySum))
.collect(Collectors.toList());
System.out.println("排序后:"+listSort.toString());
}
private static Integer comparingBySum(Map<String, Object> map){
return (Integer) map.get("sum");
}
}
import java.util.*;
import java.util.stream.Collectors;
/*
* 降序
*/
public class Test{
public static void main(String[] args) {
List<Map<String,Object>> list= new ArrayList<>();
Map<String,Object> mapOne= new HashMap<>();
mapOne.put("sum", 100);
Map<String,Object> mapTwo= new HashMap<>();
mapTwo.put("sum", 50);
Map<String,Object> mapThree= new HashMap<>();
mapThree.put("sum", 150);
list.add(mapOne);
list.add(mapTwo);
list.add(mapThree);
System.out.println("排序前:"+list.toString());
List<Map<String,Object>> listSort = list.stream().sorted(Comparator
.comparing(Test::comparingBySum).reversed())
.collect(Collectors.toList());
System.out.println("排序后:"+listSort.toString());
}
private static Integer comparingBySum(Map<String, Object> map){
return (Integer) map.get("sum");
}
}
2.将List<Map<String,Object>>转换为Map<String,Object>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test{
public static void main(String[] args) {
List<Map<String,Object>> list = new ArrayList();
Map<String,Object> map1 = new HashMap();
map1.put("fruit","Apple");
map1.put("color","Red");
list.add(map1);
map1 = new HashMap();
map1.put("fruit","Orange");
map1.put("color","Orange");
list.add(map1);
map1 = new HashMap();
map1.put("fruit","grape");
map1.put("color","pink");
list.add(map1);
map1 = new HashMap();
map1.put("fruit","watermelon");
map1.put("color","green");
list.add(map1);
Map resultMap = list.stream().collect(
Collectors.toMap(s->s.get("fruit"), s -> s));
System.out.println(resultMap);
Map resultMapTwo = list.stream().collect(
Collectors.toMap(s->s.get("fruit"), s -> s.get("color")));
System.out.println(resultMapTwo);
}
}
// List<实体>转Map<String,List<实体>>
Map<String,List<ScheduleInfoDTO>> userScheduleListMap=Optional.ofNullable(weekScheduleInfoDTOList).orElse(new ArrayList()).stream()
.collect(Collectors.groupingBy(ScheduleInfoDTO::getUserId));
// 实体类的转换
// 特殊使用:当数据呈现树状结构的时候
3.删除Map<String,Object>中key="xxx"的元素// 根据map中得值去判断删除map.values().removeIf(value -> value.contains(“xxx”));
import java.util.*;
public class Test{
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("zoo","pig");
map.put("fruit","apple");
map.put("machine","phone");
map.put("book","sss");
System.out.println("map=======>"+map);
map.values().removeIf(value -> value.contains("sss"));
System.out.println("remove=======>"+map);
}
}
// 根据key删除
map.keySet().removeIf(key -> key.contains(“xxx”));
import java.util.*;
public class Test{
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("zoo","pig");
map.put("fruit","apple");
map.put("machine","phone");
map.put("book","sss");
System.out.println("map=======>"+map);
map.keySet().removeIf(key -> key.contains("book"));
System.out.println("remove=======>"+map);
}
}
// 通过getkey()方法获得值去删除
map.entrySet().removeIf(entry -> entry.getKey().contains(“xxx”));
import java.util.*;
public class Test{
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("zoo","pig");
map.put("fruit","apple");
map.put("machine","phone");
map.put("book","sss");
System.out.println("map=======>"+map);
map.entrySet().removeIf(entry -> entry.getKey().contains("book"));
System.out.println("remove=======>"+map);
}
}
Map<String,List> userScheduleListMap=Optional.ofNullable(weekScheduleInfoDTOList).orElse(new ArrayList()).stream()
.collect(Collectors.groupingBy(ScheduleInfoDTO::getUserId));