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()); } } |
How to check if File is Directory in Java
We have 2 ways to check if file is Directory in java 1. java.io.File API’s isDirectory() method 2. java.nio.file.Files API’s [c
How to check if file is Hidden in java
To check if file is hidden in java, we use java.io.File API’s isHidden() method. In this post we will see example of how we use isHidden method i
How to get Creation Time of File in Java
To get creation time of file in java we can use the java.nio.file.Files API’s readAttributes() method. Example : Get Creation Time of File in Java [crayon-68e