springboot里面List<Parttopart> parttopartList里面有二十万条数据,如何去掉其中supid和subid重复的数据
时间: 2024-11-12 14:40:16 浏览: 21
在Spring Boot中,如果你有一个包含大量数据(如二十万条)的`List<PartToPart>`列表,并且想要去除其中`supId`和`subId`相同的元素,可以考虑使用数据结构的特性或者一些高效的算法来处理。这里提供一种常见的解决方案:
1. 使用Set集合:由于Set不允许有重复元素,你可以先将List转换为Set,这会自动移除重复项。然后再将Set转换回List。
```java
Set<PartToPart> uniqueParts = new HashSet<>(parttopartList);
List<PartToPart> deduplicatedList = new ArrayList<>(uniqueParts);
```
2. 如果需要保持原始顺序,可以使用`LinkedHashSet`,它维护了插入顺序。
```java
List<PartToPart> deduplicatedList = new ArrayList<>(new LinkedHashSet<>(parttopartList));
```
3. 这种操作通常在内存足够的情况下处理大列表是可行的。如果数据量真的非常大,可能会导致内存溢出,这时应该采用分批处理的方式,比如使用Java 8的Stream API结合`distinct()`方法。
```java
List<PartToPart> distinctParts = parttopartList.stream()
.collect(Collectors.distinctBy(PartToPart::getSupIdAndSubId))
.collect(Collectors.toList());
```
在这个例子中,假设`getSupIdAndSubId()`是一个方法,能够获取`PartToPart`的唯一标识。
阅读全文
相关推荐


















