Java 8 added a new functional interface named “Consumer”. The purpose of this interface is to consume/process the information received, it doesn’t return anything. I will be explaining using the below example. In the example I have list of employees whom I want to terminate. I will pass the list of employees to an implementation…… Continue reading Consumer Functional Interface Example
Category: Java
BiPredicate Simple Example
This post explains BiPredicate functional interface. BiPredicate is similar to Predicate functional interface except BiPredicate takes two arguments instead of one. In the below example we will filter list of Person entities based on first and last name. The predicate class name will be “NamePredicate”. NamePredicate package Function; import java.util.function.BiPredicate; public class NamePredicate implements BiPredicate…… Continue reading BiPredicate Simple Example
Different ways to convert list to array
This post explains two ways through which we can convert a list to array. First approach is simply calling toArrayList on list instance. This method returns an Object array, When converting to array it doesn’t consider the data type of the entries in the list. Below is the code snippet list.toArray(); The second approach involves…… Continue reading Different ways to convert list to array
Different ways to convert collection to an array
This post explains two ways through which we can convert a collection to an array. The collection can be a list or set. First approach is simply calling “toArray” on collection instance. This method returns an Object array, When converting to array it doesn’t consider the data type of the entries in the collection. Below…… Continue reading Different ways to convert collection to an array
Creating a custom log message format
This post explains how to create a custom log message format. Out of the box java provides two log formats SimpleFormatter and XMLFormatter. Both extends the abstract super class Formatter. We can create our own custom format. Below is the code of the custom format CustomFormatter 1 package Logging; 2 3 import java.util.logging.Formatter; 4 import…… Continue reading Creating a custom log message format
Changing the default log message format
In this post I will be explaining how to change the default log message format provided by java util loggging. In case of logging to console (done by ConsoleHandler), the default format used is an instance of java.util.logging.SimpleFormatter. In case of logging to file (done by FileHandler), the default format used is an instance of…… Continue reading Changing the default log message format
Redirecting logging to a file
In this post I will explain how to program the logger to log messages to a file instead of logging to console. Below is the code Main code 1 package Logging; 2 3 import java.io.IOException; 4 import java.util.logging.FileHandler; 5 import java.util.logging.Logger; 6 7 public class LoggingDemo2 { 8 public static void main(String[] args) throws IOException…… Continue reading Redirecting logging to a file
Java logging simple example
This post explains java logging with simple example. Main code 1 package Logging; 2 3 import java.io.IOException; 4 import java.util.logging.Logger; 5 6 public class LoggingDemo1 { 7 public static void main(String[] args) throws IOException { 8 Logger logger = Logger.getLogger(“logger1”); 9 logger.info(“Hello my name is Mathew”); 10 logger.warning(“Hello my name is Mathew”); 11 logger.config(“Hello my…… Continue reading Java logging simple example
Decompressing JAR file
JAR file is file archiver which collects/groups different files and folder into a format which represents one file. The format of JAR file is an uncompressed format. Pack200 is a tool that can be used to compress and decompress a jar file. Below is a simple example of how we can use Pack200 tool to…… Continue reading Decompressing JAR file
Compressing JAR file
JAR file is file archiver which collects/groups different files and folder into a format which represents one file. The format of JAR file is an uncompressed format. Pack200 is a tool that can be used to compress jar file. Below is a simple example of how we can use Pack200 tool to compress the file…… Continue reading Compressing JAR file