英文:
How to sort a collection filled with objects in a descending order by objects state?
问题
以下是您要求的代码部分的中文翻译:
public class Word implements Comparable<Word> {
private String content;
private int frequency;
public Word(String content) {
this.content = content;
this.frequency = 1;
}
@Override
public int compareTo(Word o) {
return o.frequency - this.frequency; // 更改此行以实现降序排序
}
@Override
public String toString() {
return "" + content + " : " + frequency;
}
}
public class WordContainer {
static List<Word> list = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
Pattern p = Pattern.compile("\\S+");
while (!(line = reader.readLine()).equals("stop")) {
Matcher m = p.matcher(line);
while (m.find()) {
addon(new Word(m.group()));
}
}
// 将列表按频率降序排序
Collections.sort(list);
// 输出列表中的单词和频率
for (Word word : list) {
System.out.println(word);
}
}
public static void addon(Word word) {
for (Word w : list) {
if (w.getContent().equals(word.getContent())) {
w.setFrequency(w.getFrequency() + 1);
return; // 找到相同的单词后直接返回
}
}
list.add(word);
}
}
这个修改后的代码将列表中的单词按降序排列,以便频率高的单词在前面。然后,它会输出按降序排列的单词和它们的频率。
英文:
The input is:
-
asd 43 asdf asd 43
-
434 asdf
-
kasdf asdf stop asdf
-
stop
The output should be:
-
asdf : 4
-
43 : 2
-
asd : 2
-
434 : 1
-
kasdf : 1
-
stop : 1
On a line "stop" it should stop
I have this class:
public class Word implements Comparable<Word> {
private String content;
private int frequency;
public Word(String content) {
this.content = content;
this.frequency = 1;
}
@Override
public int compareTo(Word o) {
return this.frequency - o.frequency;
}
@Override
public String toString() {
return "" + content + " : " + frequency;
}
}
The second class Wordcontainer should have all the functionality:
public class WordContainer {
static List<Word> list = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
Pattern p = Pattern.compile("\\S+");
while (!(line = reader.readLine()).equals("stop")) {
Matcher m = p.matcher(line);
while (m.find()) {
addon(new Word(m.group()));
}
}
public static void addon(Word word) {
for (Word w : list) {
if (w.getContent().equals(word.getContent())) {
w.setFrequency(w.getFrequency() + 1);
}
}
list.add(word);
}
}
How do I fill this list with objects new Word so that the frequencies are in a descending order? Right now I get
-
asdf : 4
-
asdf : 3
-
43 : 2
-
asd : 2
-
asdf : 2
-
43 : 1
-
434 : 1
-
asd : 1
-
asdf : 1
-
kasdf : 1
-
stop : 1
答案1
得分: 1
以下是翻译好的内容:
这是您代码中的一个错误,可以通过修改 addon
方法来进行修复。
public static void addon(Word word) {
list.stream()
.filter(w -> w.getContent().equals(word.getContent()))
.findFirst()
.ifPresentOrElse(w -> w.setFrequency(w.getFrequency() + 1), () -> list.add(word));
}
您之前将单词添加到列表中,即使频率匹配/增加也是如此。
输出:
asdf: 4
asd: 2
43: 2
434: 1
kasdf: 1
stop: 1
英文:
It's a bug in your code, can be fixed by modifying just the addon
method.
public static void addon(Word word) {
list.stream()
.filter(w -> w.getContent().equals(word.getContent()))
.findFirst()
.ifPresentOrElse(w -> w.setFrequency(w.getFrequency()+1), () -> list.add(word));
}
You were adding the word to the list, even if the frequency is matched/increased.
Output:
asdf : 4
asd : 2
43 : 2
434 : 1
kasdf : 1
stop : 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论