Learnitweb

Category: Java tutorial

  • Prevent singleton pattern from serialization in Java

    1. Introduction Serialization means to convert state of an object into a byte stream. Deserialization is the process of converting the serialized form of an object back into a copy of the object. A Singleton class is supposed to have only one instance. Using deserialization, it is possible to break this pattern. If you deserialize…

  • Sort a stream in Java

    1. Introduction In this tutorial, we’ll discuss ways to sort stream with the help of examples. Stream interface provides following overloaded sorted() methods: 2. Sort a list of integers You can use the sorted() method to sort the list in the ascending order. Complete example Output 3. Sort a list of integers in descending order…

  • How to convert Array to Stream?

    In Java 8, there are two methods to convert Array into a Stream: 1. Object Arrays In case of object arrays, you can use both Arrays.stream() and Stream.of() methods as both return the same output. Output For object arrays, the Stream.of() method calls the Arrays.stream() method internally. 2. Primitive arrays In case of primitive arrays,…

  • Java 8 StringJoiner

    1. Introduction StringJoiner is a new class added in Java 8 in java.util package. This class is very helpful to create string separated by delimiter and having a prefix and suffix. StringJoiner is used to create a sequence of characters separated by a delimiter. You can optionally provide a prefix and suffix to create the…

  • Object cloning in Java

    1. Introduction While writing programs sometimes there is a need to create a copy of the object, or we can say need to clone an object. The process of creating the clone is defined by the object itself. In this tutorial, we’ll discuss two main aspects to cloning in Java: The intention of cloning an…

  • Fork/Join framework in Java

    1. Introduction In this tutorial, we’ll discuss fork/join framework which is an implementation of ExecutorService interface. The computer systems in present time have multiple cores. A better concurrent programming approach is to use all the computation power of all cores. The fork/join framework enables to use processing power of all available cores. The fork/join framework…

  • Stack in Java

    1. Introduction Stack is a last-in-first-out (LIFO) stack of objects. Stack class is a part of collection framework. Elements can in inserted and retrieved at only one end of the Stack. Stack in Java is represented by java.util.Stack class. The java.util.Stack class extends java.util.Vector. Stack implements following interfaces: A stack of plates in a cupboard…

  • Stream peek() Method in Java

    1. Introduction The syntax of peek method is: 2. peek method without terminal operation peek() is an intermediate operation and does nothing unless terminal method is called. 3. peek method with terminal operation peek() method needs a terminal operation to start the processing. Output

  • ConcurrentHashMap in Java

    ConcurrentHashMap is defined in java.util.concurrent package. This class is a member of the Java Collections Framework. This class implements Serializable, ConcurrentMap and Map interfaces. Retrieval operations of this class does not result in locking and are non-blocking.

  • CopyOnWriteArrayList in Java

    CopyOnWriteArrayList is a thread-safe variant of ArrayList. All mutative operations (add, set and so on) are implemented by creating a new copy of the underlying array.