• list去重三种方法


    package com.kit.api.test.question;
    
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    
    /**
     * @version: V1.0
     * @author: songyan
     * @className: ListDuplicateRemoval
     * @packageName: com.kit.api.test.question
     * @description: 列表去重
     * @date: 2021/3/24   8:55
     */
    public class ListDuplicateRemoval {
    
        /**
         * @author: songyan
         * @methodsName: hashSetDuplicateRemoval
         * @param: list
         * @description: 基于HashSet元素不可重复去重
         * @return: void
         */
        public void hashSetDuplicateRemoval(List list) {
            HashSet hashSet = new HashSet(list);
            list.clear();
            list.addAll(hashSet);
        }
    
        /**
         * @author: songyan
         * @methodsName: containsDuplicateRemoval
         * @param: list
         * @description: 基于contains方法去重
         * @return: void
         */
        public List containsDuplicateRemoval(List list) {
            List list2 = new ArrayList();
            if (list != null) {
                for (Object obj : list) {
                    if (!list2.contains(obj)) {
                        list2.add(obj);
                    }
                }
            }
            return list2;
        }
    
        /**
         * @author: songyan
         * @methodsName: forDuplicateRemoval
         * @description: for循环去重
         * @return: java.util.List
         */
        public void forDuplicateRemoval(List list) {
            for (int i = 0; i < list.size(); i++) {
                for (int j = list.size() - 1; j > i; j--) {
                    if (list.get(i).equals(list.get(j))) {
                        list.remove(j);
                    }
                }
            }
        }
    
    }
  • 相关阅读:
    1242 斐波那契数列的第N项
    1256 乘法逆元
    1264 线段相交
    1265 四点共面
    Uva10881 Piotr's Ants
    hdu 5438 Ponds 长春网赛1002
    CodeForces 540D Bad Luck Island 概率dp
    hdu 1281 二分图残量增广
    hdu 2444判定二分图+最大匹配
    hdu 3416 Marriage Match IV
  • 原文地址:https://www.cnblogs.com/excellencesy/p/14572076.html
Copyright © 2020-2023  润新知