Core JAVA
Java Runtime Environment (JRE)
Java Virtual Machine (JVM)
Java overview
Java basics
Java Objects and classes
Java Constructors
Java basic datatypes
Java variable types
Java modifiers/Access Modifiers In Java
Java Basic Operators
Java Loops and Controls
Java conditions
Java numbers and characters
Java strings
Java arrays
Java date time
Java methods
Java file and IO operations
Java exceptions
Inner class
Java OOPs Concepts
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java Interface
Cohesion and Coupling
Association, Aggregation and Composition
Java Collections
Java ArrayList
Java LinkedList
Set and HashSet
LinkedHashSet and TreeSet
Queue and PriorityQueue
Deque and PriorityQueue
Java Map Interface
Java HashMap
Internal Working Of Java HashMap
Java Mutithread
Methods of Thread In Java
Join , run & Start Method in Threads
Difference b/w start & run Methods in Threads
Java Concurrency Package & its Features
CountDownLatch, CyclicBarrier, Semaphore and Mutex in Thread
Java Collections: Set and HashSetÂ
Set is an interface which extends the collection interface, it represents a real world data structure set. Set is further implemented by Abstract set and then HashClass implements AbstractSet. To actually use and perform operations we always use the implementation of Set one of those implementations is HashSet.
Key points and features related to HashSet/Set:
- Java HashSet uses hashing algorithms to store values.
- HashSet always contains unique values, duplicates are simply overridden.
- It allows null.
- It is not thread safe, which means it is asynchronous.
- Ordering is not maintained, it depends on hash value.
- The default capacity is 16 and the load factor is 0.75.
HashSet Declaration
This is how we can declare java.util.HashSet
Generic
HashSet<String> mySet = new HashSet<String>();
Normal
HashSet mySet2 = new HashSet();
Assigned to Set insted HashSet
Set mySet3 = new HashSet<>();
Constructors of HashSet
SN | Constructor | Description |
1) | HashSet() | Default HashSet. |
2) | HashSet(int capacity) | HashSet with initial capacity. |
3) | HashSet(int capacity, float loadFactor) | HashSet with capacity and load factor. |
4) | HashSet(Collection<? extends E> c) | HashSet with some collections. |
Methods of Java HashSet
SN | Modifier & Type | Method | Description |
1) | boolean | add(E e) | Adds an element to set. |
2) | void | clear() | Clears elements from the set. |
3) | object | clone() | Clonses the set. |
4) | boolean | contains(Object o) | Checks if it contains an object in the set. |
5) | boolean | isEmpty() | Checks if the set is empty or not. |
6) | Iterator<E> | iterator() | Returns iterator. |
7) | boolean | remove(Object o) | Removes object from set. |
8) | int | size() | Returns the size of the set. |
9) | Spliterator<E> | spliterator() | Returns spliterator() |
HashSet Example with use of add, remove, addAll, isEmpty, iterator(), removeIf, clear
import java.util.HashSet;
import java.util.Iterator;
public class CollectionsExamples4 {
public static void main(String[] args) {
   //Generic
   HashSet<String> mySet = new HashSet<String>();
   mySet.add("A");
   mySet.add("B");
   mySet.add("C");
   mySet.add("D");
   mySet.add("D"); //duplicate
    System.out.println("HashSet after adding duplicate: "+ mySet);
   //remove
    mySet.remove("A");
   //isEmpty
   mySet.isEmpty();
   HashSet<String> mySet2 = new HashSet<String>();
    mySet2.add("ZZ");
   //addAll
   mySet.addAll(mySet2);
   //iterator of hashSet class
   Iterator itr = mySet.iterator();
   while(itr.hasNext()) {
       System.out.println(itr.next());
    }
   //remove if
     mySet.removeIf(str->str.contains("BB"));  Â
    //clear
   mySet.clear()
}
}
Output
HashSet after adding duplicate: [A, B, C, D]
ZZ
B
C
D
Create HashSet From Another List
We may create a hashSet with a list, but remember duplicate elements will be removed.
Example
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class CollectionsExamples4 {
public static void main(String[] args) {
   //Generic
   List myList = new ArrayList<String>();
   myList.add("A");
   myList.add("A");
    myList.add("B");
   HashSet<String> mySet = new HashSet(myList);
   System.out.println(mySet);
}
}
Output
[A, B]
Next Articles To Read
- LinkedHashSet and TreeSet In Java
Â
Â