簡體   English   中英

如何在不使用比較器的情況下按在 Java 中遇到 substring 的次數對對象列表進行排序?

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

我有一個帖子 class 包含文本和標題字符串,另一個 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;
    }
}

如何對字符串進行排序,以便打印最常遇到的 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){

        }


    }
}

您可以使用org.apache.commons.lang3中的Java-Stream-APIStringUtils

您的方法如下所示:

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
}


編輯:
OP 表示他已經/希望在不使用Comparator的情況下解決這個問題。 在這種情況下,我將使用比較兩個帖子的方法擴展 class 帖子。

private class Post{

    ...

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

printTitlesConatiningString kann 使用這個新方法來比較兩個帖子。 它看起來像這樣:

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
}

個人說明:事實上Comparator仍然在這里使用。 但在我看來,另一種解決方案過於復雜且不是最新的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM