0

How would I add up values in Custom Array list having bean class consisting variables itemcode (String) , cases(int) , packs (int) , pcs (int) I have more than one value saved in Array list as

 {code1 , 3,4,5}  
    {code2 , 3,5,5}  
    {code3 , 3,2,5}  
    {code3 , 1,4,2}  
    {code1 , 3,3,5}  
    {code3 , 3,4,5}  
    {code2 , 3,8,5}  
    {code2 , 3,4,6}

In Such case I need result as

{code1 ,6,7,10 }
{code2 ,9,17,16 }
{code3 ,7,10,12}  
3
  • 1
    Show some actual code please. And explain where you're stuck exactly. Commented Feb 20, 2017 at 7:58
  • I was checking out this example stackoverflow.com/questions/25699131/… Commented Feb 20, 2017 at 8:00
  • 1
    @Yatin: What's preventing you from using the answers given in the question you link to? Commented Feb 20, 2017 at 8:33

4 Answers 4

0

I don't know exactly how your bean looks, but with a HashMap and loop you can "group by and sum" as you need. It could look something like this:

public static List<BeanClass> sumPerItemCode
    (List<BeanClass> list) {

    Map<String, BeanClass> map = new HashMap<>();

    for (BeanClass p : list) {
        String code = p.getItemCode();
        BeanClass aggregatedItem = map.get(code);
        if (aggregatedItem == null) {
            aggregatedItem = new BeanClass(code, 0,0,0);
            map.put(code, aggregatedItem);
        }

        aggregatedItem.setCases(aggregatedItem.getCases() + p.getCases());
        aggregatedItem.setPacks(aggregatedItem.getPacks() + p.getPacks());
        aggregatedItem.setPcs(aggregatedItem.getPcs() + p.getPcs());
    }
    return new ArrayList<BeanClass>(map.values());
}
Sign up to request clarification or add additional context in comments.

Comments

0

I suggest you to use Map instead of List.

Map replace the value of existing value if key is same.

what you have to do is first check is key is available get that value and do addition then again store it in Map using same key.

`

Comments

0

Try with it -

ArrayList<String> list = new ArrayList<String>();
list.add("code1");               //itemcode
list.add(String.valueOf(100));   //cases
list.add(String.valueOf(150));   //packs
list.add(String.valueOf(200));   //pcs

And to get data fron arrayList -

String itemcode  = list.get(0);              //code1
int cases = Integer.parseInt(list.get(1));   //100
int packs = Integer.parseInt(list.get(2));   //150
int pcs = Integer.parseInt(list.get(3));     //200

But i always suggest to use Map instead of ArrayList for this type of scenario with same mechanism. Because Map can store data as key value pair.

Comments

0

Create a Bean Class Like that
public class BeanClass{
public String code;
public int integerOne;
public int integerTwo;
public int integerThree;
public BeanClass(String code,int integerOne,int integerTwo,int integerThree){
this.code=code;
this.integerOne=integerOne;
this.integerTwo=integerTwo;
this.integerThree=integerThree
}
}
In Main Class
ArrayList<BeanClass>beanclassArrayList=new ArrayList<>();
for(int i=0;i<YourArrray[].length();i++){
BeanClass beanClass=new BeanClass(codeArray[i],integerOneArray[i],integerTwoArray[i],integerThreeArray[i]);
beanclassArrayList.add(beenClass);
}

2 Comments

Would it be possible for you to format your answer a bit? It's completely unreadable in its current state.
**check it**@Henrik

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.