Get Most Common Elements From ArrayListCompute the most common elements from an ArrayList, and then display elements sorted by their frequencies.
Java
Common elements. Suppose we have an ArrayList of Strings that may contain duplicate elements, and we want to find the most common one. We can do this by sorting the elements by their frequencies.
ArrayList
With a HashMap, we can determine how many times each unique string appears in the ArrayList. And then with that data, we can sort based on frequency counts.
Example. First we must import several types into the Java program. In main() we call into 2 custom functions, getFrequencies() and displaySortedFrequencies().
Step 1 We convert our String array into an ArrayList, as most Java programs will store their data in ArrayList instances.
Step 2 We call getFrequencies(), which loops over the Strings and increments a frequency count for each string in a HashMap.
for
HashMap
Step 3 DisplaySortedFrequencies converts the HashMap of frequencies to an ArrayList, and then sorts it by the counts in descending order.
Convert HashMap, ArrayList
Sort HashMap
Lambda
import java.util.ArrayList; import java.util.Collections; import java.util.Map.Entry; import java.util.HashMap; public class Program { public static void main(String[] args) { String[] values = { "bird", "cat", "bird", "dog", "bird", "man", "frog", "cat" }; // Step 1: get list of elements. ArrayList<String> valuesList = new ArrayList<>(); Collections.addAll(valuesList, values); // Step 2: add up frequencies of all elements in list. var freqs = getFrequencies(valuesList); // Step 3: sort by frequencies and display elements. displaySortedFrequencies(freqs); } static HashMap<String, Integer> getFrequencies(ArrayList<String> values) { HashMap<String, Integer> result = new HashMap<>(); for (String value : values) { // Increment frequency starting at 0 if not found. int frequency = result.getOrDefault(value, 0); result.put(value, frequency + 1); } return result; } static void displaySortedFrequencies(HashMap<String, Integer> frequencies) { // Convert to array list. ArrayList<Entry<String, Integer>> array = new ArrayList<>(); array.addAll(frequencies.entrySet()); // Sort elements. // Compare in reverse order for descending sort. Collections.sort(array, (a, b) -> Integer.compare(b.getValue(), a.getValue())); // Display all results in order. for (Entry<String, Integer> entry : array) { System.out.println(entry.getKey() + " " + entry.getValue()); } } }
bird 3 cat 2 frog 1 man 1 dog 1
Results. We can determine that the string "bird" occurred most often in the input String array with 3 instances. And other strings like "dog" only occurred once.
By combining several concepts, and converting a HashMap to an ArrayList, we can determine the most common elements in a list. This can be useful for simple data analysis in Java programs.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.