Learnitweb

Category: Java tutorial

  • Collectors partitioningBy() method in Java

    1. Introduction In Java, the Collectors class provides various methods to collect data from streams. One of the most powerful and frequently used methods is Collectors.partitioningBy(). This method is used to partition a stream of elements into two groups based on a given predicate. It returns a Map with Boolean keys (true and false) and…

  • Virtual threads in Java – An Introduction

    1. Introduction In this tutorial, we’ll discuss virtual threads. 2. Motivation Behind virtual threads Server applications typically manage concurrent user requests that operate independently. Therefore, it is logical for an application to handle each request by assigning a dedicated thread for its entire duration. This thread-per-request approach is straightforward to understand, program, debug, and profile…

  • Java switch expressions

    1. Introduction There were two problems of switch statements: default control flow behavior of switch blocks and the default scoping of switch blocks. To solves these issues, switch expression was introduced. Switch expression was a preview language feature in JDK 12 and JDK 13. With switch expressions switch was extended to be used either a…

  • Record classes in Java

    1. Introduction Many developers believe that the Java is too verbose. Writing a simple data-carrier immutable class involves a lot of low-value code such as constructors, accessors, equals, hashCode, toString etc. For example, a class Point representing a point in two dimensional space with two coordinates x and y can be written like this: Here,…

  • Sealed classes in Java

    1. Introduction In this tutorial, we’ll discuss a very important introduction in Java language, sealed classes and interfaces. The enums in Java assist in modeling fixed set of values. But sometimes we need to model fixed kind of values. For example, Here, there are three types of Vehicle. To restrict the kind of Vehicle, we…

  • Text Blocks in Java

    1. Introduction In Java, embedding a code snippet written in another language requires significant concatenation and escaping of characters. Here is such an example of HTML code embedded in Java. Such type of code is difficult to read and maintain. In Java, a text block is a multi-line string literal that simplifies writing strings that…

  • Preview feature in Java

    1. Introduction A preview feature is a new feature whose design, specification, and implementation are complete, but which is not permanent, which means that the feature may not exist in future JDK release but if exists then may exist in a different form. Every preview feature is described by JDK Enhancement Proposal (JEP). For example,…

  • Comparing file with Files.mismatch() method in Java

    1. Introduction In Java 12, mismatch() method was added to compare two files. This method returns the position of the first mismatched byte in the two files. Two files are considered a match if one of the two following conditions are satisfied: If none of the conditions are satisfied then the files are considered a…

  • Java Teeing Collectors

    1. Introduction In Java 12, method teeing() was added in Collectors. This method returns a Collector that is a composite of two downstream collectors. It allows two different collectors on a stream and then merges their results using a specified BiFunction. The Collectors.teeing() method is useful when we want to simultaneously process a stream in…

  • Compact Number Formatting in Java

    1. Introduction In US locale, you may have seen 1000 written as “1k”, 1000000 as “1M”. Here, “1K” and “1M” are shorter or compact forms. A compact number formatting refers to the representation of a number in a shorter form, based on the patterns provided for a given locale. CompactNumberFormat is a concrete subclass of…