1.求和
items.stream().mapToLong(object::get).sum()
2.字符集拆分为id
package cn.fw.finance.common.util; import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import static cn.fw.common.constant.CommonConstant.Symbol.COMMA; public class StringUtils extends org.apache.commons.lang3.StringUtils { /** */ public static Set<Long> convert2IdSet(String data) { return isBlank(data) ? new HashSet<>() : Stream.of(data.split(",")).map(Long::valueOf).collect(Collectors.toSet()); } /** * 按逗号分割 */ public static Set<String> splitCommaToSet(String data) { return isBlank(data) ? new HashSet<>() : Stream.of(data.split(",")).collect(Collectors.toSet()); } /** * id集合拼接为字符串 * * @param data * @return */ public static <T> String convert2CommaString(Collection<T> data) { return data.stream().map(String::valueOf).collect(Collectors.joining(",")); } }
3.分成集合
.stream()
.collect(Collectors.groupingBy(object::get, Collectors.mapping(object::get, Collectors.toList())));