Java Collections frequency

Description

Java Collections frequency

import java.util.ArrayList;
import java.util.Collections;

public class Main {
  public static void main(String[] args) {
    String[] langs = { "CSS", "SQL", "HTML", "C++" };
    ArrayList<String> list2 = new ArrayList<>();

    list2.add("Java");
    list2.add("Javascript");
    list2.add("C++");

    Collections.addAll(list2, langs);

    System.out.println(list2);//from  w  w w .  j a v  a 2 s .  c  o m
    // get frequency of "red"
    int frequency = Collections.frequency(list2, "C++");
    System.out.printf("%nFrequency of red in list2: %d%n", frequency);
  }
}



PreviousNext

Related