- 多个数据集合如何合并为一个数组
List<Map<String,Object>> data1 = this.queryReportInvestSu(param);
List<Map<String,Object>> data2 = this.queryReportInvestSu(param);
List<Map<String,Object>> data3 = this.queryReportInvestSu(param);
List<Map<String,Object>> data4 = this.queryReportInvestSu(param);
List<Map<String,Object>> dataAll = Stream.of(data1, data2, data3, data4)
.flatMap(Collection::stream)
.collect(Collectors.toList());
- 二维数据如何拍平为一维数组, (如果你不需要去重,可以将distinct去掉)
List<String> investorIds = investorIdArrays.stream().flatMap(Arrays::stream).distinct().collect(Collectors.toList());
- 获取数组中每条数据中某一个属性的值
List<Map<String,Object>> data = this.queryReportInvestSu(param);
List<String> reviewerIdArrays = data.stream().map((item)-> item.get("REVIEWER_ID").toString()).collect(Collectors.toList());
- java8 针对map中的key排序
public static TreeMap<String,Object> sortKeyMap(Map<String,Object> map){
TreeMap<String, Object> result = map.entrySet().stream()
.sorted(Comparator.comparing(Map.Entry::getKey))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (k1, k2) -> k1, TreeMap::new));
System.out.println("result: " + result);
return result
}
- java8 针对map中的值排序
public static TreeMap<String,Object> sortValueMap(Map<String,Object> map){
TreeMap<String, String> result = map.entrySet().stream()
.sorted(Comparator.comparing(Map.Entry::getValue))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, TreeMap::new));
System.out.println("result: " + result);
return result
}
- java8 按数值进行排序
List<Map<String,Object>> list2 = list.stream().sorted((e1,e2)->{
if(!e1.containsKey("SUM")) {
e1.put("SUM", 0.00);
}
if(!e2.containsKey("SUM")) {
e2.put("SUM", 0.00);
}
return Double.compare((Double)e1.get("SUM"),(Double) e2.get("SUM"));
}).collect(Collectors.toList());
System.out.println("map 按数值排序:"+list2);
List<Employee> emps = Arrays.asList(
new Employee("啊三",18,999.99,Status.BUSY),
new Employee("张四",36,9992341.99,Status.FREE),
new Employee("六五",28213,9199.99,Status.FREE),
new Employee("欧弟",183121,99.2,Status.VOCATION),
new Employee("秋七",823,10.3,Status.FREE),
new Employee("绿七",82,10.1,Status.VOCATION),
new Employee("张七",82,10.2,Status.BUSY)
);
System.out.println("实体类 排序前的值 : "+emps);
List<Employee> emps2 = emps.stream().sorted((e1,e2)-> Double.compare((Double)e1.getSalary(),(Double) e2.getSalary())).collect(Collectors.toList());
System.out.println("实体类按数值排序" + emps2);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- java8 按照中文排序
List<Map<String,Object>> list4 = list.stream().sorted((e1,e2)->{
Collator collator = Collator.getInstance(Locale.CHINA);
return collator.compare(e1.get("NAME"), e2.get("NAME"));
}).collect(Collectors.toList());
System.out.println("map 按中文排序" + list4);
List<Map<String,Object>> list5 = list.stream().sorted((e1,e2)->{
Collator collator = Collator.getInstance(Locale.CHINA);
return collator.compare(e2.get("NAME"),e1.get("NAME"));
}).collect(Collectors.toList());
System.out.println("map 按中文倒序" + list5);
List<Employee> emps3 = emps.stream().sorted((e1,e2)->{
Collator collator = Collator.getInstance(Locale.CHINA);
return collator.compare(e1.getName(), e2.getName());
}).collect(Collectors.toList());
System.out.println("实体类按中文排序" + emps3);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- java8 根据集合中的某个属性进行分组
package java8分组之后的数据和之前的顺序保持一致;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Dome {
public static void main(String[] args) {
List<Map<String,Object>> list = new ArrayList<>();
Map<String,Object> map1 = new HashMap<>();
map1.put("key1", "aa");
map1.put("key2", "bb");
map1.put("key3", "cc");
map1.put("key4", "dd");
Map<String,Object> map2 = new HashMap<>();
map2.put("key1", "aa");
map2.put("key2", "bb");
map2.put("key3", "cc");
map2.put("key4", "dd");
Map<String,Object> map3 = new HashMap<>();
map3.put("key1", "aa");
map3.put("key2", "bb1");
map3.put("key3", "cc");
map3.put("key4", "dd");
Map<String,Object> map4 = new HashMap<>();
map4.put("key1", "aa");
map4.put("key2", "bb1");
map4.put("key3", "cc");
map4.put("key4", "dd");
Map<String,Object> map5 = new HashMap<>();
map5.put("key1", "aa");
map5.put("key2", "bb3");
map5.put("key3", "cc");
map5.put("key4", "dd");
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
System.out.println(list);
Map<Object,List<Map<String,Object>>> group = list.stream().collect(Collectors.groupingBy(e -> e.get("key2")));
System.out.println(group);
Map<Object,List<Map<String,Object>>> group2 = list.stream().collect(Collectors.groupingBy(e -> e.get("key2"),LinkedHashMap::new,Collectors.toList()));
System.out.println(group2);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- java8 对集合中某个属性计算出合计值
IntSummaryStatistics sumcc = slist.stream().collect(Collectors.summarizingInt(e->Integer.valueOf(e.get("规模").toString())));
nmap.put("部门", sumcc.getSum());
- java8 数组按照条件过滤
List<Map<String, Object>> finalData1 = data.stream().filter((item)-> finalData1.get(finalI).get(Constant.RULE_TYPE).equals(item.get(Constant.RULE_TYPE)) && !Constant.SYMBOL.equals(item.get(Constant.SEC_TYPE1))).collect(Collectors.toList())