Getting Error in the Following Code of Implementing Frequency

Viewed 76

Hello EveryOne I want to Sort the text of a file according to their frequency.I am getting Error at runtime outofmemory Error.Please Give response as soon as possible.

import java.io.*; 

import java.util.*;

import java.util.Collections;   

class SortedInAlphaOrder

 {

public static void main(String a[]) throws IOException{

          Scanner input = new Scanner(System.in);

          System.out.println("Enter the filename with path : " );

          //String filename = input.next();

          File f = new File("Ashish.txt");

          BufferedReader br = new BufferedReader(new FileReader(f));

          StringBuffer buffer = new StringBuffer();

          String str =br.readLine();

          while((str!= null)){

          buffer.append(str);

          buffer.append(" ");

          }

          ArrayList<String> list = new ArrayList<String>();


       StringTokenizer st = new StringTokenizer(buffer.toString().toLowerCase());

          while(st.hasMoreTokens()){

                String s = st.nextToken();

                list.add(s);

                }

            HashSet<String> set = new HashSet<String>(list);

            List<String> arrayList = new ArrayList<String>(set);

            Collections.sort(arrayList);

            for(Object ob : arrayList){

                    System.out.println(ob.toString());

                }

            }           }

Getting Error OutofMemoryHeap Error.Help me out.

2 Answers
Related