Skip to content

Instantly share code, notes, and snippets.

@namannigam
Created May 17, 2021 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namannigam/d53f8523f0a6499ca661d6b922fa36cc to your computer and use it in GitHub Desktop.
Save namannigam/d53f8523f0a6499ca661d6b922fa36cc to your computer and use it in GitHub Desktop.
//https://stackoverflow.com/questions/67570361/how-to-identify-duplicate-records-in-a-list
Map<Boolean, List<MyVo>> partitionBasedOnRegistered = dataList.stream()
.collect(Collectors.partitioningBy(MyVo::isRegistered));
List<MyVo> unregisteredBusinesses = partitionBasedOnRegistered.get(Boolean.FALSE); // here
List<MyVo> registeredBusinesses = partitionBasedOnRegistered.get(Boolean.TRUE);
Map<String, List<MyVo>> groupByRegistrationNumber = registeredBusinesses.stream()
.collect(Collectors.groupingBy(MyVo::registrationNumber));
Map<Boolean, List<List<MyVo>>> partitionBasedOnDuplicates = groupByRegistrationNumber
.entrySet().stream()
.collect(Collectors.partitioningBy(e -> e.getValue().size() > 1,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
List<List<MyVo>> groupedRegisteredUniqueBusiness = partitionBasedOnDuplicates.get(Boolean.FALSE); // here
List<List<MyVo>> groupedRegisteredDuplicateBusiness = partitionBasedOnDuplicates.get(Boolean.TRUE); //here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment