关于java:Java-DEV集合去重

52次阅读

共计 1083 个字符,预计需要花费 3 分钟才能阅读完成。

private List<T> deWeight(List<T> list, String type){if (CollectionUtils.isEmpty(list))
            return null;

        while (StringUtils.isNotBlank(type)) {switch (type) {
                case "1":
                    for (int i = 0; i < list.size(); i++) {for (int j = i + 1; j < list.size(); j++) {if (list.get(i).equals(list.get(j))) {list.remove(j);
                            }
                        }
                    }
                case "2":
                    List<T> newList = new ArrayList<>();
                    for (T obj : list) {if (!newList.contains(obj)) {newList.add(obj);
                        }
                    }
                    list.clear();
                    list.addAll(newList);
                case "3":
                    HashSet hashSet = new HashSet(list);
                    list.clear();
                    list.addAll(hashSet);
                case "4":
                    TreeSet treeSet = new TreeSet(list);
                    list.clear();
                    list.addAll(treeSet);
                case "5":
                    list.stream().distinct().collect(Collectors.toList());
                case "6":
                    Iterator iterator = list.iterator();
                    while (iterator.hasNext()) {Object t = iterator.next();
                        Iterator iterator1 = list.iterator();
                        while (iterator1.hasNext()) {Object ts = iterator1.next();
                            if (t == ts)
                                iterator.remove();}
                    }
                case "7":
                    // 快慢指针的模式,汇合中的数据得是有程序的,否则去重会有问题慎用
                    int slowIndex = 0;
                    for (int fastIndex = 1; fastIndex < list.size(); fastIndex++) {if (list.get(slowIndex) != list.get(fastIndex)) {
                            slowIndex ++;
                            list.set(slowIndex, list.get(fastIndex));
                        }
                    }
                    list.subList(0,slowIndex);
                default:
                    list = list;

            }
        }
        return list;
    }

正文完
 0