java8学习笔记

基本用法

创建测试实体,测试类

创建测试对象DemoUser:

public class DemoUser {
   private String name;
   private String pwd;
   private Integer age;
   private Date birthDay;
   //getter/setter...
}

main方法中创建数组,添加数组元素(略),获得stream

List<DemoUser> list = new ArrayList();
//...添加DemoUser对象

//转化为流stream()
Stream<DemoUser> stream = list.stream();

1、根据字段分组

a、根据pwd分组

//根据pwd分组,返回一个map,collect传入函数Collectors.groupingBy()
Map<String, List<DemoUser>> map= stream.collect(Collectors.groupingBy(e -> { return e.getPwd();}));

b、根据自定义的key分组

Map<String, List<DemoUser>> collect = stream.collect(Collectors.groupingBy(e -> **fetchGroupKey**(e)));
//定义key规则
String fetchGroupKey(DemoUser demoUser){
        return demoUser.getName()+demoUser.getAge();
}

c、先根据age分组,再根据name分组

Map<Integer, Map<String, List<DemoUser>>> collect = stream.collect(Collectors.groupingBy(e -> e.getAge(), Collectors.groupingBy(e -> e.getName())));
        

d、根据age分组,再统计每组数据总数

Map<Integer, Long> collect = stream.collect(Collectors.groupingBy(e -> e.getAge(), Collectors.counting()));

e、根据名称分组,再根据age求和

 Map<String, IntSummaryStatistics> collect = stream.collect(Collectors.groupingBy(e -> e.getName(), Collectors.summarizingInt(e -> e.getAge())));
        System.out.println();

2、根据字段排序

a、根据age从小到大排序

//使用
Collections.sort(list,(e1,e2)->e1.getAge()- e2.getAge());
//或者
list.sort(Comparator.comparing(DemoUser::getAge));

b、多条件组合排序:先根据age,再根据name和pwd排序

list.sort(Comparator.comparing(DemoUser::getAge).thenComparing(DemoUser::getName).thenComparing(DemoUser::getPwd));

c、根据汉字排序

Collections.sort(list, (e1, e2) -> Collator.getInstance(Locale.CHINESE).compare(e1.getName(), e2.getName()));

3、根据字段去重

a、根据名称去重

// 根据name去重
ArrayList<DemoUser> collect = stream.collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(DemoUser::getName))), ArrayList::new)
);
 

b、多条件去重

// 根据name,age两个属性去重
List<DemoUser> unique = stream.collect(
    Collectors. collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getAge()))), ArrayList::new)
);

c、利用过滤去重

List<DemoUser> collect = stream.
	filter(distinctByKey(e -> e.getName())).
		collect(Collectors.toList());
//distinctByKey
static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object,Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

4、根据条件过滤filter

a、过滤获取name为liming,并且age大于12岁的user

List<DemoUser> collect = stream.
	filter(e -> "liming".equals(e.getName()) && e.getAge() > 12).
		collect(Collectors.toList());

5、stream().map归并

//a 将List<DemoUser>通过map重新构造并返回List<DemoUserVo>
List<DemoUserVo> collectVo = stream.map(e -> {
            DemoUserVo demoUserVo = new DemoUserVo();
            demoUserVo.setName(e.getName());
            demoUserVo.setAge(e.getAge());
            return demoUserVo;
}).collect(Collectors.toList());    

对于stream.map(),入参为:Function<? super T, ? extends R> mapper,而public interface Function<T, R>接口,参数T和R表示:

@param <T> the type of the input to the function  表示函数的输入
@param <R> the type of the result of the function 表示函数结果

则上面的代码也可以改写为:

//构建DemoUserVo
public static Function<DemoUser,DemoUserVo> buildDemoUserVo(){
        return e ->{
            DemoUserVo demoUserVo = new DemoUserVo();
            demoUserVo.setName(e.getName());
            demoUserVo.setAge(e.getAge());
            return demoUserVo;
        };
    }
//使用buildDemoUserVo
List<DemoUserVo> collect = stream.map(buildDemoUserVo()).collect(Collectors.toList());
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值