java8 lamba

java lamda 时间操作

   SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDate = new Date(System.currentTimeMillis());
        Calendar calender = Calendar.getInstance();
        calender.setTime(currentDate);
        System.out.println("当天时间:"+currentDate.toLocaleString());
        calender.add(Calendar.DATE, 3);  //加3天
        calender.add(Calendar.SECOND, -60);  //减少60秒
        Date dueTime=null;
        try {
            dueTime = simpleDateFormat.parse(simpleDateFormat.format(calender.getTime()).toString());
        } catch (ParseException e) {

            e.printStackTrace();
        }
        System.out.println("input into endDate:"+dueTime.toLocaleString());

java 8

      LinkedHashMap<String, LogisticsCodeVo> codeMap = analyse.getCodeMap();
        //获取所有数码产品
        List<LogisticsCodeVo> logisticsList = codeMap.values().stream().collect(Collectors.toList());
        //根据产品ID 分装数码
        Map<Long, List<LogisticsCodeVo>> codesByproductIdMap = logisticsList.stream().collect(Collectors.groupingBy(LogisticsCodeVo::getProductId));
        //所有有效数码
        List<String> codes = codeMap.keySet().stream().collect(Collectors.toList());
          Map<Long, ZdInProductParam>   inProductParamMap = productItemList.stream().collect(Collectors.toMap(ZdInProductParam::getProductId, Function.identity()));

HashMap<String, LogisticsCodeVo> codeVoMap = (HashMap<String, LogisticsCodeVo>) logisticsCodeVo.stream().collect(Collectors.toMap(LogisticsCodeVo::getCode, Function.identity()));
        LinkedHashMap<String, LogisticsCodeVo> codeHashMap = new LinkedHashMap<>();
//Map  转LinkedHashMap        
codeVoMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x->codeHashMap.put(x.getKey(),x.getValue()));

String 转 Long

	List<TestVo> listVo = new ArrayList<>();
	List<String> sl = listVo.stream().map(TestVo :: getAge).collect(Collectors.toList());
	sl.forEach(vo -> System.out.println(vo));
	// 此处还有个mapToLong 但不知道杂用
	List<Long> ll = listVo.stream().map(TestVo :: getAge).map(x -> Long.valueOf(x)).collect(Collectors.toList());
	ll.forEach(vo -> System.out.println(vo));
}
@ApiModelProperty(value = "收货日期开始 yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd")
private Date recDateStart;

List过滤

 List<User> collect = list.stream().filter(u -> "北京".equals(u.getAddr())).collect(Collectors.toList());
     List<QdStockSummaryDO> collect = initStack.stream().filter(u ->
                            DO.getChannelId().compareTo(u.getChannelId())==0 && DO.getProductId().compareTo(u.getProductId())==0
                    ).collect(Collectors.toList());
 String collect1 = list.stream().map(u -> u.getName()).collect(Collectors.joining(","));
 List<String> strs=persons.stream().map(person -> person.getName()).collect(Collectors.toList());
//        将Person集合转化为Student集合

        List<Student> students = persons.stream().map(person -> {
            Student student = new Student();
            BeanUtils.copyProperties(person, student);
            if (person.getName() == "张三") {
                student.setSchoolName("三中");
                student.setsId(3L);
            }
            if (person.getName() == "李四") {
                student.setSchoolName("四中");
                student.setsId(4L);
            }
            return student;
        }).collect(Collectors.toList());
        System.out.println("students = " + students);
     #去重
          List<String> myList = 
listAll.stream().distinct().collect(Collectors.toList());
// 对象根据name去重
        List<Person> unique = persons.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new));
        unique.forEach(p -> System.out.println(p));

lambda 统计list中对象的重复次数

List<Integer> list = new ArrayList() {
            {
                add(12);
                add(20);
                add(12);
            }
        };
 
Map<Integer, Long> map = list.stream().collect(Collectors.groupingBy(p -> p,Collectors.counting()));
map.forEach((k, v) -> System.out.println(k + ":" + v));

批量修改List属性的值

  list.stream().map(p ->p.setIsDelete(JcChannelDeleteConstant.DELETED)).collect(Collectors.toList());
   list.forEach(p->p.setIsDelete(1));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值