感想:之前没怎么用过JAVA8的一些特性,都不太清除有什么有点,后面发现,略微研究用其他真的是不要太爽。值得一用,这很SKR!!!
1. Lambda表达式和函数式接口
Arrays.asList("1","2","3").forEach(e->System.out.println(e));
Arrays.asList("1","2","3").forEach( String e -> System.out.println( e ));
2. 方法引用
直接对类的遍历 然后加对象属性即可获得;
cars.forEach( Car::collide );
3. Steam 流式风格最爽 可能本人用的比较多的就是这个
首先之前在项目中经常需要查询一些对象中的属性,然后又要对某一列属性进行批量查询,然后在查询完之后进行对象组装,这个时候用上steam则大大提高了代码的效率,获益匪浅!
举例:
//首先从接口调回清关的SKU
Map<String, Object> paramMaps = new HashMap<String, Object>();
Date nowDate=new Date();
String nowDateStr=DateUtil.dateFormat(nowDate,"yyyy-MM-dd");
paramMaps.put("startTime",nowDateStr+" 00:00:00");
paramMaps.put("endTime", nowDateStr+" 23:59:59");
//String url="http://192.168.29.13:8038/api/capture?startTime=2018-05-01&endTime=2018-07-01&pageNumber=1&limit=100";
//然后根据清关返回的sku去匹配清关账号和清关类型 匹配是哪种站点
AjaxResponse res = HttpUtil.doGet(URLBuilder.generate(paramMaps, skuManageUrl));
AjaxResponse skuResult=JSON.parseObject(res.getData().toString(), new TypeReference<AjaxResponse>() {}.getType());
List<SkuManage> skuManageList = JSON.parseObject(skuResult.getData().toString(), new TypeReference<List<SkuManage>>() {}.getType());
List<String> skuCodeList = skuManageList.stream().map(SkuManage::getSkuCode).collect(Collectors.toList());
Map<String,String> pMap=new HashMap<String,String>();
//将接口返回的数据进行SKU清关分组分割(去重清关前缀相同的)
for (String string : skuCodeList) {
pMap.put(string.split("-")[0], string);
}
List<String> skuPreList=new ArrayList<String>();
for(Map.Entry<String, String> entry:pMap.entrySet()) {
skuPreList.add(entry.getKey());
}
该段代码需要先对接口返回的数据集合做解析,然后对集合的某一列去单独拿出来,然后对某一列属性用Map 进行存储去重。这个时候就很适合流式写法用。按常规思想是直接循环取出,然后再遍历去重,这样对于大量数据处理而言,无疑会很慢,所以这样就巧妙就运用起来了。下面我给几个基本的写法来试用流式风格写法:
//List分组特性
Map<Long,Map<Long,List<RStudentExam>>> map = list.stream().collect(Collectors.groupingBy(RStudentExam::getId,Collectors.groupingBy(RStudentExam::getSchoolId)));
//将查询出来的data 进行组装成事业部导出列的实体
List<PurchaseOrderList> purchaseOrderLists=data.getRows();
Map<String, Map<String, List<PurchaseOrderList>>> orgMap = purchaseOrderLists.stream().collect(Collectors.groupingBy(PurchaseOrderList::getOrgId,Collectors.groupingBy(PurchaseOrderList::getPurchaseOrderId)));
List<PurchaseOrderOrgMoney> orderOrgMoneyList=new ArrayList<>();
orgMap.forEach((key,value)->{
value.forEach((key2,value2)->{
PurchaseOrderOrgMoney orderOrgMoney=new PurchaseOrderOrgMoney();
orderOrgMoney.setOrgId(key);
orderOrgMoney.setPurchaseOrderId(key2);
orderOrgMoneyList.add(orderOrgMoney);
});
});
灵活运用steam根据有些列属性进行去重
List<Person> persons = new ArrayList();
List<Integer> ids = new ArrayList<>();//用来临时存储person的id
persons.add(new Person(1, "name1", 10));
persons.add(new Person(2, "name2", 21));
persons.add(new Person(5, "name5", 55));
persons.add(new Person(3, "name3", 34));
persons.add(new Person(1, "name1", 10));
List<Person> personList = persons.stream().filter(// 过滤去重
v -> {
boolean flag = !ids.contains(v.getId());
ids.add(v.getId());
return flag;
}
).collect(Collectors.toList());
System.out.println(personList);
先记录下,后续完善,然后在工作中用到则补充上,我是根据实际用到来说的,当然强大的东西肯定不止这些,后面我再进行完善!