Orderid Amount order id Amount
c4 1 c2 60
c2 10 c3 38
c2 20 c4 13
c2 30 null null
c3 11 null null
c3 13 null null
c3 14 null null
c4 12 null null
Above are two arrays(order id , Amount)
The first Array consists of elements containing duplicate orderid’s along with their values , i’m trying to find out distinct orderid’s along with their sum as shown in the second array . for that i wrote the below code . its not working . Pls help
for(int y=0;y<data.length;y++) {
if(data[y][0].equals(data[y+1][0])==true)
{
tx=(Double) data[y][1]+(Double)data [y+1][1];
data[y+1][1] = tx;
data[y][1]=tx;
data[y][0]=null;
}
if(y+1 > data.length)
{
break;
}
System.out.println("Tx="+tx);
}
Consider using a
HashMap<String><Integer>to keep track of the order ids and totals. Iterate over the items in the array, and for each order id/amount pair, check if the order id is in the hashmap. If it’s not already there, insert it with its amount into the hashmap; if it is, add the amount to that place in the hashmap.