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列表中的重复项的主要内容,如果未能解决你的问题,请参考以下文章