List集合取出重复元素

时间:2025-03-10 10:46:46

###删除List集合中的重复元素

package ;

import ;
import ;
import ;
import ;

/**
 * Created by Administrator on 2018/6/21.
 */
public class DelRepeatObj {
    public static  void  main(String[] agrs){
        List list = new ArrayList();
        ("aaa");
        ("aaa");
        ("bbb");
        ("bbb");
        ("ccc");
        ("eee");
        ("fff");
        ("eee");
//        (getNewList(list));

        getNewListNoRepeat(list);

    }
    /**
     * 去除List集合中的重复元素
     */
    public static Set getNewList(List<String> list){
        //将集合存入set集合中
        Set set = new HashSet();
        (list);

        //jdk1.8+  利用Stream API去重
//        Stream distinct = ().distinct();
//        Object collect = (());
        return  set;
    }

    /**
     * 取出List集合中的重复元素
     */
    public static List<String> getNewListNoRepeat(List<String> list){
        List<String> strings = ()
                .map(e -> {
                    return ();
                }).collect((e -> e, e -> 1, Integer::sum))
                .entrySet().stream()
                .filter(entry -> () > 1)
                .map(::getKey)
                .collect(());
        return  strings;
    }
}