Java 去除List列表中的重复项

Posted 好久不见

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 去除List列表中的重复项相关的知识,希望对你有一定的参考价值。

   /**
       * Remove list duplicate item
     * 
     * @param srcList
     * @return
     */
    private static ArrayList<ResolveInfo> removeListDuplicateItemWithOrder(
            List<ResolveInfo> srcList) {
        ArrayList<ResolveInfo> mResolveInfoList = new ArrayList<ResolveInfo>();
        ArrayList<String> mPkgNameList = new ArrayList<String>();
        for (ResolveInfo resolveInfo : srcList) {
            String pkgName = resolveInfo.activityInfo.packageName;
            XLog.d(TAG, "removeListDuplicateWithOrder,pkgName=" + pkgName);
            // remove duplicate package name
            if (!mPkgNameList.contains(pkgName)) {
                mPkgNameList.add(pkgName);
                mResolveInfoList.add(resolveInfo);
            }
        }

        if (DEBUG_MODE) {
            for (ResolveInfo resolveInfo : mResolveInfoList) {
                XLog.d(TAG, "kkkk,removeDuplicateWithOrder,resolveInfo="
                        + resolveInfo);
            }
        }
        return mResolveInfoList;
    }

    /**
     * Remove list duplicate item
     * 
     * @param srcList
     * @return
     */
    private static ArrayList<String> removeListDuplicateItemWithOrder(
            ArrayList<String> srcList) {
        ArrayList<String> desList = new ArrayList<String>();
        for (String src : srcList) {
            XLog.d(TAG, "removeListDuplicateWithOrder,src=" + src);
            if (!desList.contains(src)) {
                desList.add(src);
            }
        }

        if (DEBUG_MODE) {
            for (String des : desList) {
                XLog.d(TAG, "removeDuplicateWithOrder,des=" + des);
            }
        }
        return desList;
    }

    /**
     * Remove liset duplicate item
     * 
     * @param srcList
     * @return
     */
    private static Set removeListDuplicateItemWithOrder2(
            ArrayList<String> srcList) {
        Set mLinkedHashSet = new LinkedHashSet<String>();
        mLinkedHashSet.add(srcList);
        for (Iterator iterator = mLinkedHashSet.iterator(); iterator.hasNext();) {
            String element = (String) iterator.next();
            XLog.d(TAG, "removeListDuplicateWithOrder2,element=" + element);
        }
        return mLinkedHashSet;
    }

 

以上是关于Java 去除List列表中的重复项的主要内容,如果未能解决你的问题,请参考以下文章

如何删除已经在list中的重复项

JAVA中的List里的Map 如何去除重复的Key?

如何去除List集合中的重复项ID的,并把重复项的数量相加

java List去除重复数据的五种方式

List 去除重复数据的五种方式,舒服~

List 去除重复数据的五种方式,舒服~