List<T>中去除重复的元素
2016-09-09 09:33
507 查看
pojo类
工具类
操作类
如果是对象,需要实现hashCode()和equals()方法。
package com.easzz.model.entity; import java.util.List; public class JsonData { private Integer id; //省级id private Integer pid; //父id private String text; //名称 @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((children == null) ? 0 : children.hashCode()); result = prime * result + ((disable == null) ? 0 : disable.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((pid == null) ? 0 : pid.hashCode()); result = prime * result + ((text == null) ? 0 : text.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; JsonData other = (JsonData) obj; if (children == null) { if (other.children != null) return false; } else if (!children.equals(other.children)) return false; if (disable == null) { if (other.disable != null) return false; } else if (!disable.equals(other.disable)) return false; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; if (pid == null) { if (other.pid != null) return false; } else if (!pid.equals(other.pid)) return false; if (text == null) { if (other.text != null) return false; } else if (!text.equals(other.text)) return false; return true; } private Integer disable; private List<JsonData> children; public Integer getDisable() { return disable; } public void setDisable(Integer disable) { this.disable = disable; } public List<JsonData> getChildren() { return children; } public void setChildren(List<JsonData> children) { this.children = children; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getText() { return text; } public void setText(String text) { this.text = text == null ? null : text.trim(); } }
工具类
public static List<JsonData> removeDuplicate(List<JsonData> list) { HashSet<JsonData> h = new HashSet<JsonData>(list); list.clear(); list.addAll(h); return list; }
操作类
//获取父级数据 List<JsonData> pDatas=new ArrayList<JsonData>(); for (JsonData jsonData : findByRemovedPid) { JsonData pdata = jsonDataService.findByPid(jsonData.getPid()); pDatas.add(pdata); } List<JsonData> removeDuplicate = removeDuplicate(pDatas); for(int i=0;i<removeDuplicate.size();i++){ System.out.println(removeDuplicate.get(i).getText()); }
如果是对象,需要实现hashCode()和equals()方法。
相关文章推荐
- FCL应用技巧之------使用List<T>的Distinct()去除重复元素
- 去除List<Object>集合中重复的元素
- 去除List<Object>集合中重复的元素(利用HashSet的特性---无重复元素)
- 去除List<String>中的重复元素
- Linq List<T>.Distinct() 去除对象重复
- 根据list<object>对象中某个字段去除重复值
- List<T>去除重复对象
- List<string>里 每个元素重复了多少次
- 去除List<Map<String,Object>>中的map的重复值(精简版)
- 使用HashSet<>去除重复元素的集合
- 去除List<Map<String,Object>>中的map中的重复值
- 去除List列表中重复值(稍作调整,也适合于List<T> 和 List<?>)
- c#中如何去除List<String>中的重复项
- 合并2个list<string>,并且去除掉重复项目
- 自我整理:去除List<String[]>中String[x]中重复的记录
- 使用HashSet<>去除重复元素的集合
- leetcode:Remove Duplicates from Sorted List(去除链表中重复元素)【面试算法题】
- //编写程序判断一个vector<int>容器所包含的元素是否与一个list<int>容器的完全相同
- 去掉重复 List<T>.Distinct()
- STL-去除list中重复的元素