按其出现的降序显示单词

时间:2015-04-17 21:51:06

标签: java find-occurrences

我必须计算单词的出现次数并按出现次数显示它们(按降序排列)。我可以数字但不知道如何继续前进。可能我没有有效地做到这一点。如果可能,请提出解决方案。

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);
    }
}

2 个答案:

答案 0 :(得分:1)

这是我使用树形图和比较器的代码,并提供正确的输出

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;
}

}

答案 1 :(得分:0)

我认为您使用HashMap类和map.containsKey方法的方法对于您解决的字数是一种有效的方法,所以剩下的就是按照降序排序和打印地图,这里如何实现这一目标是一个干净而有效的例子:

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

请注意,如果您移除Collection.reverseOrder来电,订单将会升序。

将此添加到您的代码中将如下所示:

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);

  }

}