Java求集合的交集、并集、差集(必要时重写equals和hashCode方法)

Java求集合的交集、差集、并集，必要时需要重写equals或/且hashCode方法，本文给出应用实例，并自行编写函数将求得的交、差、并集存储在新的集合，不破坏原集合的内容。

1. 应用实例

1.1 Set接口

//Collection.class
boolean    retainAll(Collection<? extends E> c)  //求交集 Adds all of the elements in the specified collection to this collection (optional operation).
boolean    addAll(Collection<? extends E> c)     //求并集 Adds all of the elements in the specified collection to this collection (optional operation).
boolean    removeAll(Collection<?> c)            //求差集 Removes all of this collection's elements that are also contained in the specified collection (optional operation).

1.2 实例

p43         [M2, M1]
p33         [M1]
p43 − p33   [M2, M1]    //正确结果应为[M1]
p43 ∪ p33  [M1, M2, M1] //正确结果应为[M1, M2]
p43 ∩ p33   [M1]

1.3 equals和hashCode

The value returned by hashCode() is the object’s hash code, which is the object’s memory address in hexadecimal.

By definition, if two objects are equal, their hash code must also be equal. If you override the equals() method, you change the way two objects are equated and Object’s implementation of hashCode() is no longer valid. Therefore, if you override the equals() method, you must also override the hashCode() method as well.

//Object.class
public boolean equals(Object obj) {
return (this == obj);
}
public native int hashCode();  //存储对象的内存地址(16进制表示)

public boolean equals (Object other) {
Message m = (Message)other;
return this.id.equals(m.getId());  //String id; like M1, M2, M3, ...
}

public int hashCode () {
String str = this.id.replaceAll("\\D+",""); //use regex and delete non-digits.
return Integer.parseInt(str);
}

2. 相关源代码

//return union of two collections
public <T> Collection<T> union(Collection<T> collection1, Collection<T> collection2) {
Set<T> set = new HashSet<T>(collection1);
return set;
}

//return intersection of two collections
public <T> Collection<T> intersection(Collection<T> collection1, Collection<T> collection2) {
Set<T> set = new HashSet<T>(collection1);
set.retainAll(collection2);
return set;
}

//return set difference of two collections
public <T> Collection<T> setDifference(Collection<T> collection1, Collection<T> collection2) {
Set<T> set = new HashSet<T>(collection1);
set.removeAll(collection2);
return set;
}