Java Collections frequency count character frequency

Description

Java Collections frequency count character frequency

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
   public static void main(String args[]) throws Exception {
      List<Character> l = new ArrayList<>();
      FileInputStream fis = new FileInputStream(new File(args[0]));
      while (fis.available() > 0)
         l.add((char) fis.read());
      fis.close();/* w w  w  .  ja v  a  2 s. c om*/
      
      for (char c = 'A'; c <= 'z'; c++) {
         System.out.println(c + " " + Collections.frequency(l, c));
      }
   }
}



PreviousNext

Related