Introduction: Iterators in Java are used to traverse elements of a given source. Spliterator in Java is one among the four available Java Iterators – Iterator, Enumeration, ListIterator, and Spliterator. It is an interface available in java.util package. Spliterator was first introduced in Java 8 to support parallel programming. However, we can use it for […]
Introduction: In this tutorial, we’ll learn how to use the Java 8 Streams API with the primitive data types. Java 8 Streams API is a powerful API to efficiently process a sequence of elements. If you wish to learn about Java 8 Streams API, please check out this tutorial. Stream Over Java Primitives: As we […]
Introduction: In this quick tutorial, we’ll look at ways in which we can create a word frequency map in Java 8. Problem Definition: Let’s say we have been given a list of names: List<String> names = {"Sam", "James", "Selena", "James", "Joe", "Sam", "James"}; We wish to print the frequency map specifying the frequency count of […]
Introduction: GroupingBy Collectors introduced in Java 8 provides us a functionality much similar to using GROUP BY clause in a SQL statement. It helps us group objects based on certain property, returning a Map as an outcome. In this article, we’ll look at various example usages of the groupingBy collector in Java 8. To follow […]
Introduction: Java 8 Stream API has two methods – findFirst() and findAny() which often seem confusing at first. In this article, we’ll learn the differences between the two so that we can choose wisely the next time. Stream.findAny(): findAny() method is a short-circuiting terminal operation. It returns an Optional<E> describing some element of the stream […]
Introduction: Java 8 Stream API is a powerful way to achieve functional programming. In this post, we’ll learn about Java 8 Stream sorted() method by looking at some good examples. In case you wish to learn more about Java 8 Streams API in general, we’ll suggest you read this article. Signature: There are two variants […]
Introduction: Java 8 Streams is a powerful feature introduced in Java 8. To learn more about Streams in Java, we’ll suggest you read this post. In this tutorial, we’ll cover how to collect the output stream into one of the known Collection types followed by exploring a few other Collectors options. We’ll be reusing the […]
Introduction: Java 8 introduced functional programming and the concept of streams and functional interfaces to back it. As the name suggests, Java 8 Stream can be thought of as a stream or source of data to operate on. For this tutorial, we assume the reader to have an understanding of Java Optional and Functional Interface. […]
Introduction: java.util.Optional class defined in Java 8 can be thought of as a container class for storing and handling not – null objects. When working with an Optional, a null value is usually represented as an absent value instead. The Optional class exposes various methods to help us treat values as either ‘Available’ or ‘Unavailable’. […]
Introduction: Java 8 map() and flatMap() operations can be applied to either Streams or Optional. Stream<T> can be thought of as a wrapper holding a sequence of objects of type T to help us operate over them. Similarly, Optional<T> in Java 8 adds a layer to the top of element T, to help us determine […]