Java 8 BiPredicate Examples

In Java 8, BiPredicate is a functional interface, which accepts two arguments and returns a boolean, basically this BiPredicate is same with the Predicate, instead, it takes 2 arguments for the test. @FunctionalInterface public interface BiPredicate<T, U> { boolean test(T t, U u); } Further Reading Java 8 Predicate Examples 1. BiPredicate Hello World. If …

Read more

Java 8 Predicate Examples

In Java 8, Predicate is a functional interface, which accepts an argument and returns a boolean. Usually, it used to apply in a filter for a collection of objects. @FunctionalInterface public interface Predicate<T> { boolean test(T t); } Further Reading Java 8 BiPredicate Examples 1. Predicate in filter() filter() accepts predicate as argument. Java8Predicate.java package …

Read more

Java 8 Stream findFirst() and findAny()

In Java 8 Stream, the findFirst() returns the first element from a Stream, while findAny() returns any element from a Stream. 1. findFirst() 1.1 Find the first element from a Stream of Integers. Java8FindFirstExample1.java package com.mkyong.java8; import java.util.Arrays; import java.util.List; import java.util.Optional; public class Java8FindFirstExample1 { public static void main(String[] args) { List<Integer> list = …

Read more

JUnit 5 Tagging and Filtering, @Tag examples

This article shows you how to use the JUnit 5 tagging and filtering via the @Tag annotation. Tested with JUnit 5.5.2 Maven 3.6.0 Gradle 5.6.2 1. @Tag A simple tagging for demo. TagMethodTest.java package com.mkyong.tags; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @Tag("branch-20") public class TagMethodTest { @Test @Tag("feature-168") void test1Plus1() { assertEquals(2, 1 + …

Read more

Java – How to search a string in a List?

In Java, we can combine a normal loop and .contains(), .startsWith() or .matches() to search for a string in ArrayList. JavaExample1.java package com.mkyong.test; import java.util.ArrayList; import java.util.List; public class JavaExample1 { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Kotlin"); list.add("Clojure"); list.add("Groovy"); list.add("Scala"); List<String> result = new ArrayList<>(); for (String s …

Read more

Java 8 – Convert a Stream to List

A Java 8 example to show you how to convert a Stream to a List via Collectors.toList Java8Example1.java package com.mkyong.java8; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Java8Example1 { public static void main(String[] args) { Stream<String> language = Stream.of("java", "python", "node"); //Convert a Stream to List List<String> result = language.collect(Collectors.toList()); result.forEach(System.out::println); } } output …

Read more

Java 8 – Filter a null value from a Stream

Review a Stream containing null values. Java8Examples.java package com.mkyong.java8; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Java8Examples { public static void main(String[] args) { Stream<String> language = Stream.of("java", "python", "node", null, "ruby", null, "php"); List<String> result = language.collect(Collectors.toList()); result.forEach(System.out::println); } } output java python node null //

Java 8 Streams filter examples

In this tutorial, we will show you few Java 8 examples to demonstrate the use of Streams filter(), collect(), findAny() and orElse() 1. Streams filter() and collect() 1.1 Before Java 8, filter a List like this : BeforeJava8.java package com.mkyong.java8; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class BeforeJava8 { public static void main(String[] args) …

Read more

How to register a servlet filter in Spring MVC

In a nutshell, a servlet filter lets you intercepts requests and responses on your web application. This article shows you how to register a servlet filter in Spring XML and JavaConfig. 1. Servlet Filter Review the following custom filter, it will catch any exceptions and redirect to an error page. package com.mkyong.form.web; import java.io.IOException; import …

Read more