java 如何根据字符串数组中出现的单词对Map进行排序

vbopmzt1  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(135)

我正在写一个java程序逻辑,用于打印带有出现次数和行号的wrods。下面是代码

package test;
import java.util.HashMap;
 import java.util.Scanner;
 import java.util.Set;

 public class Countcharacters {

/**
 * @param args
 */
static HashMap<String, Integer> countcharact=new HashMap<>();
static HashMap<String, String> linenumbertrack=new HashMap<>();
static int count=1;
static void countwords(String line){
    //System.out.println(line);
    String[] input=line.split("\\s");
    int j=0;
    String linenumber="";
    for(int i=0;i<input.length;i++){
        //System.out.println(input[i]);
        if(countcharact.containsKey(input[i])==true){
            j=countcharact.get(input[i]);
            linenumber=linenumbertrack.get(input[i]);
            countcharact.put(input[i],j+1);
            linenumbertrack.put(input[i],linenumber+", "+count);

        }
        else{
            countcharact.put(input[i], 1);
            linenumbertrack.put(input[i],count+"" );
        }

    }
    count++;

}
public static void main(String[] args) {
    // TODO Auto-generated method stub
   String inp="the quick brown fox jumped over the lazy dog's bowl.\nthe dog was angry with the fox for considering him lazy.";
   String[] line=inp.split("\n");
   for(int i=0;i<line.length;i++){
       Countcharacters.countwords(line[i]);
   }
    Set<String> s=countcharact.keySet();
    for(String c:s){
        System.out.println(c+" "+countcharact.get(c)+" "+"["+linenumbertrack.get(c)+"]");
    }

}

}

我得到的输出是

over 1 [1]
quick 1 [1]
lazy. 1 [2]
lazy 1 [1]
considering 1 [2]
jumped 1 [1]
was 1 [2]
for 1 [2]
angry 1 [2]
brown 1 [1]
him 1 [2]
fox 2 [1, 2]
the 4 [1, 1, 2, 2]
with 1 [2]
bowl. 1 [1]
dog's 1 [1]
dog 1 [2]

但我有两个问题。
第1次:如果您看到"the"出现次数为4,但行号为[1,1,2,2],而应该仅为[1,2]。
2nd:我想对它们进行排序,应该先按基数降序排序,然后按字母顺序排序。
就像这样:

the 4 [1,2]
fox 2 [1,2]
lazy 2 [1,2]
angry 1 [1]
bowl 1 [1]
.
.
kwvwclae

kwvwclae1#

最好是抽象出类中数据的逻辑单元,在你的问题中你有两个清晰的单元:
1.单词出现(单词串和行号)。

class WordOccurrence {
         private final String word;
         private final int lineNumber;

         ...
     }

1.有关单词的统计信息(出现次数、出现的行号集等)。

class WordStats {
         private List<Word> occurrences;

         public String getWord() { ... }
         public int getCount() { ... }
         public Set<Integer> getLines() { ... }
     }

使用这些类,您可以首先将text分解为ListWordOccurrenceMap;因此,对于每个不同的字,Map将包含具有以下内容的条目:
1.等于实际String字的键
1.值等于List,其中包含WordOccurrence对象,用于text中的每个匹配项
您可以通过以下方式实现此目的:

public static Map<String, List<WordOccurrence>> createOccurrencesMap(String text) {
        text = text.replaceAll("\\.", " ");
//      text = text.replaceAll("'s", ""); // dog's != dog ???
        Map<String, List<WordOccurrence>> result = new HashMap<>();
        String[] lines = text.split("\n");
        for (int i = 0; i < lines.length; i++)
            for (String word : lines[i].split("\\s+")) 
                result.computeIfAbsent(word, w -> new ArrayList<>())
                            .add(new WordOccurrence(word, i + 1));
        
        return result;
    }

然后,你可以很容易地将这个Map转换成WordStatsList(使用灵活的参数化标准排序),如下所示:

List<WordStats> createStats(String text, Comparator<WordStats> sortingCriteria) {
        return createOccurrencesMap(text).values().stream()
                .map(WordStats::new)
                .sorted(sortingCriteria)
                .collect(Collectors.toList());
    }

一旦你把你的问题分解成更小的直观的逻辑分组组件(类、方法、数据结构等),剩下的唯一事情就是把它们连接起来。
以下代码是此解决方案的完整工作演示,供您使用:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class CountWords {

    public static void main(String[] args) {
        String text = "the quick brown fox jumped over the lazy dog's bowl.\nthe dog was angry with the fox for considering him lazy.";
        Comparator<WordStats> sortingCriteria = Comparator
                .comparing(WordStats::getCount).reversed()
                .thenComparing(WordStats::getWord);

        createStats(text, sortingCriteria).forEach(System.out::println);
    }

    public static List<WordStats> createStats(String text, Comparator<WordStats> sortingCriteria) {
        return createOccurrencesMap(text).values().stream()
                .map(WordStats::new)
                .sorted(sortingCriteria)
                .collect(Collectors.toList());
    }
    
    public static Map<String, List<WordOccurrence>> createOccurrencesMap(String text) {
        text = text.replaceAll("\\.", " ");
//      text = text.replaceAll("'s", ""); // dog's != dog ???
        Map<String, List<WordOccurrence>> result = new HashMap<>();
        String[] lines = text.split("\n");
        for (int i = 0; i < lines.length; i++)
            for (String word : lines[i].split("\\s+")) 
                result.computeIfAbsent(word, w -> new ArrayList<>())
                            .add(new WordOccurrence(word, i + 1));
        
        return result;
    }
    
    static class WordStats {
        private List<WordOccurrence> occurrences;

        public WordStats(List<WordOccurrence> words) {
            this.occurrences = words;
        }
        
        public String getWord() {
            return occurrences.get(0).getWord();
        }

        public int getCount() {
            return occurrences.size();
        }
        
        public Set<Integer> getLines() {
            return occurrences.stream().map(WordOccurrence::getLineNumber).collect(Collectors.toSet());
        }
        
        public String toString() {
            return String.format("%s %d %s", getWord(), getCount(), getLines());
        }
    }
    
    static class WordOccurrence {
        private final String word;
        private final int lineNumber;

        public WordOccurrence(String word, int lineNumber) {
            this.word = word;
            this.lineNumber = lineNumber;
        }

        public String getWord() {
            return word;
        }

        public int getLineNumber() {
            return lineNumber;
        }
        
        public String toString() {
            return word + "@" + lineNumber;
        }
    }
}

Complete code on GitHub

相关问题