去除List集合中的重复对象,Map遍历代码

时间:2021-11-30 18:58:07
/***
* 去除List<PartsInfoDTO>列表中的重复对象 ~!!
* @param list
* @return
*/
public static List<PartsInfoDTO> removeDuplicate(List<PartsInfoDTO> list) {
// Set<PartsInfoDTO> set = new HashSet<PartsInfoDTO>();
List<PartsInfoDTO> newList = new ArrayList<PartsInfoDTO>(); Map map = new HashMap();
for (Iterator<PartsInfoDTO> iter = list.iterator(); iter.hasNext();) {
PartsInfoDTO element = (PartsInfoDTO) iter.next();
map.put(element.getId(), element); //如果id重复会覆盖.
} Iterator it = map.keySet().iterator();
while (it.hasNext()) {
Integer key = (Integer) it.next();
newList.add((PartsInfoDTO) map.get(key));
} return newList;
}