简体   繁体   English

如何在 SpringBoot 中添加 List 中元素的频率?

[英]How to add frequency of elements from List in SpringBoot?

This is my entity class and I am trying to show the frequency of elements as quantity but everytime I am getting 1 as quantity这是我的实体类,我试图将元素的频率显示为数量,但每次我得到 1 作为数量

@GeneratedValue(strategy = GenerationType.IDENTITY)
private int productid;
private String productname;
private String purchaseamount;
@Transient
private int quantity;

Here I am trying to get the quantity on the basis of frequency of elements of productId , but for every product I am getting 1 only在这里,我试图根据 productId 元素的频率获取数量,但对于每个产品,我只得到 1 个

   public List<AddingProductForView> getData(List<Integer> productid) {
        List<AddingProductForView> findAllByProductIdIn = repository.findAllByproductidIn(productid);
        for (int i = 0; i < findAllByProductIdIn.size(); i++) {
            Set<Integer> distinct = new HashSet<>(productid);
            for (Integer count : distinct) {
                int frequency = Collections.frequency(productid, count);
                findAllByProductIdIn.get(i).setQuantity(frequency);
        
            }
        }
        return findAllByProductIdIn;
    }

RequestBody :- [1,1,1,1,2,2]请求正文:- [1,1,1,1,2,2]

[
    {
        "productid": 1,
        "productname": "RO",
        "purchaseamount": "4000  ",
        "quantity": 1
    },
    {
        "productid": 2,
        "productname": "RO",
        "purchaseamount": "8000  ",
        "quantity": 1
    }
]

ExpectedResult:-预期结果:-

 [
        {
            "productid": 1,
            "productname": "RO",
            "purchaseamount": "4000  ",
            "quantity": 4
        },
        {
            "productid": 2,
            "productname": "RO",
            "purchaseamount": "8000  ",
            "quantity": 2
        }
    ]

I can understand your intention.我能理解你的意图。
I try to this code my develop environment.我尝试将此代码用于我的开发环境。 It works very well.它运作良好。

I suggest some method我建议一些方法

1.You had better debug 1.你最好调试一下

// whether frequency is exist or not.
int frequency = Collections.frequency(productid, count);

2.Do you want to this really? 2.你真的要这样吗? If this code excutes,Result of frequency is final entity of set.Because you don't use if condition.如果此代码执行,频率的结果是集合的最终实体。因为您不使用 if 条件。

for (int i = 0; i < findAllByProductIdIn.size(); i++) {
            //distinct = [1,2]
            Set<Integer> distinct = new HashSet<>(productid);
            /** frequency is unconditionally 2. */
            for (Integer count : distinct) {
                int frequency = Collections.frequency(productid, count);
                findAllByProductIdIn.get(i).setQuantity(frequency);
    
            }
    }

I can check frequency and getQuantity.我可以检查频率和 getQuantity。 I recommend to debug.我建议调试。

you are necessary if condition如果有条件,你是必要的

loop : for (int i = 0; i < findAllByProductIdIn.size(); i++) {
            Set<Integer> distinct = new HashSet<>(productid);
            for (Integer count : distinct) {
                if(findAllByProductIdIn.get(i).getProductId == count) {
                    int frequency = Collections.frequency(productid, count);
                    findAllByProductIdIn.get(i).setQuantity(frequency);
                    continue loop;
                }
            }
        }

I modified your code.我修改了你的代码。 this is not necessary if condition and can reduce code line如果条件允许,这不是必需的,并且可以减少代码行

for (int i = 0; i < findAllByProductIdIn.size(); i++) {
     int frequency = Collections.frequency(productid,findAllByProductIdIn.get(i).getProductId);
     findAllByProductIdIn.get(i).setQuantity(frequency);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 SpringBoot 中创建一个新的 SQL 行并将元素添加到其列表字段? - How to create a new SQL row and add elements to its list field in SpringBoot? SpringBoot ArrayList/JSON:将某种类型的所有元素添加到该类型的列表中 - SpringBoot ArrayList/JSON: add all Elements of a certain type to a list of this type 如何将元素从列表添加到标签? - How to add elements from list to label? 如何将 JSONArray 中的元素添加到 QuestionnaireItemOptionComponent 列表? - How to add elements from JSONArray to QuestionnaireItemOptionComponent List? 如何将元素添加到另一个类的列表中? - How to add elements to a list from another class? 查找列表中的频率差异元素 - Finding the frequency difference elements in a list 如何使用Java中的类确定字符串数组列表元素的频率 - How to Determine Frequency of String Array List Elements Using Classes in Java 添加从列表返回的元素 - Add Elements Returned from a List 如何将从控制台添加的字符串中的元素添加到Java中的列表 - How to add elements from string added from console to a list in java 如何将名称添加到 jSON 数组中的列表,使用 springboot 发布响应 - How to add name to List in jSON Array, posting response using springboot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM