用途:找出重复的数字或者字段,也可用于去重 public class FindOne { public static void main(String[] args) { List<Integer>list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(1); list.add(2); Set set = checkRecur(list);//调用下面静态方法 System.out.println(set); } public static Set<Integer>checkRecur(List<Integer>list){//传入需要查重的数组 Set<Integer>set = new HashSet<>(); Set<Integer>recur = new HashSet<>();//利用hashSet 不可重复添加value的方法 for (Integer one:list){ if (!set.add(one)){ recur.add(one); } } return recur; } }
查重,去重的公用方法
于 2022-02-28 23:14:02 首次发布