Sunday, April 12, 2020

Java 8 Optional orElseThrow() Example | Throw Exception in Optional in Java 8

1. introduction


In this tutorial, We'll learn how to throw an exception if the option is empty. Optional API orElseThrow() method returns value from Optional if present. Otherwise, it will throw the exception created by the Supplier.

2. Syntax


public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)
                                    throws X extends Throwable


Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.

A method reference to the exception constructor with an empty argument list can be used as the supplier. For example, IllegalStateException::new, ArithmeticException::new

Java 8 Optional orElseGet() Example

1. Introduction

In this tutorial, We'll learn java 8 Optional API orElseGet() method examples and where to use.

2. Syntax

[lock]
public T orElseGet(Supplier<? extends T> other)

Return the value if present, otherwise invoke other and return the result of that invocation. This method takes the Supplier Functional Interface. [/lock]

if the Supplier is null, it throws NullPointerException.


Wednesday, September 11, 2019

Java 8 Optional ifPresent() - Working Example

1. Overview


In this tutorial, We'll learn how to perform an action if a value is present in Optional. Java 8 Optional ifPresent() does the job for us.

Instead of directly getting the value using get() method, first, it checks the condition value != null.

java-8-optional-ifpresent


The old way is done using the isPresent() method as below. But, ifPresent is much simplified than isPresent().

Java 8: How to Get the Last Element of a Stream in Java?

$type=blogging

1. Introduction

[post_ads]

in this tutorial, We will be looking at different ways to get the last element of a Stream in Java 8.

When we work with Stream then we have to go through one by one element of Stream. But, if we want to get directly last value and ignore the remaining preceding elements.

here are the possible ways to retrieve only the last value from the stream.

2. Using reduce() method

[lock]
reduce() method is part of the Stream API and we have to pass the lambda expression with two arguments. [/lock]

Syntax:


Optional reduce​(BinaryOperator accumulator)


Monday, August 26, 2019

StringBuffer VS StringBuilder in Java

1. Overview


In this tutorial, We'll be learning differences between the StringBuffer and StringBuilder in java. First, let us take a brief introduction to both and see the differences.

StringBuffer is introduced in java api first and then

2. StringBuffer


This is a thread-safe, mutable sequence of characters. A string buffer is just like a String, but it can be modified using its methods such as insert() or append() methods.

This class is a thread-safe and it can be shared among multiple threads.

Thursday, August 22, 2019

Java Examples to Create New Empty File and A Temporary File

1. Overview


In this tutorial, We'll be learning how to create a file in java and how to create a temp file in java.

Example programs demonstrated in the following methods.

createNewFile()
createTempFile()

Example Programs on Files

createNewFile() method is used to create a new empty file.
createTempFile() method is used to create a temporary file.

Java Examples To Work With Properties File (Read & Write)

1. Overview


In this tutorial, We'll be learning how to read from and write to the properties file in java.

Java API priovides a class Properties which is part of java.util package.

The Properties class stores the set of key, value pairs as properties. It internally stores all properties in a Stream. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list stored as a string.

public class Properties
extends Hashtable<Object,​Object>

Properties inherit from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead.