0

I have to count the occurrences of words and display them by number of occurrences (in decreasing order). I can count the words but don't know how to proceed further. It may be I'm not doing it efficiently. If possible please suggest a solution.

import java.io.*;
import java.util.*;

public class MaxOccurence
{
    public static void main(String[] args)
    {
        Map<String, Integer> map = new HashMap<>();
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(new File(
                "F:/Demo/file_reading/bin/demo/hello.txt")));
            String str;
            while ((str = br.readLine()) != null)
            {
                Scanner sc = new Scanner(str);
                while (sc.hasNext())
                {
                    String word = sc.next();
                    if (map.containsKey(word))
                        map.put(word, map.get(word) + 1);
                    else
                        map.put(word, 1);
                }
            }
            System.out.println("yes");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        System.out.println(map);
    }
}
dave
  • 11,641
  • 5
  • 47
  • 65
Gaurav Rajput
  • 873
  • 3
  • 11
  • 19

2 Answers2

1

I think your approach of using the HashMap class with map.containsKey method is an efficient approach for the word count you solved so what is left is to order and print the map in a descending order, here is a clean and efficient example on how to achieve this:

map.entrySet().stream()
          .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
          .forEachOrdered(System.out::println);

Note that if you remove the Collection.reverseOrder call the order will be ascending.

Adding this to your code will look like this:

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class MaxOccurence {

  public static void main(String[] args) {

    Map<String, Integer> map = new HashMap<>();
    try {
        BufferedReader br = new BufferedReader(new FileReader(new File(
                "F:/Demo/file_reading/bin/demo/hello.txt")));
        String str;
        while ((str = br.readLine()) != null) {
            Scanner sc = new Scanner(str);
            while (sc.hasNext()) {
                String word = sc.next();
                if (map.containsKey(word))
                    map.put(word, map.get(word) + 1);
                else
                    map.put(word, 1);
            }
        }
        System.out.println("yes");
    } catch (IOException e) {
        e.printStackTrace();
    }
    map.entrySet().stream()
              .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
              .forEachOrdered(System.out::println);

  }

}
Diego López
  • 1,559
  • 1
  • 19
  • 27
1

Here is my code using tree map and comparator and gives correct output

import java.io.*;
import java.util.*;

public class DemoComprator {

public static void main(String[] args) {
    Map<String, Integer> map = new LinkedHashMap<>();

        try {
            BufferedReader br = new BufferedReader(new FileReader(new  File(
                    "/home/netiq/Documents/input.txt")));
            String str;
            while ((str = br.readLine()) != null) {
                Scanner sc = new Scanner(str);
                while (sc.hasNext()) {
                    String word = sc.next();
                    if (map.containsKey(word))
                        map.put(word, map.get(word) + 1);
                    else
                        map.put(word, 1);
                }

            }
           // System.out.println("yes");
        } catch (IOException e) {
            e.printStackTrace();
        }
     //   System.out.println(map);
        Comparator<String> comparator=new MyDemoComprator(map);
         Map<String,Integer> treemap=new TreeMap<String, Integer>(comparator);
        treemap.putAll(map);
     //   System.out.println(treemap);
        for(Map.Entry<String, Integer> entry:treemap.entrySet())
        {
            System.out.print(entry.getKey());
            //System.out.println(" :  "+entry.getValue());
            System.out.println();
        }

}

}

class MyDemoComprator implements Comparator<String>
    {
    Map<String,Integer> map;

   public MyDemoComprator(Map<String, Integer> map) {
      super();
       this.map = map;
    }

@Override
public int compare(String o1, String o2) {
    if(map.get(o1)>=map.get(o2))
        return -1;
    else
        return 1;
    //return 0;
}

}
Gaurav Rajput
  • 873
  • 3
  • 11
  • 19