Showing posts with label Collection. Show all posts
Showing posts with label Collection. Show all posts

Tuesday, December 28, 2021

How to compare two HashSet for equality in Java 8? (Fastest Way)

1. Overview


In this article, You'll learn how to compare two Sets in Java using Stream API.

Java HashSet class equals() method takes Object but when you are calling on a set object then you must pass the HashSet or Set implementation object.

Compares the specified object with this set for equality. Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set). This definition ensures that the equals method works properly across different implementations of the set interface.

Let us see the examples using equals(), containsAll() method of the Set interface. In the end, We'll explore how to compare two HashSet in java 8.

How to compare two HashSet for equality in Java 8? (Fastest Way)


Saturday, November 27, 2021

Java Set to String

1. Overview

In this tutorial, We'll learn how to convert Set to String in java with examples.

Example programs on HashSet<String> to String and LinkedHashSet<String> to String.

And also HashSet<Integer> to String with Java 8 Stream api methods.

Java Set to String

Java - Create and Iterate List of Lists With Examples

1. Overview

In this tutorial, We'll learn how to create the List of Lists and iterate them in java and the newer java 8 version with simple example programs.


This is like List is holding another list of strings or list of integers or in some cases it can be user-defined custom objects.

Let us look at each type of object creation and loop through them in java.

Java - Create and Iterate List of Lists With Examples

Friday, November 19, 2021

Java Set Add - Set.add() Adding values

1. Overview

In this tutorial, We'll learn how to add values to set in java. Set implementations are HashSet and LinkedHashSet and TreeSet.

In the next sections, how add() method is used from set class.
Java Set Add - Set.add() Adding values


Sunday, June 13, 2021

How to compare two ArrayList for equality in Java 8? ArrayList equals() or containsAll() methods works?

1. Introduction


In this article, We'll learn how to compare two ArrayLists for checking Objects equality.


ArrayList has an equal() method that takes one argument type of Object. This equals() method compares the passed list object with the current list object. If both lists are having same values then it returns true, otherwise false. equals()

Read more on how to compare two strings lexicographically.

Example programs are shown on equals(), containsAll() and java 8 Stream API.

At the end of the article you will be good with comparing two lists and find differences between two lists in java


How to compare two ArrayList for equality in Java 8? ArrayList equals() or containsAll() methods works?


Saturday, April 4, 2020

LinkedHashMap in Java With Example Programs + Java 8 New Methods

1. Introduction


In this tutorial, We'll learn LinkedHashMap API in java with example programs.

LinkedHashMap is used to store the key-value pairs as similar to the HashMap. But, LinkedHashMap extends HashMap and AbstractMap implements Map interface. The main advantage of LinkedHashMap is it preserves the insertion order that meanwhile iterating it pull the Entry Objects how the order keys are inserted into LinkedHashMap.

Iterating over collection in java

We have discussed HashMap in-depth with all methods examples including java 8 concepts. It is good to go over the HashMap article that makes you answer interview questions on all of its methods.


If you do not understand at any point just leave your question in comments.

LinkedHashMap in Java With Example Programs + Java 8 New Methods


Monday, May 27, 2019

Removing All Duplicate Values from ArrayList including Java 8 Streams

1. Overview

In this tutorial, We'll learn how to clean up the duplicate elements from ArrayList. Many of the scenarios we do encounter the same in real time projects. But, this can be resolved in many ways.

We will start with Set first then next using new List with using built in method contains() before adding each value to newList.

Next, will see duplicate elements removal with new java 8 concept Streams.

Then later, interestingly we have a list with Employee objects. Employee has id and name. Some Employee objects are added to list. Later realized that list has now duplicate employee based on the id. Now how to remove custom duplicate objects from ArrayList.

Removing All Duplicate Values from ArrayList including Java 8 Streams

Thursday, September 21, 2017

Java API ArrayList Example

Java API ArrayList Overview

In this tutorial, We'll learn about Java Collection API ArrayList.
ArrayList is a class which is widely used in real time applications. It is in package java.util and public class. This is added in java 1.2 version.

Java API ArrayList Example


Class Name: ArrayList
First Version: 1.2
Super Class: AbstractList
Implemented interfaces: List, RandomAccess, Cloneable, Serializable
Pacakge: java.util.