Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组列表计数相等对象_Java_Sorting_Search_Arraylist - Fatal编程技术网

Java 数组列表计数相等对象

Java 数组列表计数相等对象,java,sorting,search,arraylist,Java,Sorting,Search,Arraylist,我有一个产品名为class。此类中的所有字段都是从数据库中填写的: public class Product { private Long id; private String code; private String name; private String type; private Integer quantity; private Intege

我有一个产品名为class。此类中的所有字段都是从数据库中填写的:

 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;
    }
    }
ProductsController productsController = new ProductsController();
我有一个ProductsController类来处理数据库:

 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;
    }
    }
ProductsController productsController = new ProductsController();
我创建了一个新的ArrayList,并添加了一些具有相同信息的产品对象:

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<>();
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 productsList=new ArrayList();
如何为以下输出计算ArrayList中的相同元素:

  • 4安乃近
  • 2皮科维德
  • 1波诺

  • 我看到您已经实现了equals和hashCode,所以构建这样的映射就足够了:

    HashMap <String, Product> productsByName = new HashMap <>();
    productsByName.put(product1.getName(), product1);
    productsByName.put(product2.getName(), product2);
    // add all the products
    
    HashMap productsByName=newhashmap();
    productsByName.put(product1.getName(),product1);
    productsByName.put(product2.getName(),product2);
    //添加所有产品
    
    而且这个地图已经包含了这样的信息,按给定的名称查询产品列表就足够了,例如:

    List <Product> analginProducts = productsByName.get("Analgin");
    
    List analginProducts=productsByName.get(“Analgin”);
    
    Javlonbek

    我建议您使用Java Streams的“groupingBy”函数

    productsList.stream().collect(
       Collectors.groupingBy(product -> product.getName(),
       Collectors.counting())
    );
    
    在您的示例中,输出按频率降序排列。我不确定那是意外还是故意的。streamApi还允许您执行排序。请参见mkyong网站[1]上的示例

    干杯, 马可


    1:

    首先覆盖equals/hashCode,并提供使之成为可能的实现
    ProductsController
    很可能是
    ProductsRepository
    。好的,谢谢,我是beginner@Javlonbek看看你的equals方法,看看你的要求。你能看出区别吗?@Stultuske我想是的。我是用NetBeans自动完成的。这并不能解决最初的问题。向地图添加具有相同密钥的条目将替换现有存储值,并且不允许您计算每个不同产品的发生次数。