Skip to main content

Posts

Showing posts from September, 2023

Efficiently Removing Duplicates from Array , ArrayList and List of Employee using Java 8 Streams

  Introduction: In modern Java programming, streams are a powerful tool for working with collections in a concise and efficient way. Removing Duplicates from Arrays using Java 8 Streams: Suppose you have an array of integers with duplicates, and you want to create a new array containing only distinct values then follow below steps Convert the array into a stream. Use the distinct() method to eliminate duplicates. Convert the stream back into an array using toArray() . import java.util.Arrays; public class RemoveDuplicatesFromArray { public static void main (String[] args) { Integer[] array = { 1 , 2 , 2 , 3 , 4 , 4 , 5 }; // Step 1: Convert the array into a stream. // Step 2: Use the `distinct()` method to eliminate duplicates. // Step 3: Convert the stream back into an array using `toArray()`. Integer[] distinctArray = Arrays.stream(array) .distinct() .toArray(Integer[]:: new ); Syst...

Difference between HashMap vs. HashSet and HashMap vs. Concurrent HashMap in Java?

In Java, HashMap, HashSet, and Concurrent HashMap are essential components of the Java Collections Framework, each serving distinct purposes when it comes to storing and managing data. In this blog post, we will explore the differences between HashMap vs. HashSet and HashMap vs. Concurrent HashMap, complete with examples to clarify their use cases. HashMap vs. HashSet HashMap HashMap is a data structure used to store key-value pairs. It uses a hashing mechanism to quickly retrieve values based on their keys. Here are some key characteristics of HashMap: Allows duplicate values but not duplicate keys. Provides efficient key-value retrieval using hashing. Does not maintain order (the order of elements is not guaranteed). Accepts null values and a single null key. HashSet HashSet , on the other hand, is used to store a collection of unique values. It does not allow duplicate elements. Here are some key characteristics of HashSet: Stores only unique values (no duplicate elements). Does no...

Popular posts from this blog

Subscribe to get new posts

Name

Email *

Message *