按出现的降序显示单词

Display words in decreasing order of their occurance

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

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

我认为您使用 HashMap class 和 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);

  }

}

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

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

}