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

The configuration shown below should be used only in a development environment and not in production.

After enabling Spring Security in your Spring Boot application, you will not be able to access the H2 database console anymore.

With its default settings under Spring Boot, Spring Security will block access to the H2 database console.

To be able to access the H2 database console under Spring Security you need to change these four things:

  • Enable h2 console in your file
  • Allow all access to the URL path /console/*.
  • Disable CRSF (Cross-Site Request Forgery). By default, Spring Security will protect against CRSF attacks.
  • Since the H2 database console runs inside a frame, you need to disable this in Spring Security.

So, the very, very simple security configuration that allows you to access h2-console should look like:

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    protected void configure(HttpSecurity httpSecurity) throws Exception {



After applying these changes and trying to access your h2-console


You should see the h2-console login page:

Docker install on Linux Mint 19 Tara

When installing a Docker on Linux Mint you can experience an error saying that Docker doesn’t have a release file for your distribution.

E: The repository ' tara Release' does not have a Release file.

It’s happen because the Docker repository at doesn’t know about Linux Mint’s code name tara.

To solve this issue you have to provide Ubuntu code name for which the Docker is providing a repository. And this replacement will work because Linux Mint is built on top of Ubuntu.

So, to add the Docker repository to your Mint’s distribution you have to use:

sudo add-apt-repository "deb [arch=amd64] $(. /etc/os-release; echo "$UBUNTU_CODENAME") stable"

In this step, we are simply replacing the Linux Mint codename (returned by lsb_release -cs) with the Ubuntu base codename (returned by . /etc/os-release; echo "$UBUNTU_CODENAME").

Project Lombok

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

Project Lombok webpage:

How Lombok Works

  • Hooks in via the Annotation processor API
  • The AST (raw source code) is passed to Lombok for code generation before java continues.
  • Thus, produces properly compiled Java code in conjunction with the Java compiler

Project Lombok and IDEs

  • Since compiled code is changed, and source files are not, IDE’s can get confused by this.
  • Modern IDEs such as IntelliJ, Eclipse, Netbeans support Project Lombok
  • Plugin Installation may be necessary

Project Lombok Features

  • @Getter – Creates getter methods for all properties
  • @Setter – Creates setter for all non-final properties
  • @ToString – Generates String of classname, and each field separated by commas; Optional parameter to include field names; Optional parameter to include the call to the super toString method
  • @EqualsAndHashCode – Generates implementations of ‘equals(Object other) and hashCode(); By default will use all non-static, non-transient properties; Can optionally exclude specific properties.
  • @NoArgsConstructor – Generates no-args constructor; Will cause compiler error if there are final fields; Can optionally force, which will initialize final fields with 0 / false / null
  • @RequiredArgsContructor – Generates a constructor for all fields that are final or marked @NonNull; Constructor will throw a NullPointerException if any @NonNull fields are null.
  • @Data – Generates typical boilerplate code for POJOs, Combines – @Getter, @Setter, @ToString, @EqualsAndHashCode, @RequiredArgsConstructor; No constructor is generated if constructors have been explicitly declared.
  • @Value – The immutable variant of @Data; All fields are made private and final by default.
  • @NotNull – Set on a parameter of method or constructor and a NullPointerException will be thrown if the parameter is null.
  • @Builder – Implements the ‘builder’ pattern for object creation.
  • @SneakyThrows – Throw checked exceptions without declaring in calling method’s throws clause.
  • @Syncronized – A safer implementation of Java’s synchronized
  • @Log – Creates a Java util logger.
  • @Slf4j – Creates a SLF4J logger.

Spring Boot Run Dashboard

Intellij Idea Ultimate 2018.03. I thought that Spring Boot dashboard will be visible and I’ll be able to run it after adding devtools dependency to the pom (in my case) file.


But seems that there is a little work involved to be able to run your spring boot applications by Spring Boot Run Dashboard.

In the Run/Debug Configurations Popup, you should have “Spring Boot” and also “Templates”.

So, click on the “+” (Alt+Insert), find Spring Boot on the list and add it.

Now, we can use devtools functionality in Spring Boot Run Dashboard.

Interface Segregation Principle

Clients should not be forced to depend upon interfaces that they do not use.

  • Make fine grained interfaces that are client specific
  • Many client specific interfaces are better than one “general purpose” interface
  • Keep your components focused and minimize dependencies between them
  • Notice relationship to the Single Responsibility Principle?
  • avoid ‘god’ interfaces

Liskov Subsitution Principle

If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction.

  • By Barbara Liskov, in 1998
  • Objects in a program would be replaceable with instances of their subtypes WITHOUT altering the correctness of the program.
  • Violations will often fail the “Is a” test.
  • A Square “Is a” Rectangle
  • However, a Rectangle “Is Not” a Square

Open/Closed Principle

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.

  • Your classes should be open for extension
  • But closed for modification
  • You should be able to extend a classes behavior, without modifying it.
  • Use private variables with getters and setters – ONLY when you need them.
  • Use abstract base classes