[JAVA] Get the number of occurrences for each element in the list

"Getting the number of occurrences for each element in the list" means In particular,

["a", "b", "c", "c", "b", "c",]

From

{"a": 1, "b": 2, "c": 3,}

I will refer to the operation to obtain.

Let's do this in several languages.


For Python


import collections

list = ["a", "b", "c", "c", "b", "c",]

counter = collections.Counter(list)
print(dict(counter))
# -> {'a': 1, 'b': 2, 'c': 3}

Reference: [python --How to count the frequency of the elements in a list? --Stack Overflow](https://stackoverflow.com/questions/2161752/how-to-count-the-frequency-of-the-elements- in-a-list)

For PHP


$list = ["a", "b", "c", "c", "b", "c",];

$counts = array_count_values($list);
echo json_encode($counts) . PHP_EOL;
// -> {"a":1,"b":2,"c":3}

Reference: PHP: array_count_values --Manual

For Java


// import java.util.*;
// import java.util.stream.Collectors;
// import java.util.function.Function;

List<String> list = Arrays.asList("a", "b", "c", "c", "b", "c");
Map<String, Long> counts = list.stream()
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(counts);
// -> {a=1, b=2, c=3}

Reference: [java --How to count the number of occurrences of an element in a List --Stack Overflow](https://stackoverflow.com/questions/505928/how-to-count-the-number-of-occurrences-of -an-element-in-a-list)


bonus

At first I tried it with Laravel as follows, but there is PHP is a standard function itself. You did ......... As expected PHP ……

When using Laravel's Collection



$list = ["a", "b", "c", "c", "b", "c",];

$counts = collect($list)
    ->groupBy(null)
    ->map->count()
;

echo json_encode($counts) . PHP_EOL;
// -> {"a":1,"b":2,"c":3}

Recommended Posts

Get the number of occurrences for each element in the list
How to count the number of occurrences of each element in the list in Python with weight
Get the number of specific elements in a python list
Get the index of each element of the confusion matrix in Python
python note: map -do the same for each element of the list
Get the number of digits
How to get the number of digits in Python
[python] Get the list of classes defined in the module
Get the size (number of elements) of UnionFind in Python
Get the number of views of Qiita
Check the operation of Python for .NET in each environment
How to identify the element with the smallest number of characters in a Python list?
Get the first element of queryset
Get the number of Youtube subscribers
Check the processing time and the number of calls for each process in python (cProfile)
[python] Get the rank of the values in List in ascending / descending order
Get the number of readers of a treatise on Mendeley in Python
Get the column list & data list of CASTable
Change the list in a for statement
Get the EDINET code list in Python
python memo: enumerate () -get index and element of list at the same time and turn for statement
Generate a list packed with the number of days in the current month.
[Linux] Command to get a list of commands executed in the past
Get the number of visits to each page with ReportingAPI + Cloud Functions
Get the list of packages for the specified user from the packages registered on PyPI
Get the key for the second layer migration of JSON data in python
Output the number of CPU cores in Python
[Python] Sort the list of pathlib.Path in natural sort
Match the distribution of each group in Python
Make a copy of the list in Python
Find the number of days in a month
Get only the subclass elements in a list
Search by the value of the instance in the list
[IOS] Change the display time for each frame of GIF animation in Pythonista3.
plot the coordinates of the processing (python) list and specify the number of times in draw ()
Differences in the behavior of each LL language when the list index is skipped
Compare the sum of each element in two lists with the specified value in Python
Python script to get a list of input examples for the AtCoder contest
Get the last element of the array by splitting the string in Python and PHP
How to get a list of files in the same directory with python
Get the sum of each of multiple columns with awk
Count the number of parameters in the deep learning model
[Python] Get the list of ExifTags names of Pillow library
Check for the existence of BigQuery tables in Java
[Python] Outputs all combinations of elements in the list
Python: Get a list of methods for an object
Date the name of the decomposed PDF for each page
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
Find a guideline for the number of processes / threads to set in the application server
Output the specified table of Oracle database in Python to Excel for each file
How to find the optimal number of clusters in k-means
Try to get the function list of Python> os package
Maya | Find out the number of polygons in the selected object
10. Counting the number of lines
Examine the margin of error in the number of deaths from pneumonia
Make a table of multiplication of each element in a spreadsheet (Python)
Get the value of a specific key up to the specified index in the dictionary list in Python
Analyzing data on the number of corona patients in Japan
Count the number of characters in the text on the clipboard on mac
[For beginners] I want to explain the number of learning times in an easy-to-understand manner.