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.

Java Program to Convert Degree Celsius to Kelvin & Kelvin to Celsius

1. Overview


In this tutorial, We'll learn how to convert Degree Celsius to kelvin in Java programming language. First, we will learn the formula and temperature conversion programs in java.

Degree celsius and kelvin are part of temparature mesures.

The formula for conversion:

K = ( °C + 273.15 )

Where K is kelvin and °C is degree celsius

Example 1:

Celsius: 100
Kelvin: 373.15

Example 2:

Celsius: 120
Kelvin: 393.15

Wednesday, August 21, 2019

Java - String to Int Conversion Examples

1. Overview


In this tutorial, We'll learn to convert String to integer in java.

We will be showing the example programs using the following methods and possible exceptions that occur at runtime.

Integer.parseInt()
Integer.valueOf()

For Example, We have a string "456" and want it to convert int type.