Elegant use of Java 8 stream, combined with practical analysis, java code performance tuning

elegant use java stream combined

List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>() { {

add(new HashMap<String, Integer>() { { put(“a”, 1);put(“c”, 3); }});

add(new HashMap<String, Integer>() { { put(“a”, 11);put(“b”, 2); }});

add(new HashMap<String, Integer>() { { put(“a”, 111);put(“c”, 1); }});

add(new HashMap<String, Integer>() { { put(“b”, 22); }});

add(new HashMap<String, Integer>() { { put(“a”, 1111);put(“b”, 222); }});

}};

//todo Put the above list After processing , The output is one of the following :

// 1. Output is :{a=[{a=1111}, {a=111}, {a=11}, {a=1}], b=[{b=222}, {b=22}, {b=2}], c=[{c=3}, {c=1}]}

// 2. Output is :{a=[a=1111, a=111, a=11, a=1], b=[b=222, b=22, b=2], c=[c=3, c=1]}

Map<String,Object> resultMap=new HashMap<>();

List<Map<String, Object>> listA = new ArrayList<>();

List<Map<String, Object>> listB = new ArrayList<>();

List<Map<String, Object>> listC = new ArrayList<>();

for (Map<String, Integer> get_map:list){

for(Map.Entry<String, Integer> entry : get_map.entrySet()){

if (entry.getKey().equals(“a”)){

Map<String, Object> processMapA= new HashMap<>();

processMapA.put(entry.getKey(),entry.getValue());

listA.add(processMapA);

}

if (entry.getKey().equals(“b”)){

Map<String, Object> processMapB= new HashMap<>();

processMapB.put(entry.getKey(),entry.getValue());

listB.add(processMapB);

}

if (entry.getKey().equals(“c”)){

Map<String, Object> processMapC= new HashMap<>();

processMapC.put(entry.getKey(),entry.getValue());

listC.add(processMapC);

}

}

}

// Sort

listA.sort(new Comparator<Map<String, Object>>() {

public int compare(Map<String, Object> o1, Map<String, Object> o2) {

Integer value1 = Integer.valueOf(o1.get(“a”).toString());

Integer value2 = Integer.valueOf(o2.get(“a”).toString());

return value2.compareTo(value1);

}

});

//lambda transformation

listB.sort((o1, o2) -> {

Integer value1 = Integer.valueOf(o1.get(“b”).toString()) ;

Integer value2 = Integer.valueOf(o2.get(“b”).toString()) ;

return value2.compareTo(value1);

});

listC.sort((o1, o2) -> {

Integer value1 = Integer.valueOf(o1.get(“c”).toString());

Integer value2 = Integer.valueOf(o2.get(“c”).toString());

return value2.compareTo(value1);

});

resultMap.put(“a”,listA);

resultMap.put(“b”,listB);

resultMap.put(“c”,listC);

System.out.println(resultMap.toString());

You can see that the code is really a little less elegant

《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing

( Or maybe I didn't think about how to optimize the process , Forgive me ), But if you change it to Stream operation , Then it will brighten people's eyes .

Sream flow solution


List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>() { {

add(new HashMap<String, Integer>() { { put(“a”, 1);put(“c”, 3); }});

add(new HashMap<String, Integer>() { { put(“a”, 11);put(“b”, 2); }});

add(new HashMap<String, Integer>() { { put(“a”, 111);put(“c”, 1); }});

add(new HashMap<String, Integer>() { { put(“b”, 22); }});

add(new HashMap<String, Integer>() { { put(“a”, 1111);put(“b”, 222); }});

}};

//todo Put the above list After processing , The output is one of the following :

// 1. Output is :{a=[{a=1111}, {a=111}, {a=11}, {a=1}], b=[{b=222}, {b=22}, {b=2}], c=[{c=3}, {c=1}]}

// 2. Output is :{a=[a=1111, a=111, a=11, a=1], b=[b=222, b=22, b=2], c=[c=3, c=1]}

//stream

Map map = list.stream()

.flatMap(e -> e.entrySet().stream())

.sorted(((o1, o2) -> o2.getValue().compareTo(o1.getValue())))

.map(v -> new HashMap<String, Integer>() { { put(v.getKey(), v.getValue()); }})

.collect(Collectors.groupingBy(o -> o.entrySet().iterator().next().getKey()));

System.out.println(map);

Last , Elegance belongs to elegance , If you need to adjust the logic later in the code , Will make you unfamiliar stream Big head ( And there are performance problems ), Combined with reality, elegance is really elegant .

Smile code , Let yourself look elegant first .

copyright:author[m0_ sixty-four million three hundred and eighty-four thousand t],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261259557694.html