I have a class like the following.
public class Votes{
String name;
int likes;
int dislikes;
}
I have a list like the following.
List<Votes> votesList;
Suppose I fill in some elements above the list. I want to declare a method that performs the grouping and summation operation on this list.
As an example, suppose I list the following items in a list as inputfor this method.
votesList.add(new Votes("A", 10, 5));
votesList.add(new Votes("B", 15, 10));
votesList.add(new Votes("A", 20, 15));
votesList.add(new Votes("B", 10, 25));
votesList.add(new Votes("C", 10, 20));
votesList.add(new Votes("C", 0, 15));
This method should output List<Votes>with the following elements.
("A", 30, 20),
("B", 25, 35),
("C", 10, 35)
Is there an easy way to do this using streams, lambda expressions in Java8? I know how this can be done using collectorsif I have only one intmemeber.
Can someone please explain to me how I can address this situation?