当前位置:   article > 正文

stream操作写法_stream maptolong.cllect

stream maptolong.cllect

stream操作写法
代码从pdf复制过来,可能有问题,可下载附件查看


List<Map<String, Object>> numberData = dataList.stream().map(obj -> 

{ 

Map<String, Object> numberMap = new HashMap<>(); 

numberMap.put("name", obj.get("feeTypeName")); 

numberMap.put("y", obj.get("totalNumber")); 

return numberMap; 

}).collect(Collectors.toList()); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
List<String> merNoList = respList.stream() 

.map(map -> DataTran.ObjectToString(map, "merNo")) 

.collect(Collectors.toList()); 
  • 1
  • 2
  • 3
  • 4
  • 5

stream() .peek

resultList = resultList.stream() 

.peek(obj -> obj.put("y", BossStringUtils.formatAmountFenToYuan(DataTran.ObjectToString(obj, "y")))) 

.collect(Collectors.toList()); 
  • 1
  • 2
  • 3
  • 4
  • 5
List<String> aa= a2mList.stream() 

.map(obj -> String.valueOf(obj.get("MerNo"))) 

.collect(Collectors.toList()); 
  • 1
  • 2
  • 3
  • 4
  • 5
String merNoSqlStr = a2mList 

.stream() 

.map(obj -> DataTran.ObjectToString(obj,"MerNo")) 

.collect(Collectors.joining(",")); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

stream().sorted

mergedDataList = 

mergedDataList.stream().sorted(Comparator.comparing(ChartsOfFee::getName)).collect(Collectors.toList());



//List<String> merNoList 

String s =merNoList 

.stream() 

.map(merNo -> "'" + merNo + "'") 

.collect(Collectors.joining(",")); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

stream().mapToLong().sum()

long refundAmount = list.stream().mapToLong(map -> DataTran.ObjectToLong(map, "refund_amount")).sum(); 
  • 1

-----点评项目的应用----

List<UserDTO> userDTOS = userService.query() 

.in("id", ids).last("ORDER BY FIELD(id," + idStr + ")").list() 

.stream() 

.map(user -> BeanUtil.copyProperties(user, UserDTO.class)) 

.collect(Collectors.toList()); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

stream().map

//Set<String> intersect 

List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList()); 
  • 1
  • 2
  • 3
Stream.of(1,2,3,4,5,6,7,8).skip(5).forEach(System.out::print); 
  • 1

stream().skip().forEach()

List<Long> ids = new ArrayList<>(list.size()); 

Map<String, Distance> distanceMap = new HashMap<>(list.size()); 

list.stream().skip(from).forEach(result -> { 

// 4.2.获取店铺id 

String shopIdStr = result.getContent().getName(); 

ids.add(Long.valueOf(shopIdStr)); 

// 4.3.获取距离 

Distance distance = result.getDistance(); 

distanceMap.put(shopIdStr, distance); 

});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

stream().collect(Collectors.groupingBy

Map<Long, List> map = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));

http://t.csdn.cn/dOHeW

list.stream().Ũ lter(x -> x > 6).forEach(System.out::println);

Optional Ũ ndFirst = list.stream().Ũ lter(x -> x > 6).Ũ ndFirst();

boolean anyMatch = list.stream().anyMatch(x -> x > 6);

List Ũ terList = personList.stream().Ũ lter(x -> x.getSalary() > 8000).map(Person::getName)

.collect(Collectors.toList());

Optional max = list.stream().max(Comparator.comparing(String::length));// 自然排序 Optional max = list.stream().max(Integer::compareTo); // 自定义排序(从大到小排序) Optional

max2 = list.stream().max((o1, o2) -> o2 - o1);

Optional max = personList.stream().max(Comparator.comparingInt(Person::getSalary));

long count = list.stream().Ũ lter(x -> x > 6).count();

List personListNew = personList.stream().map(person -> {

Person personNew = new Person(person.getName(), 0, 0, null, null);

personNew.setSalary(person.getSalary() + 10000);

return personNew;

}).collect(Collectors.toList());

// 改变原来员工集合的方式

List personListNew2 = personList.stream().map(person -> {

person.setSalary(person.getSalary() + 10000);

return person;

}).collect(Collectors.toList());

stringList.stream().mapToInt(String::length).forEach(System.out::println);

double average = doubleList.stream().mapToDouble(Number::doubleValue).average().getAsDouble(); double

sum = doubleList.stream().mapToDouble(Number::doubleValue).sum(); double max =

doubleList.stream().mapToDouble(Number::doubleValue).max().getAsDouble();

分组合并

list.stream.collect(Collectors.toMap(User::getAge,user->user.getName(),(name1,name2)-

>name1+“,”+name2));

newMergedList.stream().sorted(Comparator.comparing(map->DataTran.ObjectToString(map,

“name”))).collect(Collectors.toList());

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/537099
推荐阅读
相关标签
  

闽ICP备14008679号