How to storing Java Object In HashSet

In this tutorial we will see how to use object in hashset , map  or hashtable in java. We have read about that set uses internally map to store data.If we use user defined object as key in map or value in set then it is must  override hashcode() and equal() method . Let’s we will create one class User and store object of user in hashset

import java.util.HashSet;

public class HashSetOfObject {
	public static void main(String[] args) {

		HashSet<User> hashSet = new HashSet<User>();
		User user1 = new User("amit", 10);
		User user2 = new User("amit", 10);
		hashSet.add(user1);
		hashSet.add(user2);
		//print hashset
		System.out.println("hashset is = "+ hashSet);
	}
}

class User {

	private String name;
	private int userId;

	public User(String name, int userId) {
		super();
		this.name = name;
		this.userId = userId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	@Override
	public String toString() {
		return "[name=" + name + ", userId=" + userId + "]";
	}
	
	

}

Output :

hashset is = [[name=amit, userId=10], [name=amit, userId=10]]

As per output user1 and user2 are different object even their values are same. The solution of this problem is we must  override hashcode() and equal() method .

import java.util.HashSet;

public class HashSetOfObject {
	public static void main(String[] args) {

		HashSet<User> hashSet = new HashSet<User>();
		User user1 = new User("amit", 10);
		User user2 = new User("amit", 10);
		hashSet.add(user1);
		hashSet.add(user2);
		//print hashset
		System.out.println("hashset is = "+ hashSet);
	}
}

class User {

	private String name;
	private int userId;

	public User(String name, int userId) {
		super();
		this.name = name;
		this.userId = userId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + userId;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (userId != other.userId)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "[name=" + name + ", userId=" + userId + "]";
	}

}

Output :

hashset is = [[name=amit, userId=10]]

Now Both object are equal in java. This post have given solution for How to use object in hashset in java or How to find user defined objects from HashSet in java. You can try more hashset example in java 

Reference :

Java Doc – HashSet

 

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 51 = 52