0

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

the graph

Edit 2: solved!

countries.stream().distinct().forEach(o -> 
                dataset.setValue(Collections.frequency(countries, o),
                                                "",
                                                o)); //sets the graph for the given country
1
  • 1
    Stream of Collection that will return all of the objects along with number of their occurrences ? Commented Dec 11, 2017 at 18:48

2 Answers 2

4

You could also use grouping collector:

Collection<Integer> collection = Arrays.asList(1, 2, 1, 4, 2);
final Map<Integer, Long> map = collection.stream().collect(
        Collectors.groupingBy(el -> el, Collectors.counting()));
System.out.println(map);

This produces

{1=2, 2=2, 4=1}
Sign up to request clarification or add additional context in comments.

1 Comment

I find this solution preferable over the accepted answer which is merely a streamified for loop with higher time complexity than this solution. By using the collectors, it retains all counts immediately without relying on maintaining any intermediate values yourself.
0

You can do this:

 List<Integer> lista = new ArrayList<>(Arrays.asList(1,2,3,3,2,1,5,4,1,2,3));
 lista.stream().distinct().forEach(o -> System.out.println(o + " occures " + Collections.frequency(lista, o) + " times in a list !"));

Output:

1 occures 3 times in a list !

2 occures 3 times in a list !

3 occures 3 times in a list !

5 occures 1 times in a list !

4 occures 1 times in a list !

In short:

We make stream out of your list, we remove duplicates from the stream by using .distinct(), and now when we are left with unique elements from the list we use Collections.frequency to print out how many times each element occurs in the list.

2 Comments

is there a way to, instead of System.out.print, save this output to a given integer?
You can save results to Map like Roman posted below. If you want you can edit your question and post exactly what you have in mind and I'll take a look at it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.