Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java List, Keep First Item, Remove Second Duplicate [duplicate]

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;
 }
like image 270
mattsmith5 Avatar asked Aug 07 '26 21:08

mattsmith5


2 Answers

You can achieve it in two steps.

  1. 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)]
    
  2. 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)
    
like image 175
Nikolas Charalambidis Avatar answered Aug 09 '26 10:08

Nikolas Charalambidis


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;
    }
like image 27
sendon1982 Avatar answered Aug 09 '26 09:08

sendon1982



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!