Learnitweb

Category: Java tutorial

  • java.time package in Java 8 – Introduction

    java.time package was introduced in Java 8 as a better way of dealing with date and time. java.util.Date and java.util.Calendar classes were not convenient to use and also didn’t handle internationalization and localization situations very well. java.util.Date, java.util.Calendar, and java.text.DateFormat classes which we were using for so many years are still there but java.time package…

  • Stream forEachOrdered method in Java with example

    Many people have written us to explain the use of this method. Many people have confusion about the use of this method and how this method is different from forEach method. forEachOrdered method is provided my Stream interface in Java 8. Syntax Official documentation states the following about this method: Performs an action for each…

  • Java 8 forEach with examples

    Java 8 provides a new method forEach in Iterable and Stream interfaces to iterate elements. forEach provides a new way of iterating elements. Classes which implement Iterable interface can use this method to iterate elements. Syntax 1. Syntax of forEach in Iterable 2. Syntax of forEach in Stream Official documentation states the following about forEach:…

  • Java 8 Collectors

    In this tutorial we’ll discuss Collectors class and few of its important methods. The Stream.collect() method collect() method is provided by the Stream interface. This method performs a mutable reduction operation on the elements of this stream using a Collector. A mutable reduction operation accumulates input elements into a mutable result container, such as a…

  • Stream flatMap() in Java with examples

    Method Syntax Important points about flatMap() What is flattening? Let us understand flattening with the help of an example. Consider the following array: After flattening, the result is: So just to understand in simple terms, we can say that flattening is the merging of multiple collections into one. Need to flat a stream Sometime there…

  • Java 8 stream map() and example

    This method is used when you want to process elements of a stream and apply some function on each element of a stream. We usually use map() while processing collection elements. An example to explain the use of map() is to write a program to multiply each element of a list of integers by 2.…

  • Java 8 filter() with examples

    Java 8 Stream interface introduces filter() method which can be used to filter out elements based on a particular condition. The condition to filter elements is specified as a predicate. If the condition evaluates to true, the object is selected. Otherwise, it will be ignored. For example, you can use filter() to get even elements…

  • Java Stream generate() method with example

    Stream interface provides a method with following signature: This method returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc. This method generates an infinite stream. We can add limit to get desired number of elements. Output

  • How to create stream in Java 8

    An instance of stream can be created from different sources. In this tutorial, we’ll discuss different ways to create a stream instance. First, we’ll discuss stream of four different sources: array, a collection, generator function and I/O channel. Stream of Array There are two common methods used to create stream of Array: Stream.of() and Arrays.stream().…

  • Java 8 Stream – An Introduction

    Stream is basically a sequence of elements. The basic definition of stream is – A sequence of elements supporting sequential and parallel aggregate operations. Java 8 provided an additional package java.util.stream. It contains classes to support functional operations on streams of elements. We’ll discuss various operations on stream later. Let us see one example without…