方法一:使用Set

List<User> newList = new ArrayList<User>();
Set<String> set = new HashSet<String>();
for (User user : list) {
String userName = user.getName();
if (!set.contains(userName)) { //set中不包含重复的
set.add(userName);
newList.add(user);
}
} System.out.println("Set去重后的集合: " + newList);

方法二:使用Map

List<User> newList = new ArrayList<User>();

HashMap<String, String> map = new HashMap<String, String>();
for (User user : list) {
String userName = user.getName();
String value = map.get(userName);
if (StringUtils.isBlank(value)) { //如果value是空的 说明取到的这个name是第一次取到
map.put(userName, userName);
newList.add(user); //newList就是我们想要的去重之后的结果
}
} System.out.println("Map去重后的集合: " + newList);

方法三:使用List(可以根据多个字段)

List<String> newList = new ArrayList<String>();
List<User> userList = new ArrayList<User>();
for (User cd : list) {
if (!newList.contains("" + cd.getAge())) { // 多个字段的话"" + cd.getAge() + cd.getSex()
newList.add("" + cd.getAge()); // 多个字段的话"" + cd.getAge() + cd.getSex()
userList.add(cd);
}
} System.out.println("List去重后的集合: " + userList);

附:测试User类

/**
* list根据某个字段去重
*
* @author xc
* @version V1.0 2017年9月30日
*/
public class list根据某个字段去重 { private static List<User> list; static {
User user1 = new User("zhangsan1", 20, true);
User user2 = new User("zhangsan2", 20, true);
User user3 = new User("zhangsan1", 30, true);
list = new ArrayList<User>();
list.add(user1);
list.add(user2);
list.add(user3);
} public static void main(String[] args) {
duplicateMap();
duplicateSet();
duplicateList();
} /**
* 使用List
*/
private static void duplicateList() {
List<String> newList = new ArrayList<String>();
List<User> userList = new ArrayList<User>();
for (User cd : list) {
if (!newList.contains("" + cd.getAge() + cd.getSex())) {
newList.add("" + cd.getAge() + cd.getSex());
userList.add(cd);
}
} System.out.println("List去重后的集合: " + userList);
} /**
* 使用set
*/
private static void duplicateSet() {
List<User> newList = new ArrayList<User>();
Set<String> set = new HashSet<String>();
for (User user : list) {
if (user == null) {
continue;
}
String userName = user.getName();
if (userName != null) {
if (!set.contains(userName)) { //set中不包含重复的
set.add(userName);
newList.add(user);
} else {
continue;
}
}
} System.out.println("Set去重后的集合: " + newList); } /**
* 使用map
*/
private static void duplicateMap() {
List<User> newList = new ArrayList<User>(); HashMap<String, String> map = new HashMap<String, String>();
for (User user : list) {
if (user == null) {
continue;
}
String name = user.getName();
if (name != null) {
String value = map.get(name);
if (StringUtils.isBlank(value)) { //如果value是空的 说明取到的这个name是第一次取到
map.put(name, name);
newList.add(user); //newList就是我们想要的去重之后的结果
} else {
continue;
}
}
} System.out.println("Map去重后的集合: " + newList); } }

list根据某个字段去重的更多相关文章

  1. list集合中指定字段去重

    在开发中,有时会需要指定字段去重,以下为实现方法: 假设有个房地产权的类,其中宗地代码ZDDM值重复,而我们在前端页面显示时,只需要一条数据,因为公共字段都一样: IEqualityComparer需 ...

  2. MySQL 查询重复的数据,以及部分字段去重和完全去重

    1.查找表中多余的重复记录(多个字段) select * from vitae a where (a.peopleId,a.seq) in  (select peopleId,seq from vit ...

  3. sql针对某一字段去重,并且保留其他字段

    今天客户提了一个小需求,希望我能提供一条sql语句,帮助他对数据中 _field 这个字段的值去重,并且保留其他字段的数据.第一反应是select distinct,但这种语句在对某个字段去重时,无法 ...

  4. php二维数组根据某个字段去重

    php的二维数组根据某个字段去重,在这默认为二维数组的结构是一样的,现在根据二维数组里的id字段去重,把id相同的重复的元素去掉 /** * 二维数组根据某个字段去重 * @param array $ ...

  5. List集合中的对象按照某个字段去重实现

    package com.liying.banana.user; import java.util.ArrayList; import java.util.Comparator; import java ...

  6. DataTable根据字段去重

    DataTable根据字段去重 最近需要对datatable根据字段去重,在网上搜了很多,找到了一个方法,代码如下 public static DataTable DistinctSomeColumn ...

  7. oracle 多字段去重查询

      oracle 多字段去重查询 CreationTime--2018年6月29日15点11分 Author:Marydon 1.情景展示 需要对表BASE_MRI_DEVICE的COMPNAME.F ...

  8. C#NPOI.RabbitMQ.EF.Attribute.HttpRuntime.Cache.AD域.List<T>根据指定字段去重.前端JQuery.Cache.I18N(多语言).data-xx(自定义属性)

    使用NPOI 操作Excel 个人使用的电脑基本默认安装Excel 操作起来 调用Excel的组件便可.如果是一台服务器.没有安装Excel,也就无法调用Excel组件. 在此推荐第三方插件.NPOI ...

  9. 数据库表设计时一对一关系存在的必要性 数据库一对一、一对多、多对多设计 面试逻辑题3.31 sql server 查询某个表被哪些存储过程调用 DataTable根据字段去重 .Net Core Cors中间件解析 分析MySQL中哪些情况下数据库索引会失效

    数据库表设计时一对一关系存在的必要性 2017年07月24日 10:01:07 阅读数:694 在表设计过程中,我无意中觉得一对一关系觉得好没道理,直接放到一张表中不就可以了吗?真是说,网上信息什么都 ...

随机推荐

  1. vue的地图插件amap

    https://www.jianshu.com/p/0011996b81e2(amap) npm install vue-amap --save

  2. C# Note28: Dispatcher类

    在项目中也是经常用到: 刚见到它时,你会想:为什么不直接使用System.Windows命名空间下的MessageBox类,何必要这么麻烦?(认真分析看它做了什么,具体原因下面解释) 主要介绍的方法: ...

  3. css3的clip-path方法剪裁实现

    本例讲解如何通过clip-path把一个div(元素,可以是图片等)裁切成不同的形状,这里以一个div为例宽高均为300px 注意:不支持IE和Firefox,支持webkit浏览器,在现代浏览器中需 ...

  4. ERROR org.hibernate.internal.SessionImpl - HHH000346: Error during managed flush [object references an unsaved transient instance - save the transient instance before flushing: cn.itcast.domain.Custom

    本片博文整理关于Hibernate中级联策略cascade和它导致的异常: Exception in thread "main" org.hibernate.TransientOb ...

  5. jaxp的dom方式操作(查找、添加、修改、删除、遍历节点)

    package cn.itcast.jaxptest; import java.io.IOException; import javax.xml.parsers.DocumentBuilder;imp ...

  6. pixel和nexus设备安卓9.0/8.1/7.1.x/6.x WiFi和信号图标出现叉x号或者感叹号的消除办 法

    在安卓9.0/8.1/8.0/7.1.2里如何消除x号(在老一点点版本是感叹号)呢? 1.首先开启usb调试,然后用数据线连接电脑和手机. 2.然后解决好您的adb驱动问题,具体教程见:http:// ...

  7. rmse均方根误差

    rmse=sqrt(sum((w-r).^2)/length(w))

  8. xx.hbm.xml中相关重要的配置

    1.<!-- 指定hibernate操作数据库时的隔离级别             #hibernate.connection.isolation 1|2|4|8                 ...

  9. codeforces484A

    Bits CodeForces - 484A Let's denote as  the number of bits set ('1' bits) in the binary representati ...

  10. Auth模块使用方法大全

    auth认证 导包 from django.contrib import auth 默认数据库中使用auth_user表 创建超级用户 python manage.py createsuperuser ...