- How To Add Unique Objects In Java HashSet
- What is HashSet
- Elements order in HashSet
- Performance of HashSet
- Accessing in Multi-threaded Environment
- HashSet is Fail-fast
- Internal Working of HashSet
- HashSet Example
- Output
- Adding Custom Objects to HashSet
- Add Unique Objects
- Output
- How to store unique values in Java using Set?
- How to store unique values in Java using Set?
- Store unique values in Java using Set interface
- Introduction to HashSet in Java
- Methods of HashSet
- Tutorials
How To Add Unique Objects In Java HashSet
Here in this example I am going to show you how to add unique objects in Java HashSet. Set interface in Java maintains uniqueness, so any duplicacy is not allowed in Set interface. The HashSet class maintains uniqueness through HashMap, so HashSet uses internally HashMap to determine unique elements.
When you add String type data or any primitive type data into HashSet then it is fine that your elements will be unique in HashSet but what if you add custom objects to HashSet; how uniqueness will be maintain? Based on which field of your Java class, the HashSet should determine the uniqueness? In this case you need to override equals() and hashCode() to maintain the uniqueness.
What is HashSet
- HashSet extends AbstractSet and is an implementation of Set interface.
- HashSet also implements Serializable and Cloneable interfaces.
- HashSet is backed by hash table(actually HashMap instance), i.e., the HashSet uses hash table(HashMap) to store collection elements.
- Like HashMap, it allows null only one element.
Elements order in HashSet
HashSet makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.
Performance of HashSet
This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets.
Like HashMap, two parameters that affect the performance of the HashSet instance: capacity and load factor.
The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created.
The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed, i.e., internal data structures are rebuilt, so that the hash table has approximately twice the number of buckets.
Iteration over this set requires time propertional to the sum of HashSet’s instances size(number of elements) plus the capacity (the number of buckets) of the HashMap. Therefore, it is highly recommended not to set the initial capacity too high (or load factor too low) if iteration performance is important.
As a general thumb of rule, the default load factor (.75) offers a good tradeoff between time and space costs.
Accessing in Multi-threaded Environment
Note that the HashSet implementation is not synchronized. So multiple threads access a set concurrently, and at least one of the threads modifies the set structurally, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be “wrapped” using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the set:
Set s = Collections.synchronizedSet(new HashSet(. ));
HashSet is Fail-fast
If the set is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
The fail-fast behavior of an iterator cannot be guaranteed and iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
Internal Working of HashSet
When you look into the HashSet.java class’s source code, you find something similar to below code:
public class HashSet extends AbstractSet implements Set, Cloneable, java.io.Serializable < private transient HashMapmap; // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object(); public HashSet() < map = new HashMap<>(); > public boolean add(E e) < return map.put(e, PRESENT)==null; >/** * Some other code */ >
It is clear from the above source code that the set achieves uniqueness through HashMap. You might know that each element in HashMap is unique. So when an instance of HashSet is created, it basically creates an instance of HashMap. When an element is added to the HashSet, it is actually added to the HashMap as a key using add(E e) method. Now a value need to be associated with key, so a dummy value PRESET (private static final Object PRESENT = new Object();) is associated with the every key in HashMap.
Now look at the add(E e) method:
So here there will be possibly two cases:
- map.put(e,PRESENT) will return null, if element is not present in the map. So map.put(e, PRESENT) == null will return true, hence add method will return true and element will be added in HashSet.
- map.put(e,PRESENT) will return old value, if the element is already present in the map. So map.put(e, PRESENT) == null will return false, hence add method will return false and element will not be added in HashSet.
HashSet Example
package com.roytuts.collections; import java.util.HashSet; import java.util.Set; public class HashSetExample < public static void main(String[] args) < Setset = new HashSet<>(); set.add("a"); set.add("b"); set.add("c"); set.add("A"); set.add("B"); set.add("C"); set.add("a"); System.out.println(set); > >
Output
So what happened when I passes duplicate element(set.add(e)) to the HashSet. The add(e) method in HashSet returns false when the element exists in the HashSet, otherwise it returns true. Therefore it did not added the duplicate element to the HashSet.
Adding Custom Objects to HashSet
It is really important to override equals() and hashCode() for any object you are going to store in HashSet. Because the object is used as key in map, must override those method to maintain uniqueness of elements or objects.
Add Unique Objects
Create Employee object. Notice I have override equals() and hasCode() method through which I am making Employee object unique based on attributes – name and address .
public class Employee < private int id; private String name; private String address; public Employee() < >public Employee(int id, String name, String address) < this.id = id; this.name = name; this.address = address; >public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String name) < this.name = name; >public String getAddress() < return address; >public void setAddress(String address) < this.address = address; >@Override public int hashCode() < return Objects.hash(address, name); >@Override public boolean equals(Object obj) < if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; return Objects.equals(address, other.address) && Objects.equals(name, other.name); >@Override public String toString() < return "Employee [id=" + id + ", name=" + name + ", address=" + address + "]"; >>
Create HashSet test class to test the uniqueness of objects added to the HashSet.
public class HashsetApp < public static void main(String[] args) < Employee e1 = new Employee(1000, "Liton", "Falakata"); Employee e2 = new Employee(1001, "Liton", "Falakata"); Employee e3 = new Employee(1000, "Liton", "Falakata"); Employee e4 = new Employee(1003, "Debabrata", "Birati"); Employee e5 = new Employee(1000, "Souvik", "Kalighat"); Setset = new HashSet<>(); set.add(e1); set.add(e2); set.add(e3); set.add(e4); set.add(e5); // for (Employee employee : set) < // System.out.println(employee); // >set.stream().forEach(s -> System.out.println(s)); > >
Output
Running the above main class will produce the following output:
Employee [id=1000, name=Souvik, address=Kalighat] Employee [id=1000, name=Liton, address=Falakata] Employee [id=1003, name=Debabrata, address=Birati]
If you do not override hashCode() and equals() method, the output will be:
Employee [id=1003, name=Debabrata, address=Birati] Employee [id=1000, name=Souvik, address=Kalighat] Employee [id=1000, name=Liton, address=Falakata] Employee [id=1001, name=Liton, address=Falakata] Employee [id=1000, name=Liton, address=Falakata]
So you got an idea how to add unique objects in Java HashSet.
How to store unique values in Java using Set?
In this tutorial we are going to use Set in Java for storing unique values. Set interface in Java can be used to store unique values within you program.
How to store unique values in Java using Set?
Store unique values in Java using Set interface
In this section we will discuss about the Set interface in Java which is designed to store unique values. We will show you how to store unique values in Java using Set? The Set interface in Java allows allows maintaining unique list in Java. Here in this example we will use HashSet to maintain list of of unique values in our program. First of all we will initialize the HashSet object and then add few values including duplicates. When we add duplicates the Set interface checks if the elements already exists if it does not already presents then it adds in the list. If value is already present then it skips and value is not added to the list. This way it maintains the unique values list in Java.
Introduction to HashSet in Java
As explained earlier Set interface in Java is used to store the unique values and HashSet is one of the most popular implementation of this interface. The HashSet is part of core Java API and it provides many methods to perform operation on the values stored in the HashSet object.
HashSet is one of the fundamental data structure in Java and its main features are:
- It is used to store unique elements
- It also permits null
- The HashSet is backed by HashMap
- The HashSet does not maintain the insertion order
- Its not thread safe so in multithreading environment it should be used carefully
Methods of HashSet
HashSet provides following important methods to perform various operations on the HashSet object:
- The add() method — This method is used to add an object to the HashSet object. If duplicate value is sent in the add method, it simply ignores the value.
- The contains() method — This method is used to find if HashSet object contains the specified value. It returns true if value is present or false if value is already not present in the Set object.
- The remove() method — This method is used to removed a value from the HashSet object. It returns true if values was present and removed.
- The clear() method — This method is used to clear the object data.
- The size() method — This method is used to find out the count of values stores in the HashSet object.
- The isEmpty() — This method is used to find if the object is empty.
We have seen some of the important methods of HashSet object. Now lets write simple program to store unique values in the HashSet object and then print the data. We are going to store same values two times and then print the data. When we iterate and print the data it only prints the unique values.
Here is the example program to store unique values in Java using HashSet:
package net.roseindia; // Web: https://www.roseindia.net import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class UniqueValues < public static void main(String[] args) < Setset = new HashSet (); set.add("One"); set.add("Two"); set.add("Three"); set.add("One"); set.add("Two"); set.add("Three"); Iterator itr = set.iterator(); while(itr.hasNext()) < String value = itr.next(); System.out.println(value); >> >
In the above tutorial we are instantiating the object of HashSet:
Then adding following values two times:
set.add("One"); set.add("Two"); set.add("Three");
Then printing the values stored in the HashSet object with the help of following code:
Iterator itr = set.iterator(); while(itr.hasNext())
Following is the output of the program:
From the above output of the program it is clear that the HashSet is stores unique values. If duplicate value is added multiple times, then it just ignores.
In this tutorial you have learned to store unique values in Java using the HashSet object. Keep in mind that the HashSet is not thread safe and you should use it with care in multi-threading environment.
Here are more tutorials related to HashSet:
Tutorials
- Classes and Interfaces of the I/O Streams
- Multithreading in Java
- Java ClassPath
- Making Custom (User Defined) Exceptions
- Use of Java
- Display Image in Java
- Constructor Overloading in Java
- Reference Data Types
- Java Exception — Handle Exceptions in Java
- Introduction to Java
- Nested classes: Examples and tutorials
- How to read a file line by line?
- Overview of JEE 5 Platform
- Tutorial — Sun Java System Application Server Platform Edition
- What is EJB 3.0?
- Show Coordinates
- OOPs and Its Concepts in Java
- What is an Applet
- The Life cycle of An Applet
- Java Applet — Creating First Applet Example
- Java — Drawing Shapes Example in java
- Java — Drawing Shapes Example using color in java
- Java — Event Listeners Example in Java Applet
- Applet — Passing Parameter in Java Applet
- Opening a URL from an Applet
- Java — Opening a url in new window from an applet
- Applet is not Working
- Display image in the applet
- Applet Write Files Example
- Play Audio in Java Applet
- Security Issues with the Applet
- Swing Applet Example in java
- The Sample Banner Example in Java
- Clock Applet in Java
- What is Java Swing?
- Creating a Frame
- Setting an Icon for a Frame in Java
- Show Dialog Box in Java
- Show message and confirm dialog box
- Show input dialog box