测试实体(数字对象使用 MDouble):
import com.mchweb.common.lang.MDouble;
import lombok.*;
@Getter
@Setter
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class User {
private MDouble money;
}
import com.mchweb.common.lang.MDouble;
import org.apache.commons.compress.utils.Lists;
import java.util.List;
public class TestTime {
public static void main(String[] args) {
// 创建 一组 User对象
List<User> list = Lists.newArrayList();
list.add(User.builder().money(new MDouble(11D)).build());
list.add(User.builder().money(new MDouble(22D)).build());
list.add(User.builder().money(new MDouble(133D)).build());
list.add(User.builder().money(new MDouble(44D)).build());
// ★★★ 求和方法 ★★★
MDouble dd = list.stream().map(User::getMoney).reduce(MDouble.ZERO, MDouble::add);
System.out.println(dd);
}
}
测试实体(数字对象使用 BigDecimal):
import lombok.*;
import java.math.BigDecimal;
@Getter
@Setter
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class User {
private BigDecimal money;
}
import org.apache.commons.compress.utils.Lists;
import java.math.BigDecimal;
import java.util.List;
public class TestTime {
public static void main(String[] args) {
// 创建 一组 User对象
List<User> list = Lists.newArrayList();
list.add(User.builder().money(new BigDecimal(12D)).build());
list.add(User.builder().money(new BigDecimal(22D)).build());
list.add(User.builder().money(new BigDecimal(133D)).build());
list.add(User.builder().money(new BigDecimal(44D)).build());
// ★★★ 求和方法 ★★★
BigDecimal dd = list.stream().map(User::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println(dd);
}
}
标签:Map,BigDecimal,Stream,money,list,add,User,MDouble
From: https://blog.51cto.com/u_16111399/6468991