如何按对象状态降序对填充有对象的集合进行排序?

huangapple go评论134阅读模式
英文:

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&lt;Word&gt; {
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 &quot;&quot; + content  + &quot; : &quot; + frequency;
}
}

The second class Wordcontainer should have all the functionality:

public class WordContainer {
static List&lt;Word&gt; list = new ArrayList&lt;&gt;();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
Pattern p = Pattern.compile(&quot;\\S+&quot;);
while (!(line = reader.readLine()).equals(&quot;stop&quot;)) {
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 -&gt; w.getContent().equals(word.getContent()))
.findFirst()
.ifPresentOrElse(w -&gt; w.setFrequency(w.getFrequency()+1), () -&gt; 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

huangapple
  • 本文由 发表于 2020年8月18日 14:57:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63463343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定