java stream

List<Map<String, Object>> groupby

List<Map<String, Object>> map = new ArrayList<Map<String, Object>>();
Map<String, List<Map<String, Object>>> glist = map.stream().collect(Collectors.groupingBy(e -> e.get("name").toString()));
List<User> list = new ArrayList<User>();
list = Arrays.asList(
    new User("小强", 11, "男"),
    new User("小玲", 15, "女"),
    new User("小虎", 23, "男"),
    new User("小雨", 26, "女"),
    new User("小飞", 19, "男"),
    new User("小玲", 15, "女")
);


// java8 Stream 流去重操作通过对属性id、name、status进行去重
        ArrayList<RequestDto> result = li.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
                () -> new TreeSet<>(Comparator.comparing(o -> o.getId() + o.getName() + o.getStatus()))), ArrayList::new));
————————————————




//分组
Map<String, List<User>> listMap = list.stream().collect(Collectors.groupingBy(User::getSex));
for(String key:listMap.keySet()){
  System.out.print(key+"组:");
  listMap.get(key).forEach(user -> System.out.print(user.getName()));
  System.out.println();
}
//排序
list.stream().sorted(Comparator.comparing(user-> user.getAge()))
    .forEach(user -> System.out.println(user.getName()));
//过滤
list.stream().filter(user -> user.getSex().equals("男")).collect(Collectors.toList())
    .forEach(user -> System.out.println(user.getName()));
//多条件去重
list.stream().collect(Collectors.collectingAndThen(
    Collectors.toCollection(() -> new TreeSet<>(
        Comparator.comparing(user -> user.getAge() + ";" + user.getName()))), ArrayList::new))
    .forEach(user -> System.out.println(user.getName()));
//最小值
Integer min = list.stream().mapToInt(User::getAge).min().getAsInt();
//最大值
Integer max = list.stream().mapToInt(User::getAge).max().getAsInt();
//平均值
Double average = list.stream().mapToInt(User::getAge).average().getAsDouble();
//和
Integer sum = list.stream().mapToInt(User::getAge).sum();
System.out.println("最小值:"+min+", 最大值"+max+", 平均值:"+average+", 和:"+sum);
//分组求和
Map<String, IntSummaryStatistics> collect = list.stream().collect(Collectors.groupingBy(User::getSex, Collectors.summarizingInt(User::getAge)));
IntSummaryStatistics statistics1 = collect.get("男");
IntSummaryStatistics statistics2 = collect.get("女");
System.out.println(statistics1.getSum());
System.out.println(statistics1.getAverage());
System.out.println(statistics1.getMax());
System.out.println(statistics1.getMin());
System.out.println(statistics1.getCount());
System.out.println(statistics2.getSum());
System.out.println(statistics2.getAverage());
System.out.println(statistics2.getMax());
System.out.println(statistics2.getMin());
System.out.println(statistics2.getCount());
//提取list中两个属性值,转为map
Map<String, String> userMap = list.stream().collect(Collectors.toMap(User::getName, User::getSex));
System.out.println(JsonUtil.toJson(userMap))
//取出所有名字
List<String> names = list.stream().map(User::getName).collect(Collectors.toList());
System.out.println(JsonUtil.toJson(names))

   

获取List中单个对象的集合

    List<Category> categoryList = CategoryService.fiall();

List<Integer> categoryTypeList = categoryList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());

获取List<Map<String, Object>>中单个字段的集合

List<String> list = map.stream().map(e->e.get("name").toString()).collect(Collectors.toList());

list<map<String,Object>>求和

DoubleSummaryStatistics sumcc = map_children.parallelStream().collect(Collectors.summarizingDouble(e -> Double.valueOf(e.get(str).toString())));
mapCount.put(str,sumcc.getSum());

List<Map<String, Object>> 排序

List<Map<String, Object>> collect = list.stream().sorted(Comparator.comparing(Test::comparingByName).thenComparing(Comparator.comparing(Test::comparingByAge).reversed())).collect(Collectors.toList());


   private static String comparingByName(Map<String, Object> map){
        return (String) map.get("name");
    }

    private static Integer comparingByAge(Map<String, Object> map){
        return (Integer) map.get("age");
    }

并行流:parallelStream.串行流:stream

效率:常规for循环>并行流>串行流

list<对象>过滤

List<Student> collect = lists.stream().filter(s -> s.getNumber()==1&& !s.getName().equals("laoli")).collect(Collectors.toList());

list<string>分组求和

Map<String, Map<String,Long>> mapCount = new LinkedHashMap<>();
for (Map.Entry<String, List<IndexOutProjectInfo>> entry : infoGroupMap.entrySet()) {     mapCount.put(entry.getKey(),entry.getValue().stream().collect(Collectors.groupingBy(IndexOutProjectInfo::getNd,Collectors.counting())));
}

list取字段放入list<string>

List<String> enames = list.stream().map(e -> e.getNd()).collect(Collectors.toList());

map升排序:

Map<String, String> sortMap = new LinkedHashMap<>();
year.entrySet().stream().sorted(Map.Entry.<String, String>comparingByValue().reversed()).forEachOrdered(e -> sortMap.put(e.getKey(), e.getValue()));

map降序

Map<String, String> sortMap = new LinkedHashMap<>(); year.entrySet().stream().sorted(Map.Entry.<String, String>comparingByValue()). forEachOrdered(e -> sortMap.put(e.getKey(), e.getValue()));

list按字段去重

List<IndexOutProjectInfo> unique = list.stream().collect(
        Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(IndexOutProjectInfo::getNd))), ArrayList::new)
);

list转map
Map<String,String> map = list.stream().collect(Collectors.groupingBy(IndexOutProjectInfo::getTypeIndex,IndexOutProjectInfo::getTypeName));

如果有重复值,用这种方式

Map<String,String> map = list.stream().collect(Collectors.toMap(IndexOutProjectInfo::getTypeIndex,IndexOutProjectInfo::getTypeName,(entity1,entity2) -> entity1));

list替换字段

List<SysFile> sysFiles = (List<SysFile>) jsonObject.get("list");
sysFiles.stream().filter(e->{e.setVersion(map.get(e.getVersion()));
    return true;
      }).collect(Collectors.toList());

list<对象>排序


List<Student> studentList=Arrays.asList(new Student(1,"ziwen1",10),new Student(2,"aiwen2",18),new Student(3,"biwen3",28));

List<Student> studentList1=studentList.stream().sorted().collect(Collectors.toList());//自然序列

List<Student> studentList2=studentList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());//逆序

List<Student> studentList3=studentList.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());//根据年龄自然顺序

List<Student> studentList4=studentList.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());//根据年龄逆序
 

list求和

BigDecimal:
BigDecimal bb =list.stream().map(Plan::getAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
int、double、long:
double max = list.stream().mapToDouble(User::getHeight).sum();

list按字段分组

Map<String, List<IndexOutProjectInfo>> infoGroupMap = list.stream().collect(Collectors.groupingBy(IndexOutProjectInfo::getTypeIndex));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值