set接口修改判断重复的标准, 自定义判断重复标准

工作中遇到一个问题,我在javaeye中也提问了,但是还是自己搞定了。这里和大家分享下!
假如有一个类,Stu代表学生类,有编号和姓名(sid,sname,sage)。
假如需求是查找姓名为'corleone'或者年龄为23的学生的信息。
按照两个条件查到的集合累加的话则同时满足两个条件的学生就被算了两次。显然是重复的。

写了如下的例子来模拟下

Java代码 复制代码
  1. public class Stu {   
  2.     private Integer sage;   
  3.     private String sname;   
  4.     private Integer sid;   
  5.   
  6.     public Stu(Integer sage, String sname, Integer sid) {   
  7.         super();   
  8.         this.sage = sage;   
  9.         this.sname = sname;   
  10.         this.sid = sid;   
  11.     }   
  12.   
  13.     public Stu() {   
  14.         super();   
  15.     }   
  16.   
  17.     public Integer getSage() {   
  18.         return sage;   
  19.     }   
  20.   
  21.     public void setSage(Integer sage) {   
  22.         this.sage = sage;   
  23.     }   
  24.   
  25.     public String getSname() {   
  26.         return sname;   
  27.     }   
  28.   
  29.     public void setSname(String sname) {   
  30.         this.sname = sname;   
  31.     }   
  32.   
  33.     public Integer getSid() {   
  34.         return sid;   
  35.     }   
  36.   
  37.     public void setSid(Integer sid) {   
  38.         this.sid = sid;   
  39.     }   
  40. }  
public class Stu {
	private Integer sage;
	private String sname;
	private Integer sid;

	public Stu(Integer sage, String sname, Integer sid) {
		super();
		this.sage = sage;
		this.sname = sname;
		this.sid = sid;
	}

	public Stu() {
		super();
	}

	public Integer getSage() {
		return sage;
	}

	public void setSage(Integer sage) {
		this.sage = sage;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}
}


这个是测试类:

Java代码 复制代码
  1. import java.util.ArrayList;   
  2. import java.util.HashSet;   
  3. import java.util.List;   
  4. import java.util.Set;   
  5. class Test {   
  6.   
  7.     public static void main(String[] args) {   
  8.         List<Stu> stus1 = createStus(null);   
  9.         List<Stu> stus2 = createStus(1);   
  10.         Set<Stu> stus = new HashSet<Stu>();   
  11.         stus.addAll(stus1);   
  12.         stus.addAll(stus2);   
  13.         for (Stu stu : stus) {   
  14.             System.out.println(stu.getSage() + ".." + stu.getSid() + ".."  
  15.                     + stu.getSname());   
  16.         }   
  17.     }   
  18.   
  19.     public static List<Stu> createStus(Integer type) {   
  20.         if (null == type) {   
  21.             List<Stu> stus = new ArrayList<Stu>();   
  22.             stus.add(new Stu(23"corleone"1));   
  23.             stus.add(new Stu(14"corleone"3));   
  24.             return stus;   
  25.         } else {   
  26.             List<Stu> stus = new ArrayList<Stu>();   
  27.             stus.add(new Stu(23"corleone"1));   
  28.             stus.add(new Stu(23"OX"2));   
  29.             return stus;   
  30.         }   
  31.     }   
  32. }  
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
class Test {

	public static void main(String[] args) {
		List<Stu> stus1 = createStus(null);
		List<Stu> stus2 = createStus(1);
		Set<Stu> stus = new HashSet<Stu>();
		stus.addAll(stus1);
		stus.addAll(stus2);
		for (Stu stu : stus) {
			System.out.println(stu.getSage() + ".." + stu.getSid() + ".."
					+ stu.getSname());
		}
	}

	public static List<Stu> createStus(Integer type) {
		if (null == type) {
			List<Stu> stus = new ArrayList<Stu>();
			stus.add(new Stu(23, "corleone", 1));
			stus.add(new Stu(14, "corleone", 3));
			return stus;
		} else {
			List<Stu> stus = new ArrayList<Stu>();
			stus.add(new Stu(23, "corleone", 1));
			stus.add(new Stu(23, "OX", 2));
			return stus;
		}
	}
}


显然得到的结果是有重复的。
于是想,Set可以有这样的功能,肯定是有判断重复的方法的,这里大家都想到了contains(),对不?
就看JDK源码:
JDK1.5源码如下:

Java代码 复制代码
  1. public boolean contains(Object o) {   
  2.     return map.containsKey(o);   
  3.     }  
public boolean contains(Object o) {
	return map.containsKey(o);
    }


发现Set接口下的HashSet实现类的contains()取决于map.containsKey(o);
containsKey()是不是眼熟,估计是Map的实现类。那map到底是个啥?
果然,查看结果如下

Java代码 复制代码
  1. private transient HashMap<E,Object> map;  
 private transient HashMap<E,Object> map;


是HashMap,那控制唯一的手段应该就是通过HashMap的key-value控制key唯一来实现对象唯一的。
接着就想,如果改变了这个key,并且指定key是上述例子中Stu的sid的值的话,岂不是就可以达到想要的效果了?
接着发现:这个HashSet中的HashMap的key是HashSet的泛型入参E

Java代码 复制代码
  1. public class HashSet<E>   
  2.     extends AbstractSet<E>   
  3.     implements Set<E>, Cloneable, java.io.Serializable  
public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable


这样的话,不再无从下手了。
那这个E是指什么呢?因为是唯一的,而且相同的对象是被判断为重复(无论属性是不是相同)所以我首先想到的是toString()。
这样我再次查看源码:

Java代码 复制代码
  1. public String toString() {   
  2.     return getClass().getName() + "@" + Integer.toHexString(hashCode());   
  3.     }  
public String toString() {
	return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }


这时才明白,默认的toString是类名和hashCode()的拼接。
结论就是这个key就是hashCode()的返回结果。
尝试修改代码……Stu类重写hashCode(),使返回逻辑唯一的sid属性值

Java代码 复制代码
  1.        @Override  
  2. ublic int hashCode() {   
  3. return this.getSid();   
         @Override
	public int hashCode() {
		return this.getSid();
	}


testing


  • 23..1..corleone
    14..3..corleone
    23..1..corleone
    23..2..OX


再次尝试,failed!重复的sid依然存在,我感觉到它在嘲笑我,还不止一个!!
于是继续振作起来,到底是哪里出问题了?
思前想后,出现这个问题的原因有两个
1、到底以上修改有没有让sid成为key?
2、如果1实现了,那又是什么原因导致依然重复的呢?
于是修改代码:
再写个测试类:

Java代码 复制代码
  1. import java.util.LinkedHashSet;   
  2. import java.util.Set;   
  3. public class T {   
  4.     public static void main(String[] args) {   
  5.         Stu stu = new Stu(23"corleone"1);   
  6.         Set<Stu> stus = new LinkedHashSet<Stu>();   
  7.         stus.add(stu);   
  8.         stu.setSid(20);   
  9.         stus.add(stu);   
  10.         System.out.println(stus.size());   
  11.         for (Stu s : stus) {   
  12.             System.out.println(s.getSage() + ".." + s.getSid() + ".."  
  13.                     + s.getSname()+".. hashCode(): "+s.hashCode());   
  14.         }   
  15.     }   
  16. }  
import java.util.LinkedHashSet;
import java.util.Set;
public class T {
	public static void main(String[] args) {
		Stu stu = new Stu(23, "corleone", 1);
		Set<Stu> stus = new LinkedHashSet<Stu>();
		stus.add(stu);
		stu.setSid(20);
		stus.add(stu);
		System.out.println(stus.size());
		for (Stu s : stus) {
			System.out.println(s.getSage() + ".." + s.getSid() + ".."
					+ s.getSname()+".. hashCode(): "+s.hashCode());
		}
	}
}


运行结果如下:


  • 2
    23..20..corleone.. hashCode(): 20
    23..20..corleone.. hashCode(): 20


结论是:显然易见,唯一判断属性值改变,即使是同个对象,只要hashCode()不一样就可以做为两个不同的对象。但是两个对象的所有属性全部一致,这个大家很容易想到答案,因为对象是引用类型的。
那么第一个疑点被排除,偶们是成功让sid成为key的。
那么我们再来研究疑问2
再看源码:
HashSet<E> 的add(E o)

Java代码 复制代码
  1. public boolean add(E o) {   
  2.     return map.put(o, PRESENT)==null;   
  3.     }  
public boolean add(E o) {
	return map.put(o, PRESENT)==null;
    }


在看调用的HashMap<K,V>的put(K key, V value)

Java代码 复制代码
  1. public V put(K key, V value) {   
  2.     if (key == null)   
  3.         return putForNullKey(value);   
  4.         int hash = hash(key.hashCode());   
  5.         int i = indexFor(hash, table.length);   
  6.         for (Entry<K,V> e = table[i]; e != null; e = e.next) {   
  7.             Object k;   
  8.             if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {   
  9.                 V oldValue = e.value;   
  10.                 e.value = value;   
  11.                 e.recordAccess(this);   
  12.                 return oldValue;   
  13.             }   
  14.         }   
  15.         modCount++;   
  16.         addEntry(hash, key, value, i);   
  17.         return null;   
  18.     }  
public V put(K key, V value) {
	if (key == null)
	    return putForNullKey(value);
        int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }
        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }


我是细细研究了一番,是不是大家也有和我一样的想法了?既然是已经成功修改了key的取值,那很大的可能就是比较的时候出问题了。于是偶就把焦点放在了

Java代码 复制代码
  1. if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {   
  2.                 V oldValue = e.value;   
  3.                 e.value = value;   
  4.                 e.recordAccess(this);   
  5.                 return oldValue;   
  6.             }  
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }


仔细看可以发现如果if成立,就返回oldValue,那下面的操作都不错了。大家可以根据方法名顾名思义,那么偶又把焦点方法了这个判断的条件上了。
前面的都是没什么问题的,最后,最大嫌疑犯这个称号落在了
key.equals(k)上,大家都知道,每个类都有个从Object继承下来的equals()。
于是查看源码:Object类的equals()

Java代码 复制代码
  1. public boolean equals(Object obj) {   
  2. urn (this == obj);   
  3. }  
    public boolean equals(Object obj) {
	return (this == obj);
    }


大家都知道==比较的都是内存地址,也许有人会有疑问那String类型呢?
不妨跑下题:String的equals()

Java代码 复制代码
  1. public boolean equals(Object anObject) {   
  2.     if (this == anObject) {   
  3.         return true;   
  4.     }   
  5.     if (anObject instanceof String) {   
  6.         String anotherString = (String)anObject;   
  7.         int n = count;   
  8.         if (n == anotherString.count) {   
  9.         char v1[] = value;   
  10.         char v2[] = anotherString.value;   
  11.         int i = offset;   
  12.         int j = anotherString.offset;   
  13.         while (n-- != 0) {   
  14.             if (v1[i++] != v2[j++])   
  15.             return false;   
  16.         }   
  17.         return true;   
  18.         }   
  19.     }   
  20.     return false;   
  21.     }  
public boolean equals(Object anObject) {
	if (this == anObject) {
	    return true;
	}
	if (anObject instanceof String) {
	    String anotherString = (String)anObject;
	    int n = count;
	    if (n == anotherString.count) {
		char v1[] = value;
		char v2[] = anotherString.value;
		int i = offset;
		int j = anotherString.offset;
		while (n-- != 0) {
		    if (v1[i++] != v2[j++])
			return false;
		}
		return true;
	    }
	}
	return false;
    }


偶就不多说了。
于是偶就把Stu修改了下,重写了父类的equals():

Java代码 复制代码
  1. @Override  
  2. public boolean equals(Object obj) {   
  3.     return obj == null ? this == null ? true : false  
  4.             : obj instanceof Stu ? ((Stu) obj).getSid() == null ? this  
  5.                     .getSid() == null ? true : false : ((Stu) obj).getSid()   
  6.                     .equals(this.getSid()) : false;   
  7. }  
	@Override
	public boolean equals(Object obj) {
		return obj == null ? this == null ? true : false
				: obj instanceof Stu ? ((Stu) obj).getSid() == null ? this
						.getSid() == null ? true : false : ((Stu) obj).getSid()
						.equals(this.getSid()) : false;
	}


再次尝试……success!!!同个sid的被过滤在外了。


  • 23..1..corleone
    14..3..corleone
    23..2..OX



PS:有个问题,这里的hashCode()返回的是int类型的,但是如果是String类型的话就没办法了。我的项目上是一个32位加密的String。呵呵……依然解决不了问题。
不过Set的问题已经解决了,希望对大家有用,希望各位多动动手。
如果有别的好方法,或者疑问,欢迎回复。偶天天要逛下javaeye的。
祝大家圣诞快乐!!!!!!!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值