JUnit 5 is JUnit next generation; it requires Java 8 or above since it was born as a JUnit Lambda project, that project wanted to take full advantage of Java 8 new features, especially lambda expressions.
Assertions Assumptions Conditions Parameterized Tests Test Execution Order Using Gradle, you need to create a build.gradle file with the following structure:
plugins { id 'java' } def junitJupiterVersion = '5.9.2' repositories { mavenCentral() } dependencies { testImplementation "org.
[Read More]
Lambda Expressions
Since Java 8 we have Lambda expressions which is a function defined under some interface contract and the main purpose is to implement functional programming in Java. Basically lambda expression is an argument list follow by an arrow token and a body or code expression.
Example 1:
(x, y) -> x + y; Example 2:
() -> "Hello World!"; Example 3:
x -> x.toLowerCase(); Lambdas should be an expression, not a narrative
[Read More]
Functional Interfaces
A functional interface in Java is any with @FunctionalInterface annotation and with SAM(Single Abstract Method). It was introduced to facilitate Lambda expressions. Since a lambda function can only provide the implementation for one method, it is mandatory for the functional interfaces to have only one abstract method. Java has defined a lot of functional interfaces in java.util.function package. Some of them are Consumer, Supplier, Function and Predicate.
Basic Functional Interfaces Special Functional Interfaces Consumer has an accept(Object) method and represents an operation that accepts a single input argument and returns no result.
[Read More]
Generics
Generics are a great way to prevent bugs at runtime since they provide compile-time type checking and promote cast elimination. Another important advantage of using generics is that you can write code that uses abstractions instead of concrete classes, so you can create maintainable and extendable code.
The following example shows how we can print a collection containing different types using generics:
import java.util.List; import java.util.Arrays; public class ListPrinter { private <T> void printList(List<T> collection){ for(T item: collection){ System.
[Read More]
Executors
Executors are part of concurrent Java API, and the intention is to have a high-level managing thread; therefore, executors typically manage a pool of threads.
ExecutorService executor = Executors.newFixedThreadPool(3); In the previous code, we created a thread pool with three threads of capacity. It would be best if you stopped executors at some point; otherwise, they will keep listening for new tasks; the usual way to stop it is:
executor.shutdown(); Here is a simple executor example:
[Read More]
Stream Collectors
This time, I will show you how to use collectors along with streams to group by, concatenate, map, and list.
Example: Convert a list of elements to a string separated by ‘,’
import java.util.List; import java.util.stream.Collectors; public class StreamToStringConverter { private String parse(){ return List.of("Java", "C++", "Lisp", "Haskell"). stream().collect(Collectors.joining(",")); } public static void main(String[] args){ var result = new StreamToStringConverter().parse(); assert "Java,C++,Lisp,Haskell".equals(result); } } From a list, generate another list selecting elements if the string length equals four.
[Read More]
Stream Filters
A stream represents a sequence of elements and supports different operations to perform calculations in those elements:
import java.util.List; import java.util.Arrays; import java.util.stream.Collectors; public class StringFilter { private List<String> parse(){ return Arrays.asList("Java", "C++", "Lisp", "Haskell"). stream().filter( p -> p.startsWith("J")).toList(); } public static void main(String[] args){ var result = new StringFilter().parse(); assert 1 == result.size(); assert "Java" == result.get(0); } } After creating a Stream of Strings, we filtered by those starting with the letter J.
[Read More]
Streams
The stream interface is defined in the java.util.stream package. Since Java 8, the stream API will be used to process collections. Streams support aggregate operations. The common aggregate operations are filter, map, reduce, find, match, and sort.
The map method maps each element to its corresponding result. The following Java code uses the map to get squares of numbers.
import java.util.List; import java.util.Arrays; import java.util.ArrayList; import java.util.stream.Collectors; public class MapSquares { private List<Integer> square(List<Integer> numbers){ return numbers.
[Read More]
Java
There are three main features in Java 8 that I love, one of them is the lambda expressions, it will have enormous implications for simplifying development. The another one is the new java.util.stream package provide a Stream API to support functional-style operations on streams of elements. The Stream API is integrated into the Collections API, which enables bulk operations on collections, such as sequential or parallel map-reduce transformations. And lastly but not least the java.
[Read More]
Java Time API
Some important things that we have to know about the java.time API is the following:
Is an evolution from the previous java.util.Date (Calendar, TimeZone & DateFormat) Instances of time/date now are immutable. (This is importat for lambda expressions) Time and Date operations now are thread safe. The API support strong typing, which enables you to write better code. A quick glance to the Date methods
import java.time.*; public class DateOperations { private void operations() throws Exception { LocalDate localDate = LocalDate.
[Read More]