import java.util.Collections; import java.util.List; import java.util.Arrays; public class FrequencyExample { public static void main(String[] args) { Listlist = Arrays.asList("apple", "banana", "orange", "banana"); int frequency = Collections.frequency(list, "banana"); System.out.println("Frequency of banana: " + frequency); } }
import java.util.Collections; import java.util.ArrayList; public class FrequencyExample { public static void main(String[] args) { ArrayListThis example uses the frequency() method to determine how many times the integer 5 appears in the ArrayList. The Collections frequency() method belongs to the java.util package library.list = new ArrayList (); list.add(5); list.add(3); list.add(5); list.add(7); list.add(5); int frequency = Collections.frequency(list, 5); System.out.println("Frequency of 5: " + frequency); } }