Friday, August 9, 2019

Java ConcurrentSkipListSet Examples

1. Overview


In this tutorial, We'll guide to the new Java 1.6 version ConcurrentSkipListSet. This is part of the new Collection API and package java.util.concurrent.

This class is a direct subclass of AbstractSet and implements NavigableSet.

public class ConcurrentSkipListSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable


ConcurrentSkipListSet is mainly used to keep the elements sorted according to their natural ordering. This is the default behavior and no need to provide the comparator logic to it. If we provide the Comparator at the time of constructor creation then it uses our logic to sort the set. ConcurrentSkipListSet internally built using ConcurrentSkipListMap. This is equivalent to TreeSet and works well in a concurrent environment.

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.

Saturday, August 3, 2019

Java 8 IntStream.Builder - Methods Examples

1. Introduction


In this tutorial, We'll learn how to use IntStream.Builder Interface in Java 8. This is part of java.util.stream package. We have already covered Java 8 Stream API.

IntStream.Builder follows the builder design pattern to build the IntStream object.

IntStream.Builder is a mutable builder for an IntStream. This creates an integer stream which can be added with the values or elements before invoking the build phase. We will discuss how to create a builder instance and its life cycle step by step in this article. For now, you need not worry about these terms. By the end of this article, you will be confident about this interface.



intstream-builder


In the end, We will write example programs using accept(), add() and build methods.

Friday, August 2, 2019

Java 8 DoubleSummaryStatistics API - Working Examples

1. Overview


In this tutorial, We'll guide to DoubleSummaryStatistics class which added in Java 8 API. This is introduced in java.util package.

DoubleSummaryStatistics

DoubleSummaryStatistics class is designed to get statistics of a for a stream such as count, min, max, sum, and average operations for double values or DoubleStream.

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.