CodeBlanks Logo Icon CodeBlanks

Search and filter

Write search and filtering with Stream.

Searching by title

As the blog grows, you need to find a specific post. A findByTitle(String keyword) method walks the posts list and collects posts whose title contains keyword into a new list.

Java's String.contains(keyword) checks for a substring. If title.contains("Java") returns true, we add that post to the result. The search is case-sensitive; for case-insensitive search use toLowerCase().

Collecting results in a new list

Inside the search method, you create an empty List<Post> result = new ArrayList<>();. The loop checks each post, and matching ones are added with result.add(post). After the loop, return result; gives back the list.

This pattern is the simplest way to filter without changing the original list. The Stream API can also do it, but we focus on the classic loop here.

Practice Add the findByTitle method

Add a List<Post> findByTitle(String keyword) method to the Blog class. Inside, create a List<Post> result = new ArrayList<>();, loop with for (Post post : posts), check post.title.contains(keyword), add matches with result.add(post), and return result;. In main, add two posts (one with "Java" in the title), then print the size() of blog.findByTitle("Java").

Starter code

Main.java
import java.util.List;
import java.util.ArrayList;

class Post {
    String title;
    String author;
    Post(String title, String author) {
        this.title = title;
        this.author = author;
    }
}

class Comment {
    String author;
    String text;
    Comment(String author, String text) {
        this.author = author;
        this.text = text;
    }
}

class Blog {
    List<Post> posts = new ArrayList<>();
    void addPost(Post post) { posts.add(post); }
}

// Add the findByTitle method to Blog

// main: add two posts, search "Java" and print the match count

Sample solution

Main.java
class Post {
    String title;
    String author;
    Post(String title, String author) {
        this.title = title;
        this.author = author;
    }
}

class Comment {
    String author;
    String text;
    Comment(String author, String text) {
        this.author = author;
        this.text = text;
    }
}

class Blog {
    List<Post> posts = new ArrayList<>();
    void addPost(Post post) { posts.add(post); }
    List<Post> findByTitle(String keyword) {
        List<Post> result = new ArrayList<>();
        for (Post post : posts) {
            if (post.title.contains(keyword)) {
                result.add(post);
            }
        }
        return result;
    }
}

public class Main {
    public static void main(String[] args) {
        Blog blog = new Blog();
        blog.addPost(new Post("Java intro", "Ayşe"));
        blog.addPost(new Post("Other", "Mehmet"));
        System.out.println(blog.findByTitle("Java").size());
    }
}

Expected output

1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the substring check

Question
if (post.title.____(keyword)) {
    result.add(post);
}
Answer
if (post.title.contains(keyword)) {
    result.add(post);
}

Explanation: contains checks for a substring.

2 When does title.contains("Java") return true?

  • When title equals Java exactly
  • When title contains the Java substring Correct answer
  • When title starts with Java
  • When title is the empty string

Explanation: contains checks for a substring; equality or prefix is not required.

3 Match the search concepts

Term Meaning
contains Substring check
findByTitle Search by title method
result List of matching posts
toLowerCase Case-insensitive search

All lessons in Intermediate