1、LinkedHashSet去重
List<String> list = new ArrayList<>();
list.add("111");
list.add("22");
list.add("111");
list.add("33");
list.add("111");
List<String> newPhoneList = new ArrayList<String>(new LinkedHashSet<>(list));//LinkedHashSet集合去重
2、stream去重
//添加信息 List<PenBean> penBeanList = new ArrayList<PenBean>(); penBeanList.add(new PenBean("铅笔","black")); penBeanList.add(new PenBean("铅笔","white")); penBeanList.add(new PenBean("铅笔","black")); penBeanList.add(new PenBean("中性笔","white")); penBeanList.add(new PenBean("中性笔","white")); //使用java8新特性stream进行List去重 List<PenBean> newPenBeanList = penBeanList.stream().distinct().collect(Collectors.toList());
3、Hashset去重
//添加信息 List<PenBean> penBeanList = new ArrayList<PenBean>(); penBeanList.add(new PenBean("铅笔","black")); penBeanList.add(new PenBean("铅笔","white")); penBeanList.add(new PenBean("铅笔","black")); penBeanList.add(new PenBean("中性笔","white")); penBeanList.add(new PenBean("中性笔","white")); //新数据 List<PenBean> newPenBeanList = new ArrayList<PenBean>(); //set去重 HashSet<PenBean> set = new HashSet<>(penBeanList); newPenBeanList.addAll(set);
标签:penBeanList,PenBean,list,List,add,集合,new From: https://www.cnblogs.com/ht-privete-logs/p/17125393.html