To get size of HashMap we use the int size() method provided by Map API. The size method returns the number of key/value pairs that are available in that Map. Example : Get
How to Check if value exists in HashMap
To check if Value exists in HashMap, we use the containsValue(Object key) method provided by HashMap. boolean containsValue(Object key) searches the HashMap for the given value and returns
How to Check if key exists in HashMap
To check if Key exists in HashMap, we use the containsKey(Object key) method provided by HashMap. boolean containsKey(Object key) searches the HashMap for the given key and returns boole
How to remove elements from HashMap
To remove elements from HashMap we can use either of the 2 ways 1. Use the remove(Object key) method 2. Remove elements while iterating the HashMap Lets see some examples for both these ways 1. Use
How to add elements in HashMap (Key/Value)
To add elements in HashMap we use V Map.put(K key, V value) method. Elements in Hashmap are added in a Key/Value pair. Points to Remember about put() method 1. put() method maps the given
How to convert HashSet to ArrayList
In this post we will see examples to convert HashSet to ArrayList in java. HashSet and ArrayList are both Collection objects. Hence we can pass HashSet to the construtor of the ArrayList and get it
How to check if HashSet contains an element
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()); } } |