Showing posts with label java.util. Show all posts
Showing posts with label java.util. Show all posts

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().

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.

Wednesday, August 7, 2019

Java 8 Optional flatMap() Method Example

1.Overview


In this tutorial, We'll learn how to wrap all nested Optional values into a Direct Map instead of having hierarchies.
You get a better understanding once you see the example.

You will be learning today syntax, examples and its internal implementation part.

This is part of our Optional Method series course.

API Description:

If a value is present, returns the result of applying the given Optional-bearing mapping function to the value, otherwise returns an empty Optional. This method is similar to map(Function), but the mapping function is one whose result is already an Optional, and if invoked, flatMap does not wrap it within an additional Optional.

Monday, August 5, 2019

Java WeakHashMap Working Examples

1. Overview


In this tutorial, We'll be talking about WeakHashMap in java and it is placed in java.util package.

WeakHashMap is implemented based on Hashtable with weak keys. Map internally is built on Entry objects. When adding a key-value pair, it will be stored in the Entry object. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. So, once the key is deleted from the map it is eligible for Garbage Collection and we can not prevent from running GC on it. This class completely behaves differently than other Map implementations such as HashMap, LinkedHashMap, etc. And also it has efficiency parameters of initial capacity and load factor as same as HashMap.

java-weakhashmap

Sunday, August 4, 2019

Java 8 Optional equals() Method Example

1. Overview

In this tutorial, We'll learn how to compare the Optional value with another value or object. Optional is a java 8 new class and introduced in java.util package.

First, we'll understand the syntax and its meaning. Next, equals() example program and its internal implementation.

optional-equals


Optional class All Methods

API Note: 

Indicates whether some other object is "equal to" this Optional value. The other object is considered equal if it is also an Optional and; both instances have no value present or; the present values are "equal to" each other via equals().

Thumb rule is that the passed object also must be Optional object. Otherwise, this method will return false.

Thursday, August 1, 2019

Java 8 Optional filter() Method Example

1. Overview

In this tutorial, We'll discuss how to use Predicate with Optional class. The Java 8 optional class has a method filter() which takes Predicate as an argument.

Optional is a class and it is in java.util package. Optional is declared as final in its source code. Because no other classes can be inherited and to stop overriding the behavior.

Java 8 Optional filter


API Note: If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.

Wednesday, July 31, 2019

Java 8 Optional ofNullable() Method Example

1. Overview


In this tutorial, We'll learn ofNullable() example on how to create new Java 8 Optional object for any value or null value. Optional is part of java.util package.

Java 8 Optional ofNullable() Method Example


API Note: Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.

Tuesday, July 30, 2019

Java 8 Optional of() Method Example

1. Overview


In this tutorial, We'll learn how to create an Optional object for any value in Java 8. This is a very useful method to create an instance of Optional. Optional is a new class and introduced part of java.util package.

In last tutorial, We've discussed Optional.empty() method.

Let us take a look at Syntax, Example program on of() method.

API note: Returns an Optional describing the given non-null value. If null value, it throws NullPointerException.

Java 8 Optional of() Method Example


Monday, July 29, 2019

Java 8 Optional empty() Method Examples

1. Overview


In this tutorial, We'll learn how to create an empty Optional instance using Java 8 empty() method. Let us take at the Java 8 Optional empty() example program.
Before that let us take a look at the syntax first and then next its usage.

empty() method is used to create an empty Optional object. That means no value is present for this Optional. Optional is a final class.



optional-empty

Thursday, July 25, 2019

Java 8 Stream allMatch() Method

Stream allMatch() Method 


In this tutorial, We will see the example program on Java 8 Stream allMatch() method. allMatch() method checks whether all elements of Stream is matched to the given predicate then it returns true. Otherwise returns false.


This is called Terminal Operation because this is executed at the end of the stream. That means Stream is closed after the execution of allMatch() method.


Java 8 Stream allMatch()