温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

List集合中怎么实现复杂字段判断去重

发布时间:2021-06-15 14:51:49 来源:亿速云 阅读:599 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关List集合中怎么实现复杂字段判断去重,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

List去重复,我们首先想到的可能是 利用ListSet集合,因为Set集合不允许重复。所以达到这个目的。 如果集合里面是简单对象,例如IntegerString等等,这种可以使用这样的方式去重复。但是如果是复杂对象,即我们自己封装的对象。用List转Set 却达不到去重复的目的。 所以,回归根本。 判断Object对象是否一样,我们用的是其equals方法。 所以我们只需要重写equals方法,就可以达到判断对象是否重复的目的。

话不多说,上代码:

package com.test; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.collections.CollectionUtils; public class TestCollection {  //去重复之前集合  private static List<User> list = Arrays.asList(   new User("张三", BigDecimal.valueOf(35.6), 18),   new User("李四", BigDecimal.valueOf(85), 30),   new User("赵六", BigDecimal.valueOf(66.55), 25),   new User("赵六", BigDecimal.valueOf(66.55), 25),   new User("张三", BigDecimal.valueOf(35.6), 18));  public static void main(String[] args) {  //排除重复  getNoRepeatList(list);    }  /**  * 去除List内复杂字段重复对象  * @param oldList  * @return  */  private static List<User> getNoRepeatList(List<User> oldList){  List<User> list = new ArrayList<>();  if(CollectionUtils.isNotEmpty(oldList)){   for (User user : oldList) {   //list去重复,内部重写equals   if(!list.contains(user)){    list.add(user);   }   }  }  //输出新集合  System.out.println("去除重复后新集合:");  if(CollectionUtils.isNotEmpty(list)){   for (User user : list) {   System.out.println(user.toString());   }  }  return list;   }   static class User{  private String userName; //姓名  private BigDecimal score;//分数  private Integer age;  public String getUserName() {   return userName;  }  public void setUserName(String userName) {   this.userName = userName;  }  public BigDecimal getScore() {   return score;  }  public void setScore(BigDecimal score) {   this.score = score;  }  public Integer getAge() {   return age;  }  public void setAge(Integer age) {   this.age = age;  }  public User(String userName, BigDecimal score, Integer age) {   super();   this.userName = userName;   this.score = score;   this.age = age;  }  public User() {   // TODO Auto-generated constructor stub  }  @Override  public String toString() {   // TODO Auto-generated method stub   return "姓名:"+ this.userName + ",年龄:" + this.age + ",分数:" + this.score;  }  /**   * 重写equals,用于比较对象属性是否包含   */  public boolean equals(Object obj) {       if (obj == null) {         return false;       }       if (this == obj) {         return true;       }       User user = (User) obj;       //多重逻辑处理,去除年龄、姓名相同的记录      if (this.getAge() .compareTo(user.getAge())==0       && this.getUserName().equals(user.getUserName())       && this.getScore().compareTo(user.getScore())==0) {         return true;       }       return false;     }   } }

执行结果:

去除重复后新集合:
姓名:张三,年龄:18,分数:35.6
姓名:李四,年龄:30,分数:85
姓名:赵六,年龄:25,分数:66.55

看完上述内容,你们对List集合中怎么实现复杂字段判断去重有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI