简体   繁体   English

如何在不使用比较器的情况下按在 Java 中遇到 substring 的次数对对象列表进行排序?

[英]How to sort a list of objects by number of times a substring is encountered in Java without using Comparator?

I have a Post class containing strings of text and titles and another class called Blog containing a List of Posts.I have created a method sorting all the posts containing a specified String and putting them into another List, but I can't figure out how to sort the second list by how many times each string occurs.我有一个帖子 class 包含文本和标题字符串,另一个 class 名为博客包含帖子列表。我创建了一个方法,对包含指定字符串的所有帖子进行排序并将它们放入另一个列表,但我不知道如何按每个字符串出现的次数对第二个列表进行排序。 I am not allowed to use the comparator class.我不允许使用比较器 class。

public class Post {
    private static int serialID = 0;
    private String title;
    private String text;
    Post(String title,String text){
        this.serialID++;
        this.text = text;
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public String getTitle() {
        return title;
    }

    boolean checkIfContainedInTitle(String string){
        boolean isFound = title.contains(string);
        return isFound;
    }

    int checkTimesEncountered(String string){
        int M = string.length();
        int N = text.length();
        int res = 0;

        for (int i = 0; i <= N - M; i++) {

            int j;
            for (j = 0; j < M; j++) {
                if (text.charAt(i + j) != string.charAt(j)) {
                    break;
                }
            }
            if (j == M) {
                res++;
                j = 0;
            }
        }
        return res;
    }
}

How do I sort the strings so that I print the Post titles with the most frequently encountered substring?如何对字符串进行排序,以便打印最常遇到的 substring 的帖子标题?

public class Blog {
    private List<Post> postList;

    Blog(List<Post> postList){
        this.postList = postList;
    }

    void sortByTimesEncountered(List<Post> postList){
        List<Post> sortedPosts = null;
        for (Post post : postList){

        }
    }

    void printTitlesConatiningString(String string){ // this is the method I am struggling with
        List<Post> postList1 = null;
        List<Integer> postIndex = null;
        for(Post post : postList){
            if(post.getText().contains(string)){
                postList1.add(post);
            }
        }
        for(Post post : postList1){

        }


    }
}

You can use the Java-Stream-API and StringUtils from org.apache.commons.lang3 :您可以使用org.apache.commons.lang3中的Java-Stream-APIStringUtils

Your Method would look like this:您的方法如下所示:

void printTitlesConatiningString(String string){ 
    List<Post> postList1 = postlist.stream()
      .filter(p -> p.getText().contains(string)) // Collect all Posts that contains the string
      .sorted(Comparator.comparingInt(p -> StringUtils.countMatches(p.getText(), string))) // Sorts them by how many times each string occurs
      .collect(Collectors.toList()); // Collect them to a new List
}


EDIT:编辑:
OP has indicated that he has/would like to solve this problem without using Comparator . OP 表示他已经/希望在不使用Comparator的情况下解决这个问题。 In this case I would extend the class post with a method to compare two posts.在这种情况下,我将使用比较两个帖子的方法扩展 class 帖子。

private class Post{

    ...

    public int compareByStringCount(Post postToCompare, String string){
        return Integer.compare(StringUtils.countMatches(this.getText(), string), StringUtils.countMatches(postToCompare.getText(), string));
    }
}

the printTitlesConatiningString -Method kann use this new method to compare two posts. printTitlesConatiningString kann 使用这个新方法来比较两个帖子。 It would look like this:它看起来像这样:

void printTitlesConatiningString(String string){ 
    List<Post> postList1 = postlist.stream()
      .filter(p -> p.getText().contains(string)) // Collect all Posts that contains the string
      .sorted((p1, p2) -> p1.compareByStringCount(p2, s)) // Sorts them by how many times each string occurs
      .collect(Collectors.toList()); // Collect them to a new List
}

Personal note: as a matter of fact Comparator is still used here.个人说明:事实上Comparator仍然在这里使用。 But another solution is in my eyes much too complicated and not up to date.但在我看来,另一种解决方案过于复杂且不是最新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM