Various ways to deduplicate Java List collection
1 Loop all elements in list and then delete duplicates
/** * Loop all elements in list and then delete duplicates * @param list list to be removed * @return List after removing the heavy load */ public static <T> List<T> removeDuplicate(List<T> list){ for(int i=0;i<()-1;i++){ for(int j=()-1;j>i;j--){ if ((j).equals((i))){ (j); } } } return list; }
2 Deduplication via HashSet
/** * Deduplication via HashSet * @param list list to be removed * @return List after removing the heavy load */ public static <T> List<T> removeDuplicateHashSet(List<T> list){ HashSet<T> hs = new HashSet<>(list); (); (hs); return list; }
3 Delete duplicate elements in List and keep the order
/** * Remove duplicate elements in List and keep order * @param list list to be removed * @return List after removing the heavy load */ public static <T> List<T> removeDuplicateKeepOrder(List<T> list){ Set set = new HashSet(); List<T> newList = new ArrayList<>(); for (T element : list) { //If you can add set, it means that it is not a duplicate element. if ((element)) (element); } (); (newList); return list; }
4 Use () to deduplicate
/** * Use () to deduplicate * @param list * @return */ public static <T> List<T> removeDuplicateContain(List<T> list){ List<T> listTemp = new ArrayList<>(); for (T aList : list) { if (!(aList)) { (aList); } } return listTemp; }
5 Things to note
The size or length is also changing when deleting, which will cause less deletion. Stream streams can also be used for filtering and deletion.
List filterList = ().filter(user -> () >= 40) .collect(toList());
example:
JSONObject jsonObject = (()); String content = ("content"); JSONArray jsonArray = (content); for (int i = () - 1; i >= 0; i--) { JSONObject jsonObject1 = (i); String caseId = ("caseId"); QueryWrapper queryWrapper=new QueryWrapper(); ("user_id",()); ("case_id",caseId); ("limit 1"); CaseInfo caseInfo = (queryWrapper); ("Case binding details for deduplication->{}",caseInfo); if (caseInfo!=null){ (i); } }
6 Use stream grouping and deduplication
Grouping
().collect((ClassEntity::getGrade));
Java8 deduplication (according to grade and major, when grade and major are the same, it is considered to be duplicate data)
List<ClassEntity> distinctClass = ().collect(((() -> new TreeSet<>((o -> () + ";" + ()))), ArrayList::new));
Deduplication is calculated by hashSet (such as classNames deduplication): This type of deduplication is calculated when the bean is exactly the same.
List<String> classNameList = new ArrayList(new HashSet(classNames));
7 Delete multiple elements in the collection
Because the underlying layer of stream traversal is still an iterator, it cannot delete it while traversing and then causing exceptions.ConcurrentModificationException
, so use traditional for loop to delete. One type of deletion is to delete it backwards, and the other type is to rewind one flag bit after deletion.
public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList(); ("111"); ("222"); // ().forEach(s -> { // (s); // (()); // } // ); for (int i = 0; i < (); i++) { (i); i--; } (arrayList); }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.