I'm new to streams and how they work and I am trying to get the occurrences of a specific object that is added in the list.
I found a way doing this using Collections. It goes as follows:
for (int i = 0; i < countries.size(); i++) {
int occurrences = Collections.frequency(countries, countries.get(i));
}
But I want to use stream. The method I used with streams was:
countries.parallelStream().filter(p -> p.contentEquals(countries.get(countries.size()-1))).count()
This only returns the current object and its occurrences, instead I want all the objects and their occurrences.
Edit:
`private final ArrayList<String> countries = new ArrayList<>();
dataset.setValue(countries.parallelStream()
.filter(p -> p.contentEquals(countries.get(countries.size()-1)) )
.count(),
"",
countries.get(countries.size()-1)); //sets the graph for the given country
@Override
public void addCountries(String country) {
countries.add(country);
}
@Override
public void removeCountries(int country) {
countries.remove(country);
}`
I am making a graph. The first statement of dataset.setValue() is the amount of occurrences of the country. This has to be done for each country so you can see how many occurrences of a country there is. hope this helps
Edit 2: solved!
countries.stream().distinct().forEach(o ->
dataset.setValue(Collections.frequency(countries, o),
"",
o)); //sets the graph for the given country