0

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

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

2 回答 2

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

}
于 2015-04-20T10:01:04.787 回答
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);

  }

}
于 2015-04-17T22:12:46.070 回答