In this post we will see examples on how to check if Hashset contains an element/object. For this we will use the boolean contains(Object o) method. Example : Check if HashSet contains the
How to remove Elements from HashSet
In this post we will see various ways to remove elements from HashSet in java. We can either use remove(Object o) OR an Iterator<E> 1. Example : Remove Elem
How to Iterate HashSet in java
In this post we will see how to iterate HashSet in java. We can iterate HashSet either using 1. For loop 2. Iterator. We will see examples for all these methods. 1. Iterate HashSet in Java using Fo
How to initialize HashSet in java
We have many ways to initialize HashSet in java. HashSet can be initialized in one line, Using constructors, Using Arrays. 1. Initialize HashSet using Constructors
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes.sampleproject; import java.util.HashSet; import java.util.Set; public class HashSetInitalize { public static void main(String[] args) { Set<String> hashSet = new HashSet<>(); hashSet.add("Java"); hashSet.add("Spring"); hashSet.add("Hibernate"); hashSet.add("JavaScript"); System.out.println(hashSet.toString()); } } |
How to convert ArrayList to HashSet
In this post we will see examples to convert ArrayList to HashSet in java. HashSet and ArrayList are both Collection objects. Hence we can pass ArrayList to the construtor of the HashSet and get it
How to convert ArrayList to Array
In this post we will see examples on how to convert ArrayList to Array in java. To convert ArrayList to Array we will use the toArray method from ArrayList.
1 |
public <T> T[] toArray(T[] a) |
How to create Sublist from ArrayList
To create Sublist from ArrayList in java, we need to use the subList(int fromIndex,int toIndex) method provided by ArrayList. We will see the example on how to use the subList method. [crayon-68e94
ArrayList ensureCapacity example
If you have an ArrayList that already has lots of elements and you are trying to add more elements to the ArrayList, this may degrade the performance of the system. To improve the performance, you
How to remove elements from ArrayList
In this post we will see various ways to remove elements from ArrayList in java. ArrayList provides following methods to remove elements. 1. E remove(int index) Removes an element fr
How to add elements in ArrayList
In this post we will see various ways to add elements in ArrayList in java. ArrayList provides following methods to add elements. 1. boolean add(E e) Adds an element at the end of