I have class say for example public class Item {
int price;
String name;
// getters and setters
}
I have such 1000 or more objects (just example).
For each item there is a different price. And All this item objects are in List<Item> my requirement is to get total price (i.e price for item 1 to nth item of the list).
Is there any utility or way by which i can get total for the particular field (i.e total price of all the items). I just give List, ClassName and fieldName I get the total? I know we can get the total by iterating through the list, call get method add all up and store in some variable.?
Thanks in advance.
I have just written a simple method which calculates a sum of some properties in list:
For this I use javabeans technology. You can download needed libraries directly from apache site.
Here’s example of using it:
And calculating sum: