List内容分组去重

本文介绍了一种基于类型和品牌的分组查询优化方法,通过使用集合去重和双重循环遍历,实现了对产品类数据的有效分类和展示。此方法提高了查询效率,避免了重复数据的展示。

分组查询

${temp.proname}${temp.leix}${temp.pingpai}${temp.jiage}

@RequestMapping(“selectCode”)
public String selectCode(Model model){
//set是可以去重的所以放置set中
Set setLeix = new HashSet();
Set setPingpai = new HashSet();
for(int i = 0 ; i<list.size();i++){
setLeix.add(list.get(i).getLeix());
setPingpai.add(list.get(i).getPingpai());
}
//定义一个List用来接收 待会儿传到前台让他重新展示一下
List listAll = new ArrayList<>();
//先按照类型循环
for(String str:setLeix){
List liType = new ArrayList<>();
for(int i = 0;i<list.size();i++){
ProductClass productClass = list.get(i);
if(str.equals(productClass.getLeix())){
liType.add(productClass);
}
}
//根据对应的类型进行品牌分组
for (String from:setPingpai){
for (int i = 0; i <liType.size(); i++) {
ProductClass pl = liType.get(i);
if (from.equals(pl.getPingpai())){
listAll.add(pl);
}
}
}
}

model.addAttribute(“productlist”,listAll);
return “productlist”;
}

### 使用 Java Stream API 对 List 进行分组 对于给定的 `List`,如果目标是对对象基于特定字段进行分组以及,则可以通过组合使用 `Collectors.groupingBy()` 和 `Collectors.collectingAndThen()` 或者利用自定义比较逻辑配合 `distinct()` 来完成此目的。 #### 基于单个属性并收集到新列表 假设存在一个类 `Person`,其具有 `name` 属性和其他可能不同的属性。为了仅保留名字唯一的人: ```java import java.util.*; import java.util.stream.Collectors; class Person { private String name; // other fields... public Person(String name) { this.name = name; } public String getName() { return name; } @Override public boolean equals(Object o){ if (this == o) return true; if (!(o instanceof Person)) return false; Person person = (Person) o; return Objects.equals(name, person.name); } @Override public int hashCode(){ return Objects.hash(name); } } public class Main{ public static void main(String[] args){ List<Person> people = Arrays.asList( new Person("Alice"), new Person("Bob"), new Person("Alice") // duplicate entry ); List<Person> uniquePeople = people.stream() .distinct() // removes duplicates based on the natural key defined by equals/hashCode .collect(Collectors.toList()); System.out.println(uniquePeople.size()); // prints "2" } } ``` 这段代码展示了如何通过覆盖 `equals` 和 `hashCode` 方法来确保根据名称判断两个 `Person` 实例是否相同[^1]。 #### 按照某个条件分组后再 当需要按照某些标准对元素进行分类再从中挑选代表时,可以采用如下方式: ```java Map<String, Optional<Person>> groupedByName = people.stream() .collect(Collectors.groupingBy(Person::getName, Collectors.reducing((p1, p2) -> p1))); // keeps first occurrence of each group // Extract values from map and flatten into a list List<Person> representatives = groupedByName.values().stream() .map(Optional::get) .collect(Collectors.toList()); ``` 这里采用了 `groupingBy` 结合 `reducing` 的策略,在每组内部只留下第一个遇到的对象作为该组的代表[^3]。 #### 多字段联合判定复项 如果有更复杂的业务场景,比如要依据多个字段共同决定两条记录是不是应该视为同一个实体的话,可以在流中间操作阶段加入过滤器或者创建复合键来进行区分: ```java Set<CompositeKey> seenKeys = new HashSet<>(); people.stream() .filter(p -> seenKeys.add(new CompositeKey(p.getName(), /*other properties*/))) .collect(Collectors.toList()); ``` 其中 `CompositeKey` 是一个实现了 `equals` 和 `hashCode` 接口的新类,用来表示由若干成员变量组成的唯一标识符。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值