Foreword
For the Java8 stream (stream) This elegant, convenient, fast, efficient operation is already Xinshou stick to the bar, but only List, Set. Do I have to face not Map Well? Required to operate the data transfer from the Map List at work, so there will be this essay.
Create a new User
class, pay attention to the use of the following lombok
; if you are idea
, please download the plug-in and rely on, here I use the maven
quote here a little detail.
/**
* @program: strategy-demo
* @description: stream操作
* @author: YuAoTian
* @create: 2020-01-06 23:23
**/
@Data
@AllArgsConstructor
class User{
private Integer id;
private String name;
private String city;
private Integer age;
}
Continue to look at the code and see Main
which method
public static void main(String[] args) {
List<User> userList = Arrays.asList(
new User((int) (Math.random()*10),"小明","北京",18),
new User((int) (Math.random()*10),"小红", "南京", 17),
new User((int) (Math.random()*10),"小丫", "湖北", 14),
new User((int) (Math.random()*10),"小黑", "深圳", 22),
new User((int) (Math.random()*10),"小玉", "广州", 19),
new User((int) (Math.random()*10),"小小", "江西", 21)
);
Map<Integer, List<User>> userMap = userList.stream().collect(Collectors.groupingBy(User::getId));//已ID作为Key
}
Map Sorting
Positive displacement
Map<Integer, List<User>> map = userMap.entrySet().stream().sorted(Comparator.comparing(o -> o.getValue().get(0).getAge())).map(entry -> {
Map<Integer, List<User>> result = new LinkedHashMap<>();
result.put(entry.getKey(), entry.getValue());
return result;
}).reduce((map1, map2) -> {
map2.forEach(map1::put);
return map1;
}).get();
System.out.print(map);
Export
{5=[User(id=5, name=小丫, city=湖北, age=14), User(id=5, name=小玉, city=广州, age=19), User(id=5, name=小小, city=江西, age=21)], 0=[User(id=0, name=小红, city=南京, age=17)], 8=[User(id=8, name=小明, city=北京, age=18)], 9=[User(id=9, name=小黑, city=深圳, age=22)]}
Inverted
Map<Integer, List<User>> map = userMap.entrySet().stream().sorted(Comparator.comparing(o -> {
//倒排中 reversed() 方法 是object 对象,需要在里面强制回 Entry 就行。
Map.Entry<Integer, List<User>> temp = (Map.Entry<Integer, List<User>>) o;
return temp.getValue().get(0).getAge();
}).reversed()).map(entry -> {
Map<Integer, List<User>> result = new LinkedHashMap<>();
result.put(entry.getKey(), entry.getValue());
return result;
}).reduce((map1, map2) -> {
map2.forEach(map1::put);
return map1;
}).get();
System.out.print(map);
Export
{3=[User(id=3, name=小黑, city=深圳, age=22)], 5=[User(id=5, name=小玉, city=广州, age=19)], 0=[User(id=0, name=小明, city=北京, age=18), User(id=0, name=小丫, city=湖北, age=14)], 2=[User(id=2, name=小红, city=南京, age=17), User(id=2, name=小小, city=江西, age=21)]}
These are for age from small to large sums of sort, and the last return is a Optional
, if not understand can next look at some examples.
Optional<Map<Integer, List<User>>> map = userMap.entrySet().stream().sorted(Comparator.comparing(o -> {
//倒排中 reversed() 方法 是object 对象,需要在里面强制回 Entry 就行。
Map.Entry<Integer, List<User>> temp = (Map.Entry<Integer, List<User>>) o;
return temp.getValue().get(0).getAge();
}).reversed()).map(entry -> {
Map<Integer, List<User>> result = new LinkedHashMap<>();
result.put(entry.getKey(), entry.getValue());
return result;
}).reduce((map1, map2) -> {
map2.forEach(map1::put);
return map1;
});
//为null返回false,否则true,即可调用get()返回~
if(map.isPresent()){
Map<Integer, List<User>> listMap = map.get();//调用get()返回一个Map
}
System.out.print(map);
optimization
Map<Integer, List<User>> map = userMap.entrySet().stream().sorted(Comparator.comparing(o -> {
//倒排中 reversed() 方法 是object 对象,需要在里面强制回 Entry 就行。
Map.Entry<Integer, List<User>> temp = (Map.Entry<Integer, List<User>>) o;
return temp.getValue().get(0).getAge();
}).reversed()).collect(LinkedHashMap::new,(m,e) -> m.put(e.getKey(),e.getValue()),LinkedHashMap::putAll);
The world is not instantaneous feeling a lot of peace and quiet - you can deal with what some of the indentation, can he reduced to one line!
Map filter
map.entrySet().stream().filter(entry -> entry.getValue().get(0).getAge() > 16).collect(LinkedHashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), LinkedHashMap::putAll);
System.out.println(map);
Export
{1=[User(id=1, name=小丫, city=湖北, age=14)]}
Filter out the age of <
16, the filter is very simple I do not provide other examples of; a filter and then directly collect on the line ~
In this paper it is over, if there is a better way to inform Hope ~