I am learning streams in java and I am confused. I have search a lot on google but could not find an approach. Map still confuses me.
So, I have two integer lists PlayerA[] and PlayerB[] of same size 8. I am trying to add all elements except the last one in each list.
int smallPitStonesPlayerA = 0;
int smallPitStonesPlayerB = 0;
for (int i = 1; i <= 6; i++) {
smallPitStonesPlayerA = smallPitStonesPlayerA + playerA.getStonesInPit(i);
smallPitStonesPlayerB = smallPitStonesPlayerB + playerB.getStonesInPit(i) ;
}
What I have tried so far. But this is equivalent to two loops. Is there a way to this once?
int num1 = playerA.stream()
.map(n-> n.getStonesInPit())
.collect(Collectors.summingInt(Integer::intValue));
int num2 = playerB.stream()
.map(n-> n.getStonesInPit())
.collect(Collectors.summingInt(Integer::intValue));