This section provides an overview of Java 8’s primary new features— with code examples—to give you an idea of what’s available. Lambda Expressions Lambda expressions let you pass around a piece of code in a concise way. For example, say you need to get a Thread to perform a task. You could do so by creating a Runnable object, which you then pass as...

Read More

Java 8 introduces a brand new Date and Time API that fixes many problems typical of the old Date and Calendar classes. The new Date and Time API was designed around two main principles: Domain-driven design:  The new Date and Time API precisely models various notions of date and time by introducing new classes to represent them. For example, you ca...

Read More

So what is a stream? Informally, you can think of it as a “fancy iterator” that supports database-like operations. Technically, a Stream is a sequence of elements supporting sequential and parallel bulk operations. Streams support lazy transformative operations (transforming a stream to another stream) such as filter and map, and consuming operatio...

Read More

Streams can be created from various data sources, especially collections. Lists and Sets support new methods stream() and parallelStream() to either create a sequential or a parallel stream. Parallel streams are capable of operating on multiple threads and will be covered in a later section. Here we will check different ways to build streams from c...

Read More

In the previous post we have alreday seen different ways of creating a Stream. In this post we will check some other Stream operations. Empty Stream The empty() method should be used in case of a creation of an empty stream: Stream emptyStream = Stream.empty(); Its often the case that the empty() method is used upon creation to avoid returning nu...

Read More

Convert elements of a collection to upper case Java 7 vs Java 8 import static java.util.stream.Collectors.toList; import java.util.ArrayList; import java.util.List; public class Test { /** * Convert elements in a Collection to upper case using Java 7 * @param inList * @return */ public static List convertToUpperCa...

Read More

Filter collection elements thus only elements with less than 4 characters are returned : import static java.util.stream.Collectors.toList; import java.util.ArrayList; import java.util.List; public class Test { /** * Filter elements with less than 4 characters in a Collection to upper case * using Java 7 * * @param inList ...

Read More

Consider you have a Hierarchical collection as List> and you want to convert it into a flat collection like List. First consider the same by Java 7 import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class FlatCollectionByJava7 { /** * Convert into flat list by Java 7 * @param ...

Read More

Here in this example we are summing up the elements of a List of Integer by Java 7 and Java 8. In Java 7 it is very straight forward. 1.  We will take a variable total and set its initial value as 0. 2.  Then we will loop through the collection and add elements one by one with total. 3.  We are ready with the sum at the end of the iteration. publ...

Read More

Here in this example we will see, how we can compare a list of objects to find a particular one based on some condition. Consider the following example. Here we are going to find the costliest iPhone from a list of  iPhones. For this example we are using the following class to create Object of different models of iPhone. import java.util.Arrays; ...

Read More

FOLLOW US ON LinkedIn



Explore Tutu'rself