Negating a Predicate in Java
Java 8 predicate negate() returns a predicate that is the logical negation of this predicate. Java 11 introduced not() which is also the same.
Java 8 predicate negate() returns a predicate that is the logical negation of this predicate. Java 11 introduced not() which is also the same.
Learn to use Stream.filter(Predicate) method to traverse all the elements and filter all items which match a given condition through Predicate argument. 1. Stream filter() Method The stream() method syntax is as follows: Predicate is a functional interface and represents the condition to filter out the non-matching elements from the …
Learn to use Stream.forEachOrdered(Consumer action) method to traverse all the elements and performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
Learn to use Stream forEach(Consumer action) method to traverse all the elements of stream and performs an action for each element of this stream.
Learn to create Streams of primitives and objects in Java using some most popular ways. We will learn to create finite as well as infinite streams.
In this spring webflux websocket example, Learn to create reactive applications which support persistent websocket connection between a client and server.
In this spring webflux tutorial, we will learn the basic concepts behind reactive programming, webflux apis and a fully functional hello world example.
Learn to write spring boot async rest controller using SseEmitter which is a specialization of ResponseBodyEmitter for sending Server-Sent Events.
Learn to write spring boot async rest controller using ResponseBodyEmitter to call multiple services and collect the results to the response to the client.
Learn to test spring boot async rest controller using MockMVC and @WebMvcTest which support async requests and return CompletableFuture as response type.
Learn to write spring boot async rest controller which support async request processing and returning the response using Callable interface.
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null) error and solution.
The open-closed principle (OCP) states that a module should be open to extension but closed for modification. It is one of SOLID principles.
The single responsibility principle (SRP) states that a software component (in general, a class) must have one and only one responsibility.
The state pattern is a behavioral design pattern. According to GoF definition, a state allows an object to alter its behavior when its internal state changes. The object will appear to change its class. It can be drawn from above definition that there shall be a separate concrete class per …
Mediator helps in establishing loosely coupled communication between objects and helps in reducing the direct references to each other.
According to the GoF definition, the observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. It is also referred to as the publish-subscribe pattern. In the observer pattern, many observers (subscriber objects) observe a particular subject …
Memento design pattern is behavioral pattern and one of 23 design patterns discussed by Gang of Four. Memento pattern is used to restore state of an object to a previous state. It is also known as snapshot pattern. A memento is is like a restore point during the life cycle …
An iterator design pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
According to GoF definition of proxy design pattern, a proxy object provide a surrogate or placeholder for another object to control access to it. A proxy is basically a substitute for an intended object which we create due to many reasons e.g. security reasons or cost associated with creating fully …
As per GoF definition, flyweight design pattern enables use sharing of objects to support large numbers of fine-grained objects efficiently. A flyweight is a shared object that can be used in multiple contexts simultaneously. The flyweight acts as an independent object in each context. 1. When to use flyweight design …
Facade design pattern provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
If you’ve been developing software for a while, you know how to generate a random number and perhaps even securely with Java’s SecureRandom class. Unfortunately, generating secure random numbers is not as easy as simply using SecureRandom. In this java example, we’ve assembled a simple checklist to help you be …
Learn to write a unit test that invokes a method multiple times with different arguments – and then verifies the invocations and arguments.
Learn about mockito annotations such as @Mock, @Spy, @Captor, @InjectMocks and write tests for behavior testing using mockito annotations.
In this mockito tutorial, learn the fundamentals of the mockito framework, and how to write JUnit tests along with mockito with an example.
Learn the difference between @Mock and @InitMocks annotations in mockito and how to use these for creating and injecting mock dependencies.
Learn to write unit tests for the service layer of Spring boot applications using JUnit 5 and Mockito testing frameworks. We are using Spring Boot 3 in this demo. For Spring Boot applications, we only need to change the import statements, and everything should work automatically. 1. Maven The spring-boot-starter-test …
This Spring boot MockMvc example discusses @WebMvcTest to perform integration testing of REST controller, its GET and POST methods and verify responses.
Learn to perform load and performance testing for a web application in this step by step JMeter tutorial. This Jmeter example is intended for beginners who are trying to use the Jmeter for the first time.
Learn to apply if-else logic in a Java 8 stream of elements to filter elements based on certain condition. The if-else condition can be applied using the lambda expressions in stream filter() function.
Java forEach() is a utility function to iterate over a Collection (list, set or map) or Stream. It performs the specified Consumer action on each item in the Collection.
Learn to find the union of two arrays in Java using HashSet and Stream APIs with easy-to-understand Java examples.
Learn to find the intersection of two arrays in Java using HashSet and Stream API. An intersection contains the items present in both arrays.
Java 8 stream distinct by multiple fields example. Learn to find distinct objects from a stream by comparing multiple fields or creating a custom key class.
Learn to create a spring boot project having multiple modules. The parent project will work as a container for base maven configurations. The child modules will be actual spring boot projects – inheriting the maven properties from the parent project. 1. Parent Spring Boot Project The parent project is single …
Learn how to create multi-module maven project from console IDE. In this maven tutorial, we will learn to create nested maven projects maven cli commands.
Learn how to create a multi-module maven project in eclipse IDE. In this maven tutorial, we will learn to create nested maven projects in Eclipse. 1. Multi-module Project Structure Let’s create a maven project having modules packaging ear, war and jar types. We are creating the structure of an enterprise …
The program arguments passed at launching the Java program are called command line arguments. While launching the program we can pass additional arguments (no limit of numbers of arguments).
Java instance initializers are the code blocks containing the instructions to run every time a new class instance is created in runtime.
Java naming conventions are sort of guidelines that application programmers are expected to follow to produce consistent and readable code throughout the application. If teams do not follow these conventions, they may collectively write an application code that is hard to read and difficult to understand. Always try to give …
this and super are reserved keywords in Java. this refer to current instance of a class while super refer to the parent class of that class where super keyword is used.
Java strictfp (enabled by default since Java 17) ensures that all floating-point operations will provide consistent results as predicted by IEEE 754 in all JVMs.
In this Java tutorial, learn about difference between final, finally and finalize in detail. In short, final is a keyword, finally is a block and finalize is a method. They have their own very specific purpose in Java programs.
In Java, the extends keyword is used for extending a class or interface; and the implements keyword is used for implementing the interfaces.
Java instanceof (type comparison operator) is used to test if a specified variable is an instance of the specified class or interface
In Java exception handling, throw keyword is used to explicitly throw an exception from a method or constructor. And throws keyword is used to declare the list of exceptions that may be thrown by that method or constructor. 1. Throw Let us learn basic things about throw keyword before going …
Java synchronized keyword marks a block or method a critical section. A critical section is where one and only one thread is executing at any given time.
Java try catch finally blocks helps in writing the application code which may throw exceptions in runtime and gives us chance to recover from the exception.
Java boolean keyword is used to declare a variable as a boolean type which represents only one of two possible values i.e. either true or false.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.