In this tutorial I am going to show how default and static methods can be included into Java 8 interface. Prior to Java 8 we did not have such mechanism to include default method or static method into interface but Java 8 provides such option to include those methods. Static methods are regarded as utility methods, not as essential, core methods. Whereas default methods are treated as common behaviors for all the client classes. Continue reading “Java 8 default and static methods example”
In this tutorial I am going to show how to create custom thread pool in Java.
Multi-threaded and multi-process programming is a great way to optimize CPU usage and get things done quickly.
Creation of Threads in Java is a costly IO operation. Therefore it is not advisable to create & destroy thread(s) every often. It is recommended to use pool of threads as per the needs. Thread pool is a collection of threads, which are created to perform certain tasks.
I came across a situation where I required to fetch data from a HashMap and add it to an ArrayList. So I will show you here how to iterate a Map and form an object and finally add it to the List using Java 8 lambda expression.
This tutorial will show you how to use Java 8 stream API’s filter and map methods.
filter() method is used to produce a stream containing only a certain type of objects into the result output and
map() method is used to transform a stream from one type to another type into the result output.
A Future represents the pending result of an asynchronous computation. It offers a method — get — that returns the result of the computation when it’s done.
The problem is that a call to get is blocking until the computation is done. This is quite restrictive and can quickly make the asynchronous computation pointless.
CompletableFuture<T> extends Future<T> and makes it completable in an ad hoc manner. This is a big deal, considering that Future objects were limited before Java 8. Continue reading “CompletableFuture in Java”
The Date-Time package, java.time, introduced in the Java SE 8 release, provides a comprehensive model for date and time.
Why do we need for a new Date and Time library ?
The existing classes (such as java.util.Date and SimpleDateFormatter) aren’t thread-safe, leading to potential concurrency issues for users — not something the average developer would expect to deal with when writing date-handling code. The new API avoids this issue by ensuring that all its core classes are immutable and represent well-defined values.
Some of the date and time classes also exhibit quite poor API design. For example, years in java.util.Date start at 1900, months start at 1, and days start at 0 — not very intuitive. For example, java.util.Date represents an instant on the timeline — a wrapper around the number of milli-seconds since the UNIX epoch — but if you call toString(), the result suggests that it has a time zone, causing confusion among developers. The new API models its domain very precisely with classes that represent different use cases for Date and Time closely.
Use of third party date and time library
These issues, and several others, have led to the popularity of third-party date and time libraries, such as Joda-Time. The new API allows people to work with different calendaring systems in order to support the needs of users in some areas of the world, such as Japan or Thailand, that don’t necessarily follow ISO-8601.
This tutorial will explain about Java’s new API called stream, which is introduced in Java 8 and included in package java.util.stream. The stream API supports functional-style operations on collection of elements or stream of elements in a collection.
For example, you only want to print even numbers from a list of numbers, then you can use the stream API in the following manner
List<Integer> integers = Arrays.asList(1,2,3,5,8,11); integers.stream().filter(i -> i % 2 == 0).forEach(i -> System.out.println(i));
The classes Stream, IntStream, LongStream, and DoubleStream are streams over objects and the primitive int, long and double types. Continue reading “Java 8 Stream API”
This tutorial will show you how to use Java 8 forEach loop to iterate Collection using Lambda Expression. Prior to Java 8 or JDK 8 we used to use the for loop though it was for-each style but in Java 8 the inclusion of forEach loop simplifies the iteration process in mainly one line.
Let’s look at the below example to see how it can be used and how simple it is using Java forEach loop.