3

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;
 }
2
  • What do you mean "based on alphabetization"? Do you want to keep the first one sharing the same ProductId based on their ProductTitle alphabetical ascending sort? Commented Mar 8, 2023 at 3:50
  • based on productTitle alphabetization, keep 3-f, remove 3t, cc @NikolasCharalambidis Commented Mar 8, 2023 at 3:51

2 Answers 2

1

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)
    
Sign up to request clarification or add additional context in comments.

Comments

1

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;
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.