[Java] Java中List 去掉重复的值,并保持原先List顺序
private List<YourBean> removeDuplicate(List<YourBean> list) { Set<YourBean> set = new HashSet<YourBean>(); List<YourBean> newList = new ArrayList<YourBean>(); for (Iterator<YourBean> iter = list.iterator(); iter.hasNext();) { YourBean element = (YourBean) iter.next();
//利用set集合不会添加重复元素的特性 if (set.add(element)) newList.add(element); } return newList; }