Java HashSet - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

Java HashSet

 

HashSet:

The HashSet is part of collection framework and it is available in “java.util” package.

HashSet implements Set, Cloneable, Serializable interfaces and extends AbstractSet class.

HashSet uses Hash table data structure.

HashSet contains unique elements only if we try to insert duplicates, we won’t get compile time or runtime errors, add() method simply returns false value.

HashSet doesn't maintain the insertion order, here, elements are inserted based on their hash code.

HashSet stores the elements by using a mechanism called hashing.

HashSet allows one null value.

The initial default capacity of HashSet is 16, and the load factor is 0.75, here load factor means always load factory value should be 0-1.

suppose the elements of hash set moved to another hash table that is double the size of the original once it is filled by 75%.

HashSet is not synchronized i.e., if multiple threads access the hash set at the same time and one of the threads modifies the hash set than it must be externally synchronized.

HashSet is the best approach for search operations.


Creation of Hashset:

syntax:

Hashset<E> hs = new Hashset<E>();

Hashset<E> hs = new Hashset<E>(int capacity);

 

Methods of Hashset:

Some of the important methods in HashSet

Boolean add(Element obj): This method is used to place the specified element into the set.

Boolean remove(Element obj): This method is used to delete the specified element from the set. If it is available.

Boolean contains(Element obj): This method return true if the specified element is available in the set.

Boolean isEmpty(): This method return true if the set isEmpty.

intsize(): This method returns the count of the no.of elements available in the set.

void clear(): This method is used to delete all the elements from the set.

 

 Java Code:


Output:

No comments:

Post a Comment