0

I have a Product named class. All fields in this class are filled from the database:

 public class Product {
            private Long id;
            private String code;
            private String name;
            private String type;
            private Integer quantity;
            private Integer numOfPieces;
            private Integer sumOfPieces;
            private String savingPeriod;
            private Double costPrice;
            private Double price;

            public Product() {
            }

            public Product(Long id, String code, String name, String type, Integer 
            quantity, Integer numOfPieces, Integer sumOfPieces, String savingPeriod, 
            Double costPrice, Double price) {
                this.id = id;
                this.code = code;
                this.name = name;
                this.type = type;
                this.quantity = quantity;
                this.numOfPieces = numOfPieces;
                this.sumOfPieces = sumOfPieces;
                this.savingPeriod = savingPeriod;
                this.costPrice = costPrice;
                this.price = price;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public Integer getQuantity() {
            return quantity;
        }

        public void setQuantity(Integer quantity) {
            this.quantity = quantity;
        }

        public Integer getNumOfPieces() {
            return numOfPieces;
        }

        public void setNumOfPieces(Integer numOfPieces) {
            this.numOfPieces = numOfPieces;
        }

        public Integer getSumOfPieces() {
            return sumOfPieces;
        }

        public void setSumOfPieces(Integer sumOfPieces) {
            this.sumOfPieces = sumOfPieces;
        }

        public String getSavingPeriod() {
            return savingPeriod;
        }

        public void setSavingPeriod(String savingPeriod) {
            this.savingPeriod = savingPeriod;
        }

        public Double getCostPrice() {
            return costPrice;
        }

        public void setCostPrice(Double costPrice) {
            this.costPrice = costPrice;
        }

        public Double getPrice() {
            return price;
        }

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

        @Override
        public String toString() {
            return "Product{" + "id=" + id + ", code=" + code + ", name=" + name + ", type=" + type + ", quantity=" + quantity + ", numOfPieces=" + numOfPieces + ", sumOfPieces=" + sumOfPieces + ", savingPeriod=" + savingPeriod + ", costPrice=" + costPrice + ", tax=" + price + '}';
        }
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 83 * hash + Objects.hashCode(this.id);
        hash = 83 * hash + Objects.hashCode(this.code);
        hash = 83 * hash + Objects.hashCode(this.name);
        hash = 83 * hash + Objects.hashCode(this.type);
        hash = 83 * hash + Objects.hashCode(this.quantity);
        hash = 83 * hash + Objects.hashCode(this.numOfPieces);
        hash = 83 * hash + Objects.hashCode(this.sumOfPieces);
        hash = 83 * hash + Objects.hashCode(this.savingPeriod);
        hash = 83 * hash + Objects.hashCode(this.costPrice);
        hash = 83 * hash + Objects.hashCode(this.price);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Product other = (Product) obj;
        if (!Objects.equals(this.code, other.code)) {
            return false;
        }
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        if (!Objects.equals(this.type, other.type)) {
            return false;
        }
        if (!Objects.equals(this.savingPeriod, other.savingPeriod)) {
            return false;
        }
        if (!Objects.equals(this.id, other.id)) {
            return false;
        }
        if (!Objects.equals(this.quantity, other.quantity)) {
            return false;
        }
        if (!Objects.equals(this.numOfPieces, other.numOfPieces)) {
            return false;
        }
        if (!Objects.equals(this.sumOfPieces, other.sumOfPieces)) {
            return false;
        }
        if (!Objects.equals(this.costPrice, other.costPrice)) {
            return false;
        }
        if (!Objects.equals(this.price, other.price)) {
            return false;
        }
        return true;
    }
    }

And I have a ProductsController class to work with the database:

ProductsController productsController = new ProductsController();

I create a new ArrayList and add some Product Objects that have the same information:

Product p1 = productsController.findOneById(1); //p1.getName() = "Analgin"
Product p2 = productsController.findOneById(1); //p2.getName() = "Analgin"
Product p3 = productsController.findOneById(1); //p3.getName() = "Analgin"
Product p4 = productsController.findOneById(1); //p4.getName() = "Analgin"
Product p5 = productsController.findOneById(2); //p5.getName() = "Pikovid"
Product p6 = productsController.findOneById(2); //p6.getName() = "Pikovid"
Product p7 = productsController.findOneById(3); //p7.getName() = "Bolnol"
List<Product> productsList = new ArrayList<>();

How can I calculate the same elements in the ArrayList for the following output:

  1. 4 Analgin
  2. 2 Pikovid
  3. 1 Bolnol

2 Answers2

1

Javlonbek,

I suggest you use the 'groupingBy' function of Java Streams.

productsList.stream().collect(
   Collectors.groupingBy(product -> product.getName(),
   Collectors.counting())
);

In your example the output was sorted descending by frequency. I'm not sure if that was an accident or intentional. The streamApi also allows you to so the sorting. See the example on the mkyong-website [1].

Cheers, Marco

1: https://www.mkyong.com/java8/java-8-collectors-groupingby-and-mapping-example/

McBeelen
  • 280
  • 1
  • 10
-1

I see that you have implemented equals and hashCode, so it is enough to build a map like this:

HashMap <String, Product> productsByName = new HashMap <>();
productsByName.put(product1.getName(), product1);
productsByName.put(product2.getName(), product2);
// add all the products

And this map already contains such information, it is enough to query the list of products by given name, for instance:

List <Product> analginProducts = productsByName.get("Analgin");
NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • This does not solve the original question. Adding an entry to the map with the same key will replace the existing stored value and there will not allow you to count the number of occurances of each distinct product. – McBeelen Oct 12 '20 at 13:24