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

Traditional Way of Performing Operation on Map: for (Map.Entry entry : SOME_MAP.entrySet()) {     if("somevalue".equals(entry.getValue())){ // Do Some Processing // Add Value to some list or Add the Value to list or Perform Some operation         System.out.println(“Result Found”)     } } Using Java 8 Stream API: Below Exa...

Read More

In our A Quick Tour of Java 8 Features  We have already discussed about all new features introduced in Java 8. Now in this article we will discuss more about new features like Lambda expressions and Streams.  Java 8 Lambda Expressions : Lambda expressions are new in Java 8. Lambda expressions are Java’s first step of entering into functional progra...

Read More

What are Functional interfaces ? Functional interfaces have a single functionality to achieve. Like the Runnable interface, the Callable interface and the Comparator interface, and a whole host of other interfaces already defined within Java—is what Java 8 calls a functional interface: It is an interface which requires exactly one method to be impl...

Read More

In our earlier Article about Java 8 Lamda Java 8: Lambdas, Part 1 , Get to know lambda expressions in Java 8 we have already discussed about the basic of Lambda expression introduced in Java 8. Here we will have some more discussion about Lamdas. A lambda in Java essentially consists of three parts: A parenthesized set of parameters. An arrow. And ...

Read More

Note: This post is inspired by and is an attempt at creating an extension to this post on Medium. As such, it will use some of the code in that post by Joel Planes. As a Java developer, the most common task we have to do is to write some server-side validations for our model data so as to validate the incoming objects to our application. Sure there...

Read More

Comparing the content of Lists against some condition is a common use case to handle in many business scenarios. This comparison can be broadly classified as: Comparing each element of a List against some condition. As an example, you have a List of Employee Objects and you need to check that all your Employees are above 18. One or more elements fr...

Read More

In Java 8 the most important library changes are made in Collection API and there is also a new addition named Stream. Streams allow us to write collections-processing code at a higher level of abstraction. The Stream interface contains a series of functions that we’ll explore throughout this tutorial, each of which corresponds to a common operatio...

Read More

The most frequent operation while working with collections is to iterate over a collection and perform some action on each element in turn. For example, Consider we have a List of Employees and we want to find the senior most and junior most employee from the List. So in normal boilerplate way we need to iterate over the List and identify the emplo...

Read More

One important operation that we often perform on Collection is partition it into two collections of values. For example, if you’ve got a Stream of Student, then you might wish to divide them based on gender. One approach to doing this is to perform two different filters, one looking for female students and the other for male students. This approach...

Read More

FOLLOW US ON LinkedIn



Explore Tutu'rself