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. I am not allowed to use the comparator 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?
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){
}
}
}