gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:20:45 25 4
gpt4 key购买 nike

我正在编写一个 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]。

第二:我想对它们进行排序。它应该首先按基数降序排序,然后按字母顺序排序。

像这样:

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

最佳答案

最好在类中抽象出数据的逻辑单元。在您的问题中,您有两个明确的单位:

  1. 单词出现次数(单词字符串和行号)。

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

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

         class WordStats {
    private List<Word> occurrences;

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

使用这些类,您可以首先将您的文本分解为WordOccurrenceListMap;所以对于每个不同的单词,Map 将包含一个条目:

  1. 键等于实际的String
  2. 值等于 List,其中包含 WordOccurrence 对象,用于在 text
  3. 中出现的每个对象

您可以通过以下方式实现此目的:

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

然后您可以轻松地将此映射转换为 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

希望这对您有所帮助。

关于java - 如何根据字符串数组中的单词出现对 map 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56205718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com