stream操作写法

该文章展示了如何在Java中使用StreamAPI进行数据操作,包括映射(map),过滤(filter),收集(collect),转换(ObjectToString),排序(sorted),分组(groupingBy)以及计算求和(sum)等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()); 
List<String> merNoList = respList.stream() 

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

.collect(Collectors.toList()); 

stream() .peek

resultList = resultList.stream() 

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

.collect(Collectors.toList()); 
List<String> aa= a2mList.stream() 

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

.collect(Collectors.toList()); 
String merNoSqlStr = a2mList 

.stream() 

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

.collect(Collectors.joining(",")); 

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(",")); 

stream().mapToLong().sum()

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

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

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()); 

stream().map

//Set<String> intersect 

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

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); 

});

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());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值