Reputation: 11
I'm new to programming and I've got a task to make a swing/GUI crud where you are able to put in people/animal/movies into an array etc (I'm currently doing people) and it will generate mail, username etc. I've done all that but I'm missing one thing.
Now - I want to give each element in the array an unique ID of some sort where if 1 person has ID 25 for instance, so there can't be another element with the same ID 25 unless i remove that specific element.
I use a Jframe and then a java public class where i have get's and set's for my Jframe.
Sorry but I'm new - thank you.
Upvotes: 1
Views: 2565
Reputation: 4935
Java.lang.Object has methods called hasCode()
and equals()
. These methods play a significant role in the real time application. However its use is not always common to all applications.
hashCode()
As you know this method provides the has code of an object. Basically the default implementation of hashCode()
provided by Object
is derived by mapping the memory address to an integer value. If look into the source of Object
class , you will find the following code for the hashCode
. public native int hashCode()
; It indicates that hashCode
is the native implementation which provides the memory address to a certain extent. However it is possible to override the hashCode
method in your implementation class.
equals()
This particular method is used to make equal comparison between two objects. There are two types of comparisons in Java
. One is using “= =”
operator and another is “equals()”
. I hope that you know the difference between this two. More specifically the “.equals()”
refers to equivalence relations. So in broad sense you say that two objects are equivalent they satisfy the “equals()”
condition. If you look into the source code of Object class you will find the following code for the equals()
method.
So, lets create a class Person
overriding these methods:
public class Person {
private Integer personId;
private String fullName;
public Integer getPersonId() {
return personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
@Override
public String toString() {
return "Person [personId=" + personId + ", fullName="
+ fullName + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((fullName == null) ? 0 : fullName.hashCode());
result = prime * result + ((personId == null) ? 0 : personId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (fullName == null) {
if (other.fullName != null)
return false;
} else if (!fullName.equals(other.fullName))
return false;
if (personId == null) {
if (other.personId != null)
return false;
} else if (!personId.equals(other.personId))
return false;
return true;
}
}
and now our main class to create and manage duplicate objects of class Person:
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class UniqueArrayExample {
//To remove duplicates objects we use here HashSet util collection
private static Set<Person> personSet = new HashSet<Person>();
public static void main(String[] args) {
//creating four objects of Person Class
Person person1 = new Person();
Person person2 = new Person();
Person person3Dupl = new Person();
Person person4 = new Person();
//third object person3Dup1 is duplicate of Object person1
person1.setPersonId(12341);
person2.setPersonId(12342);
person3Dupl.setPersonId(12341);
person4.setPersonId(12344);
person1.setFullName("Suresh Kumar");
person2.setFullName("Mahesh Singh");
person3Dupl.setFullName("Suresh Kumar");
person4.setFullName("Rajkumar Singh");
//Add those Person class Object to Set<Person> collection
personSet.add(person1);
personSet.add(person2);
personSet.add(person3Dupl);
personSet.add(person4);
//So here after getting all Objects to Iterator and by
//printing those will not give you the person1 Object duplicate.
Iterator<Person> iterator = personSet.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next().toString());
}
}
}
here output generated as above class:
Person [personId=12342, fullName=Mahesh Singh]
Person [personId=12341, fullName=Suresh Kumar]
Person [personId=12344, fullName=Rajkumar Singh]
the duplicate object possibles to remove by use of HashSet
, because of overriding hashCode()
and equals()
methods inside class Person
, if you remove those methods from the class Person
, Set
collection will add all Objects
of Person
and will remove the redundancy.
Hope this would help you.
Upvotes: 1