Count occurrence of numbers

The problem

Write a program that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0.

Breaking it down

Collecting numbers from a user and adding them to an ArrayList will allow us to use java 8 Collectors.counting() a group by technique. Finally printing out the stream will show the number entered and the number of times it was inputted.

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter the integers between 1 and 100: ");

    List<Integer> numbers = new ArrayList<>();

    for (int i = 0; i < 10; i++) {
        numbers.add(input.nextInt());
    }
    input.close();

    Map<Object, Long> frequentNumbers = numbers.stream().collect(
            Collectors.groupingBy(c -> c, Collectors.counting()));

    frequentNumbers.forEach((k, v) -> System.out.println(k + ":" + v));
}

Output

Enter the integers between 1 and 100: 1
2
3
4
56
7
3
3
3
3
1:1
2:1
3:5
4:1
7:1
56:1