[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; }

 

posted @ 2016-03-30 10:20  Aviva_ye  阅读(1936)  评论(1编辑  收藏  举报