[Solved] Iteration In Arraylist in Java


Use a hashMap and check if this key already exists then update the double value by adding existing double value then put in the map.

public static void main(String[] args) {
    List<MyObject> list = new ArrayList<MyObject>();
    list.add(new MyObject("a","b","c",new BigDecimal(10.23)));
    list.add(new MyObject("a","b","e",new BigDecimal(10.23)));
    list.add(new MyObject("a","b","c",new BigDecimal(10.23)));
    list.add(new MyObject("a","b","d",new BigDecimal(10.23)));
    Map<MyObject,MyObject> map = new HashMap<MyObject,MyObject>(){
       @Override
    public MyObject put(MyObject key, MyObject value) {
           //synchronize if required
        if (containsKey(key)) {
            value.addDouble(get(key).myDouble);
        }
        return super.put(key, value);
    }
    };

 for (MyObject obj:list) {
        map.put(obj, obj);
    }
    for (MyObject obj : map.values())
    System.out.println(obj);
}

Output:

abd10.230000000000000426325641456060111522674560546875
abc20.460000000000000852651282912120223045349121093750
abe10.230000000000000426325641456060111522674560546875

Make sure to use hashCode and equals only on first 3 properties. Not on double.

class MyObject {
    String myProp1;
    String myProp2;
    String myProb3;
    BigDecimal myDouble;

    public MyObject(String myProp1, String myProp2, String myProb3, BigDecimal myDouble) {
        super();
        this.myProp1 = myProp1;
        this.myProp2 = myProp2;
        this.myProb3 = myProb3;
        this.myDouble = myDouble;
    }
    public void addDouble(BigDecimal addValue) {
        myDouble = myDouble.add(addValue);
    }
    @Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + ((myProb3 == null) ? 0 : myProb3.hashCode());
        result = PRIME * result + ((myProp1 == null) ? 0 : myProp1.hashCode());
        result = PRIME * result + ((myProp2 == null) ? 0 : myProp2.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final MyObject other = (MyObject) obj;
        if (myProb3 == null) {
            if (other.myProb3 != null)
                return false;
        } else if (!myProb3.equals(other.myProb3))
            return false;
        if (myProp1 == null) {
            if (other.myProp1 != null)
                return false;
        } else if (!myProp1.equals(other.myProp1))
            return false;
        if (myProp2 == null) {
            if (other.myProp2 != null)
                return false;
        } else if (!myProp2.equals(other.myProp2))
            return false;
          return true;
        }
      }

11

solved Iteration In Arraylist in Java