Reputation: 69
In my case i have two hash sets one set contain friend's name and number and another set
contain existing friend's name and number this data retrieved from db and stored into set
how to compare and removing duplicates from tow set and intersection data stored into new set?
i need code for that in google i saw the comparator in comparator they used only one set but in my case i am checking two sets sorry for my bad english
Upvotes: 3
Views: 6898
Reputation: 5946
My understanding of your requirement is that removing duplicates from two set and intersection data stored into new set.
My way is I override hashCode() and equals() method in Friend class. Then I create commonFriends that have intersection data with retainAll() method like this. Modify - getting remaining data in both set
Set<Friend> commonFriend = new HashSet<Friend>();
commonFriend.addAll(f);
commonFriend.retainAll(set2);
// Find remaining data
set2.removeAll(commonFriend);
f.removeAll(commonFriend);
Detail coding is
package examA;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
public class Test1 {
/**
* @param args
*/
public static void main(String[] args) {
Friend f1 = new Friend("Karthik", "111");
Friend f2 = new Friend("Mani", "222");
Friend f3 = new Friend("Karthik", "111");
Friend f4 = new Friend("Mani", "Manik");
Friend f5 = new Friend("Karthik", "111");
Set<Friend> f = new HashSet<Friend>();
f.add(f1);
f.add(f2);
f.add(f3);
f.add(f4);
f.add(f5);
Set<Friend> set2 = new HashSet<Friend>();
set2.add(new Friend("Karthik", "111"));
set2.add(new Friend("Raju", "3333"));
set2.add(new Friend("Karthikeyan", "111"));
set2.add(new Friend("Raju", "3333"));
System.out.println("Set 1 size" + f.size());
System.out.println("Set 2 size" + set2.size());
Set<Friend> commonFriend = new HashSet<Friend>();
commonFriend.addAll(f);
commonFriend.retainAll(set2);
System.out.println("Common size" + commonFriend.size());
f.removeAll(commonFriend);
System.out.println("Remaining Data in f" + f.size());
set2.removeAll(commonFriend);
System.out.println("Remaining Data in set2" + set2.size());
for (Friend friend : commonFriend) {
System.out.println(friend);
}
}
}
class Friend {
private String name;
private String number;
public Friend(String name, String number) {
this.name = name;
this.number = number;
}
@Override
public String toString() {
return "Friend [name=" + name + ", number=" + number + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((number == null) ? 0 : number.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;
Friend other = (Friend) obj;
if (number == null) {
if (other.number != null)
return false;
} else if (!number.equals(other.number))
return false;
return true;
}
}
Upvotes: 1
Reputation: 136002
Assuming we have set1 and set2 we can do the following
Set set3 = new HashSet(set1);
set3.retainAll(set2);
set1.removeAll(set3);
set2.removeAll(set3);
in your case this is
public class Friend {
String name;
String number;
public Friend(String name, String number) {
this.name = name;
this.number = number;
}
@Override
public int hashCode() {
return name.hashCode() + 31 * number.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Friend)) {
return false;
}
Friend other = (Friend)obj;
return other.number.equals(number) && other.name.equals(name);
}
@Override
public String toString() {
return name + "-" + number;
}
}
public static void main(String[] args) throws Exception {
Set<Friend> set1 = new HashSet<Friend>();
set1.add(new Friend("Karthik", "111"));
set1.add(new Friend("Mani", "222"));
set1.add(new Friend("Karthik", "111"));
set1.add(new Friend("Mani", "444"));
set1.add(new Friend("Karthik", "111"));
Set<Friend> set2 = new HashSet<Friend>();
set2.add(new Friend("Karthik", "111"));
set2.add(new Friend("Raju", "3333"));
Set<Friend> set3 = new HashSet<Friend>(set1);
set3.retainAll(set2);
set1.removeAll(set3);
set2.removeAll(set3);
System.out.println(set1);
System.out.println(set2);
}
output
[Mani-444, Mani-222]
[Raju-3333]
Upvotes: 4
Reputation: 20741
Try this
Friend f1=new Friend("Karthik",111);
Friend f2=new Friend("Mani",222);
Friend f3=new Friend("Karthik",111);
Friend f4=new Friend("Mani",222);
Friend f5=new Friend("Karthik",111);
Set<Friend> set1=new HashSet<Friend>();
set1.add(f1);
set1.add(f2);
set1.add(f3);
set1.add(f4);
set1.add(f5);
Set<Friend> set2=new HashSet<Friend>();
set2.add(new Friend("Karthik", 111));
set2.add(new Friend("Raju", 333));
set1.addAll(set2); //Adding all the objects from set2 to set1
Set<Friend> set3=new HashSet<Friend>(); // creating a new Set object
/////////// Logic for removing duplicates ///////////////////////
Set mine=new HashSet();
for (Iterator<Friend> it = set1.iterator(); it.hasNext();) {
Friend s = it.next();
if(mine.add(s.getName()))
set3.add(new Friend(s.getName(),s.getId()));
}
/////////// Now set3 contains non-duplicates objects ///////////
for (Iterator<Friend> it = set3.iterator(); it.hasNext();) {
Friend s = it.next();
System.out.println("Name:"+s.getName()+" Id:"+s.getId());
}
Upvotes: 3