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)