从两个哈希集中删除重复项

时间:2013-03-05 04:20:40

标签: java set hashset

在我的情况下,我有两个哈希集,一组包含朋友的姓名和号码,另一组 包含现有朋友的姓名和号码,这些数据从db中检索并存储到集合中 如何比较和删除存储在新集中的拖曳集和交集数据中的重复项? 我需要谷歌的代码我在比较器中看到比较器他们只使用了一套但在我的情况下我正在检查两套抱歉我的坏英语

4 个答案:

答案 0 :(得分:4)

假设我们有set1和set2,我们可以执行以下操作

    Set set3 = new HashSet(set1);
    set3.retainAll(set2);    
    set1.removeAll(set3);
    set2.removeAll(set3);

在你的情况下这是

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);
}

输出

[Mani-444, Mani-222]
[Raju-3333]

答案 1 :(得分:3)

试试这个

 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());
 }

答案 2 :(得分:1)

我对您的要求的理解是从存储到新集合中的两个集合和交集数据中删除重复项。

我的方法是在Friend类中覆盖hashCode()和equals()方法。 然后我创建具有这样的retainAll()方法的交集数据的commonFriends。 修改 - 在两个集合中获取剩余数据

Set<Friend> commonFriend = new HashSet<Friend>();
commonFriend.addAll(f);
commonFriend.retainAll(set2);
// Find remaining data
set2.removeAll(commonFriend);
f.removeAll(commonFriend);

详细编码

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;
    }
}

答案 3 :(得分:0)

您可以使用

  

addAll()

集方法。这会将元素从一个集合复制到另一个集合。由于套装不允许重复,因此需要注意