简体   繁体   中英

Using Java8 stream(), collect(), and groupingBy() , etc to add integer value based on two same beans properties value

I have a bean as:

class Foo { 
  private String category;
  private int qty;
  private int price;
}

now I have a list as:

  List foos = new ArrayList();
  foos.add(new Foo("A", 1, 10));
  foos.add(new Foo("A", 2, 10));
  foos.add(new Foo("A", 2, 20));
  foos.add(new Foo("B", 1, 10));
  foos.add(new Foo("C", 1, 30));
  foos.add(new Foo("C", 5, 10));

I want to add price whose category and qty are same, for example two same A category has two same qty(Foo("A", 2, 10), new Foo("A", 2, 20)), then another or same list should be as "A 1 10" and "A 2 30"

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Main {

    private static <T> Predicate<T> distinctByKeys(Function<? super T, ?>... keyExtractors) {
        final Map<List<?>, Boolean> seen = new ConcurrentHashMap<>();

        return t -> {
            final List<?> keys = Arrays.stream(keyExtractors).map(ke -> ke.apply(t)).collect(Collectors.toList());

            return seen.putIfAbsent(keys, Boolean.TRUE) == null;
        };
    }

    public static void main(String[] args) {
        List<Foo> foos = new ArrayList<>();
        List<Foo> resultfoos = new ArrayList<>();
        foos.add(new Foo("A", 1, 10));
        foos.add(new Foo("A", 2, 10));
        foos.add(new Foo("A", 2, 20));
        foos.add(new Foo("B", 1, 10));
        foos.add(new Foo("C", 1, 30));
        foos.add(new Foo("C", 5, 10));


        foos.stream().filter(distinctByKeys(Foo::getCategory, Foo::getQty)).forEach(o -> {

            o.setPrice(foos.stream().distinct()
                    .filter(x -> x.getCategory().equalsIgnoreCase(o.getCategory()) && x.getQty() == o.getQty())
                    .mapToInt(t -> t.getPrice()).sum());

            System.out.println(o.toString());

            resultfoos.add(o), o.getPrice()));
        });

    }

}

class Foo {
    private String category;
    private int qty;
    private int price;

    Foo(String category, int qty, int price) {
        this.category = category;
        this.qty = qty;
        this.price = price;

    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getQty() {
        return qty;
    }

    public void setQty(int qty) {
        this.qty = qty;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Foo [category=" + category + ", qty=" + qty + ", price=" + price + "]";
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM