示例一:对于简单类型,比如String
public static void main(String[] args) {List<String> list = new ArrayList< >();list.add("aaa");list.add("bbb");list.add("bbb");list.add("ccc");list.add("abc");list.add("abc");list.add("abd");list.add("cba");list.add("cba");for (String s : list) {System.out.print(s+"\t");}System.out.println("\n-----------------");//方法一:利用Set去重Set<String> set = new HashSet< >();set.addAll(list);for (String s : set) {System.out.print(s+"\t");}System.out.println("\n-----------------");//方法二:利用TreeSet集合特性排序+去重:TreeSet可以将字符串类型的数据按照字典顺序进行排序,首字母相同则看第二位List<String> list2 = new ArrayList<>(new TreeSet<>(list));for (String s : list2) {System.out.print(s+"\t");}System.out.println("\n-----------------");//方法三:LinkedHashSet虽然可以去重,但是根据他的特性,他不能对数据进行排序,只能维持原来插入时的秩序List<String> list3 = new ArrayList<>(new LinkedHashSet<>(list));for (String s : list3) {System.out.print(s+"\t");}System.out.println("\n-----------------");//方法四:用list.contains()的方法进行判断,然后将其添加到新的list当中,元素的顺序不发生改变List<String> list4 = new ArrayList<>();for (String item : list) {if (!list4.contains(item)) {list4.add(item);}}for (String s : list4) {System.out.print(s+"\t");}System.out.println("\n-----------------");//方法五:使用Java8特性去重:把list集合->Stream流,然后对流用distinct()去重,再用collect()收集List<String> list5 = list.stream().distinct().collect(Collectors.toList());for (String s : list5) {System.out.print(s+"\t");}System.out.println("\n-----------------");//方法六:使用list自身方法remove():将同一个list用两层for循环配合.equals()方法,有相同的就用remove()方法剔除掉,然后得到一个没有重复数据的listfor (int i = 0; i < list.size()-1; i++) {for (int j = list.size()-1; j >i; j--) {if(list.get(i).equals(list.get(j))){list.remove(j);}}}for (String s : list) {System.out.print(s+"\t");}}
结果:
示例二:对于复杂类型,根据指定属性去重
测试代码:
public static void main(String[] args) {List<Dept> depts = new ArrayList<>();depts.add(new Dept(10, "ACCOUNTING", "NEWYORK"));depts.add(new Dept(20, "RESEARCH", "DALLAS"));depts.add(new Dept(20, "RESEARCH", "DALLAS"));depts.add(new Dept(30, "SALES", "CHICAGO"));depts.add(new Dept(30, "SALES", "CHICAGO"));depts.add(new Dept(30, "OPERATIONS", "BOSTON"));//方法一:List<Dept> res = depts.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Dept::getDeptno))), ArrayList::new));res.forEach(System.out::println);//方法二:List<Integer> deptnoList = new ArrayList<>();List<Dept> res2 = depts.stream().filter(item -> {boolean flag = !deptnoList.contains(item.getDeptno());deptnoList.add(item.getDeptno());return flag;}).collect(Collectors.toList());res2.forEach(System.out::println);}
结果:
示例三:对于复杂类型,根据指定属性去重(建议✨✨✨✨✨)
public class DemoTest {public static void main(String[] args) {List<Dept> depts = new ArrayList<>();depts.add(new Dept(10, "ACCOUNTING", "NEWYORK"));depts.add(new Dept(20, "RESEARCH", "DALLAS"));depts.add(new Dept(20, "RESEARCH", "DALLAS"));depts.add(new Dept(30, "SALES", "CHICAGO"));depts.add(new Dept(30, "SALES", "CHICAGO"));depts.add(new Dept(30, "OPERATIONS", "BOSTON"));//filter参数为true则保留depts.stream().filter(distinctByKey(Dept::getDeptno)).forEach(System.out::println);}private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {Map<Object,Boolean> seen = new ConcurrentHashMap<>();//putIfAbsent方法添加键值对,如果map集合中没有该key对应的值,则直接添加,并返回null,如果已经存在对应的值,则依旧为原来的值。//如果返回null表示添加数据成功(不重复),不重复(null==null :TRUE)return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;}}
结果:
示例四:对于复杂类型,根据指定属性去重
public static void main(String[] args) {List<Dept> depts = new ArrayList<>();depts.add(new Dept(10, "ACCOUNTING", "NEWYORK"));depts.add(new Dept(20, "RESEARCH", "DALLAS"));depts.add(new Dept(20, "RESEARCH", "DALLAS"));depts.add(new Dept(30, "SALES", "CHICAGO"));depts.add(new Dept(30, "SALES", "CHICAGO"));depts.add(new Dept(30, "OPERATIONS", "BOSTON"));final List<Dept> res = depts.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Dept::getDeptno))), ArrayList::new));res.forEach(System.out::println);}
结果: