Stream filter() in Java with examples


Stream<T> filter(Predicate<? super T> predicate)

Returns a stream consisting of the elements of this stream that match the given predicate.

To see how filter() works, let’s create a Player class:

public class Player {

    private String name;
    private int points;
    private boolean vip;
    //Constructor and standard getters

and create some data to play with:

Player peter = new Player("Peter Parker", 15, false);
Player sarah = new Player("Sarah Johnes", 200, true);
Player charles = new Player("Charles Chaplin", 150, false);
Player mary = new Player("Mary Poppins", 1, true);

List<Player> players = Arrays.asList(peter, sarah, charles, mary);

So, for example if we want to see only VIP players, before Java 8 our filter of players would looks like:

List<Player> vipPlayersJava7 = new ArrayList<>();
for (Player p : players) {
    if (p.isVip()) {

How this can be done with Java 8 ? It is just a matter of single line as follows.

List<Player> vipPlayersJava8 =
.filter(v -> v.isVip())

We have passed a Predicate instance into the filter() method in the form of a Lambda expression.

We can also use a method reference, which is shorthand for a lambda expression:

List<Player> vipPlayerJava8MethodRef =

Also, we can use multiple conditions with filter(). For example, filter by VIP status and name:

List<Player> sarahAndVip =
        .filter(p -> p.getName().startsWith("Sarah") && p.isVip())

Spring Boot Security and H2 console

Fatal error: Uncaught Error: Call to a member function id() on array in /home/customer/www/ Stack trace: #0 /home/customer/www/ CrayonFormatter::format_code('', Array, Object(CrayonHighlighter)) #1 [internal function]: CrayonFormatter::delim_to_internal(Array) #2 /home/customer/www/ preg_replace_callback('#()#msi', 'CrayonFormatter...', 'spring.h2.conso...') #3 /home/customer/www/ CrayonFormatter::format_mixed_code('spring.h2.conso...', Object(CrayonLang), Object(CrayonHighlighter)) #4 /home/customer/www/ in /home/customer/www/ on line 36