List累计属性的和
时间: 2025-03-14 10:11:36 浏览: 37
### 计算列表内元素某个属性的累加和
在 Java 编程中,可以通过流式处理(Stream API)来实现对 `List` 中对象某一特定属性的累加操作。以下是基于所提供的引用内容以及相关技术背景的具体解决方案。
#### 方法一:使用 Stream API 和 Lambda 表达式
可以利用 `Stream` 的 `mapToLong` 或者 `mapToInt` 方法提取目标属性值并进行累加。以下是一个完整的代码示例:
```java
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 定义一个简单的实体类
class Item {
private int value;
public Item(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
// 创建一个包含多个对象的 List
List<Item> itemList = Arrays.asList(new Item(1), new Item(2), new Item(3));
// 使用 Stream API 对指定属性进行累加
long sum = itemList.stream()
.mapToInt(Item::getValue) // 提取每个对象的 value 属性
.sum(); // 进行累加运算
System.out.println("总和为:" + sum);
}
}
```
上述代码展示了如何通过 `Stream` 接口提取 `Item` 类型对象的 `value` 属性,并对其进行累加[^1]。
---
#### 方法二:针对复杂数据结构的合并与累加
如果需要对更复杂的嵌套数据结构(如引用[3]中的例子),则可以采用 `Collectors.toMap` 来完成去重与属性累加的操作。下面是对该场景的一个改进版本:
```java
import java.util.*;
import java.util.stream.Collectors;
class Way {
String id;
public Way(String id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Way way = (Way) o;
return Objects.equals(id, way.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
class A {
private Long total;
private List<Way> ways;
public A(Long total, List<Way> ways) {
this.total = total;
this.ways = ways;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<Way> getWays() {
return ways;
}
@Override
public String toString() {
return "A{" +
"total=" + total +
", ways=" + ways +
'}';
}
}
public class ComplexSumExample {
public static void main(String[] args) {
List<A> aList = Arrays.asList(
new A(10L, Arrays.asList(new Way("1"))),
new A(5L, Arrays.asList(new Way("1"))),
new A(3L, Arrays.asList(new Way("2")))
);
List<A> result = aList.stream()
.collect(Collectors.groupingBy(A::getWays,
Collectors.reducing(
new A(0L, Collections.emptyList()),
(a1, a2) -> {
a1.setTotal(a1.getTotal() + a2.getTotal());
return a1;
})
))
.values().stream()
.flatMap(List::stream)
.toList();
result.forEach(System.out::println);
}
}
```
此代码片段实现了将具有相同 `ways` 字段的对象按照其 `total` 值进行累加的功能[^3]。
---
#### 方法三:线程安全环境下的原子化累加
当涉及到多线程环境下对共享资源的安全访问时,可以考虑使用 `AtomicInteger` 或 `AtomicLong` 等工具类来进行无锁化的高效累加操作。例如:
```java
import java.util.concurrent.atomic.AtomicLong;
public class ThreadSafeCounter {
private final AtomicLong counter = new AtomicLong(0);
public void increment(long amount) {
while (true) {
long currentValue = counter.get();
long newValue = currentValue + amount;
if (counter.compareAndSet(currentValue, newValue)) {
break;
}
}
}
public long getCounterValue() {
return counter.get();
}
public static void main(String[] args) throws InterruptedException {
ThreadSafeCounter counterInstance = new ThreadSafeCounter();
Runnable task = () -> {
for (int i = 0; i < 1000; ++i) {
counterInstance.increment(i % 7); // 随机增加不同数值
}
};
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("最终计数器值为:" + counterInstance.getCounterValue());
}
}
```
这里采用了 CAS 技术确保即使在高并发情况下也能正确执行累加逻辑[^4]。
---
### 总结
以上分别介绍了三种不同的方式用于解决不同类型的数据集合上的属性累加问题——简单列表、复合对象列表以及多线程条件下的同步累加。
阅读全文
相关推荐



















