Monday, August 24, 2020

Java 8 Optional Tutorial With Examples

1. Overview

In this tutorial, You will learn in-depth about Java 8 Optional Class methods and its usages.

Optional class is added to the java.util package. The intention of introducing this class in java 8 is mainly to check whether the value is present in the object or it is absent.

The object is to hold the set of values that means it contains the real values. So, such kind of object is called a Container.

The container object may contain a null or non-null value in it.

Java 8 Optional Tutorial With Examples

Wednesday, August 19, 2020

Java 8 Base64 Encoding and Decoding (With Examples)

1. Overview

In this article, you'll learn the different ways to do the base 64 encoding and decoding techniques in java 8 and other alternative libraries such as apache common API utility.

Main focus on How to do Base64 encoding and decoding in Java, using the new APIs introduced in Java 8 as well as Apache Commons.

Understand the techniques on how to encode and decode base64 in java.

Java 8 Base64 Encoding and Decoding (With Examples)

Tuesday, August 18, 2020

How to remove all duplicates from a List in Java 8?

1. Overview

In this article, you'll explore the different ways to clean up and remove duplicates from the list and ArrayList.

Let us see the example programs using plain java and java 8 stream api lambda expressions.


2. Removing Duplicates Using Plain Java

A simple way is to remove the duplicates to clean up the list using List.contains() method. Create a new list and Pass every value of the original list to the contains() method on a new list. Returns false if the current value is not present in the new list and add the value to the new list. if returns false means that value is already present in the list and skip the value-adding to the new list.

Example:

import java.util.ArrayList;
import java.util.List;

public class RemoveDuplicatesContains {

	public static void main(String[] args) {

		// Creating a new list
		List<String> originalList = new ArrayList<>();
		
		// Adding duplicate values
		originalList.add("A");
		originalList.add("B");
		originalList.add("C");
		originalList.add("C");
		originalList.add("B");
		originalList.add("A");
		
		// printing the original list
		System.out.println("Original list values : "+originalList);
		
		// created a new list to add only unique values
		List<String> newList = new ArrayList<>();
		
		// filtering the duplicates from the 
		originalList.forEach(eachValue -> {
			if(!newList.contains(eachValue)) {
				newList.add(eachValue);
			}
		});
		
		// printing the cleaned list without duplicatesa
		System.out.println("newlist values are : "+newList);

	}
}

Output:

Original list values : [A, B, C, C, B, A]
newlist values are : [A, B, C]

3. Removing Duplicates Using LinkedHashSet

Next, use LinkedHashSet to remove the duplicates and preserve the order as in the original list. LinkedHashSet is a collection api Set interface. Set interface implementation such as HashSet or LinkedHashSet. But, many developers use HashSet rather than LinkedHashSet.

Example:

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class RemoveDuplicatesLinkedHashSet {

	public static void main(String[] args) {

		// Creating a new list
		List<String> originalList = new ArrayList<>();

		// Adding duplicate values
		originalList.add("A");
		originalList.add("B");
		originalList.add("C");
		originalList.add("C");
		originalList.add("B");
		originalList.add("A");

		// printing the original list
		System.out.println("Original list values : " + originalList);

		// Creating linkedhashset object
		Set<String> linkedSet = new LinkedHashSet<>();

		// adding list values to set
		linkedSet.addAll(originalList);

		// removing all values from list
		originalList.clear();

		// add all values from set to list.
		originalList.addAll(linkedSet);

		// printing the cleaned list without duplicates
		System.out.println("originalList values ater removing duplicates  : " + originalList);

	}
}

Output:

Original list values : [A, B, C, C, B, A]
originalList values ater removing duplicates  : [A, B, C]

4. Remove Duplicates From a List Using Java 8 Lambdas

Let us look at the new JDK 8 lambda expressions and Stream api's distinct() method to remove duplicates.

distinct() method internally calls equals() method on each value and filters the duplicates objects.

In our example, we are adding Strings to the list so equals() method of String class will be called. If you are passing the Custom objects then you need to override the equals() method in the custom class as per needed.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class RemoveDuplicatesJava8Distinct {

	public static void main(String[] args) {

		// Creating a new list
		List<String> originalList = new ArrayList<>();

		// Adding duplicate values
		originalList.add("A");
		originalList.add("B");
		originalList.add("C");
		originalList.add("C");
		originalList.add("B");
		originalList.add("A");

		// printing the original list
		System.out.println("Original list values : " + originalList);

		// Java 8 Stream API - distinct() method used
		originalList = originalList.stream().distinct().collect(Collectors.toList());

		// printing the cleaned list without duplicates
		System.out.println("Removed duplicates with java 8 api : " + originalList);

	}
}

Output:

Original list values : [A, B, C, C, B, A]
Removed duplicates with java 8 api : [A, B, C]

5. Conclusion

In this article, you've seen the 3 different ways to remove all duplicates from ArrayList with examples programs.

All are over GitHub.

distinct example

Java 8 Stream concat() - How To Merge Two Streams or More

1. Overview

In this article, you are going to learn the java 8 Stream api concat() method to merge two streams into one stream. In the end, merged streams or collections will have all elements from both streams.

Let us explore the different ways to merge the streams using java 8 stream api.

Java 8 Stream concat() - How To Merge Two Streams or More

Monday, August 17, 2020

Java 8 Stream limit() Method Example

1. Overview

In this article, you'll learn how to limit the stream elements to the given size even though it has more elements.

Use Java 8 Stream.limit() method to retrieve only the first n objects and setting the maximum size. And it ignores the remaining values after size n. Stream.limit(long maxSize) returns a Stream of objects.

We will explore the different ways to do limit the stream using the limit() method and how to limit the collection elements based on a condition?

Java 8 Stream limit() Method Example

Sunday, August 16, 2020

Spring Boot Scheduling Tasks With @Scheduled Annotation

1. Introduction

In this tutorial, We'll learn how to run scheduled jobs in Spring Boot. There are some scenarios, you need to perform some tasks periodically at a fixed interval of time. Spring boot provides mainly with @Scheduled fixedRate and fixedDelay attributes.

In fact, Spring Boot bundled with two annotations those support scheduling at a given time.

@EnableScheduling and @Scheduled annotations do the job in spring boot. 

First, @EnableScheduling should be applied to the SpringBootApplication.
Next, @Scheduled must be applied on any method but that method should not take any arguments and should not return any value hence void should be return type.


Spring Boot Scheduling Tasks Examples - @Scheduled fixedRate Vs fixedDelay


Java 8 Stream skip() Examples

1. Overview

In this article, You'll learn how to use stream skip() method of java 8 and how to skip the first n objects from stream.

Java 8 Stream skip() Examples