I have a list from a legacy system. Its in a List. Sometimes it may have duplicates.
How do I remove second duplicate (or multiple) while keeping first one? For example, keep 3-furniture and remove 3-TV. Removal is based on alphabetization.
| ProductId | ProductTitle | ProductDescription |
|---|---|---|
| 1 | car | used for driving |
| 2 | book | reading books |
| 3 | furniture | home decorations |
| 3 | TV | TV entertainment |
| 4 | jewelry | fashion item |
@Data
public class Product {
private long productId;
private String productTitle;
private String productDescription;
}
You can achieve it in two steps.
Group to a Map where the productId is a key and the product with the lowest alphabetical productTitle using Collectors.maxBy as a downstream collector.
Map<Long, Optional<Product>> productMap = products.stream().collect(
Collectors.groupingBy(
Product::getProductId,
Collectors.maxBy(Comparator.comparing(Product::getProductTitle))));
productMap.forEach((id, opt) -> System.out.println(id + " = " + opt));
1 = Optional[Product(productId=1, productTitle=car, productDescription=used for driving)] 2 = Optional[Product(productId=2, productTitle=book, productDescription=reading books)] 3 = Optional[Product(productId=3, productTitle=furniture, productDescription=home decorations)] 4 = Optional[Product(productId=4, productTitle=jewelry, productDescription=fashion item)]
Extract each Product from Optional in the Map values to a new List<Product>.
List<Product> filteredProducts = productMap.values().stream()
.filter(Optional::isPresent)
.map(Optional::get)
.toList();
filteredProducts.forEach(System.out::println);
Product(productId=1, productTitle=car, productDescription=used for driving) Product(productId=2, productTitle=book, productDescription=reading books) Product(productId=3, productTitle=furniture, productDescription=home decorations) Product(productId=4, productTitle=jewelry, productDescription=fashion item)
Hi you can easily do it in Java with PriorityQueue and time complexity is O(nlogn)
public static List<Product> filterProduct(List<Product> products) {
PriorityQueue<Product> pq = new PriorityQueue<>((a, b) -> {
if (a.getProductId() == b.getProductId()) {
return a.getProductTitle().toLowerCase().compareTo(b.getProductTitle().toLowerCase());
}
return Long.compare(a.getProductId(), b.getProductId());
});
pq.addAll(products);
Set<Long> existingIds = new HashSet<>();
List<Product> res = new ArrayList<>();
while (!pq.isEmpty()) {
Product product = pq.poll();
if (!existingIds.contains(product.getProductId())) {
res.add(product);
existingIds.add(product.getProductId());
}
}
return res;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With