Java 8

Related Tags

Tutorials

Append or Prepend Items to a Stream

Learn to add items to a Java Stream. Remember that a Stream is not a data structure or collection that can store values. To add items to an existing Stream, we need to : Create a new Stream with items to be added Concatenate with the first stream to get …

Java Stream – Get Object with Max Date From a List

Learn to get an object with the latest date (max date) from a Stream of custom objects. We will use a custom Comparator for comparing the Date values stored in the custom objects. This example uses Employee class. We will create a program to get the youngest employee in a …

Java Stream findAny()

Java 8 Stream.findAny() returns an Optional describing any element of Stream if Stream is non-empty. It returns an empty Optional if the stream is empty.

Java Stream findFirst() Example

Java Stream findFirst() returns an Optional describing the first element of stream if Stream is non-empty, or an empty Optional if the stream is empty.

Java IntPredicate Example

Java 8 IntPredicate is a functional interface whose functional method is boolean test(int a). It can be considered a function returning true or false value.

Guide to IntStream in Java

Java IntStream represents a stream of primitive int-valued elements supporting sequential and parallel aggregate operations.

Join or Append Stream Elements in Java

Learn to join a stream of strings with a delimiter, prefix, and suffix using Collectors.joining(), String.join(), and append elements using StringBuilder.

Sorting a Stream by Multiple Fields in Java

Java 8 example to sort stream of objects by multiple fields using comparators and Comparator.thenComparing() method. This method returns a lexicographic-order comparator with another comparator. It gives the same effect as SQL group by clause.

How to Sort a Stream in Java

Learn to sort stream of numbers and strings in ascending (natural order) and descending orders (reverse order) using Stream.sorted() method.

Java Collect Stream to List (with Examples)

Learn to convert stream to list using Collectors.toList() and Collectors.toCollection() APIs. This post contains multiple examples for collecting stream elements to list under different usecases.

Java Stream toArray() Example

Learn to convert stream to array using Stream.toArray() API. This post contains multiple examples for collecting stream elements to array under different usecases.

Java Stream noneMatch()

Java Stream noneMatch(predicate) is a short-circuiting terminal operation to check if no element in the stream matches the given predicate.

Java Stream allMatch()

Java Stream allMatch() is a short-circuiting terminal operation that is used to check if all the elements in the stream satisfy the provided predicate. 1. Stream allMatch() Method 1.1. Syntax Here predicate a non-interfering, stateless predicate to apply to all the elements of the stream. The allMatch() method returns always …

Java Stream anyMatch()

Java Stream anyMatch (Predicate predicate) is terminal-short-circuiting operation which is used to check if the stream contains any matching element with provided predicate.

Java Stream skip()

Java Stream skip(n) method returns a new stream consisting of the remaining elements of this stream, after the n elements have been skipped.

Java Stream limit()

We can use Stream.limit(long) to retrieve elements while they must not be greater than a certain maximum count. Java 8 Stream limit() example

Java Stream peek()

Java 8 Stream interface has peek(Consumer action) method which returns a new stream consists of all the elements of original stream after applying the method argument Consumer action.

Java Stream flatMap()

Learn to use Java Stream flatMap() method which is used to flatten a stream of collections to a stream of elements combined from all collections.

Java Stream map()

Learn to use Java Stream map() method which produces one output value of a different type ‘X’ for each input value of type ‘Y’.

Java Stream min()

Learn to use Stream min() method to select the smallest element in the stream according to the comparator provided in its argument.

Java Stream max()

Learn to use Java Stream max() method to select the largest element in the stream according to the comparator provided in its argument.

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 Stream filter()

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 …

Java Stream forEachOrdered()

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.

Java Stream forEach()

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.

Secure Random Number Generation in Java

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 …

Using ‘if-else’ Conditions with Java Streams

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()

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.

Boxed Streams in Java

In java 8, to convert a stream of primitives to collection, you must first box the elements in their wrapper class and then collect them i.e. boxed stream.

Java 8 Method Reference (with Examples)

Since Java 8, in simplest words, the method references are a way to refer to methods or constructors without invoking them. Learn the syntax with examples.

Java Stream distinct(): Collect Unique Values

Using java 8 stream API, you can use stream.distinct() method to filter or collect all distinct elements from a collection. Let’s learn how to find distinct elements with java stream API.

About Us

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.