Sum value Of Class Objects Property In ArrayList

前端 未结 3 588
北恋
北恋 2021-01-25 04:56

I have an ArrayList which having many objects. I want to do sum of values of the same property name.

Examples Data in Array List which is Objects of

3条回答
  •  一整个雨季
    2021-01-25 05:30

    Why you don't just put them in an Map based collection with key/value pairs ?

    I think that in this case a Map based collection is more suitable, for the data you have and the context in which you want to use it.

    If you need to preserve the single items inside the list, you could use a linked list as Value of the map .

    For example :

    HashMap> cache = new HashMap>();
    

    and SUM each element of the value LinkedList to find the total amount ... You could even wrap the LinkedList of doubles into a container object with helper functions like getTotal() or Sum().

提交回复
热议问题