下面列出了java.util.HashSet#equals ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean equals(Object obj) {
if (obj instanceof MapWritable) {
MapWritable map = (MapWritable) obj;
if (fSize != map.fSize) return false;
HashSet<KeyValueEntry> set1 = new HashSet<KeyValueEntry>();
KeyValueEntry e1 = fFirst;
while (e1 != null) {
set1.add(e1);
e1 = e1.fNextEntry;
}
HashSet<KeyValueEntry> set2 = new HashSet<KeyValueEntry>();
KeyValueEntry e2 = map.fFirst;
while (e2 != null) {
set2.add(e2);
e2 = e2.fNextEntry;
}
return set1.equals(set2);
}
return false;
}
/**
* Is it possible to do a distinct scan on this ResultSet tree.
* (See SelectNode for the criteria.)
*
* @param distinctColumns the set of distinct columns
* @return Whether or not it is possible to do a distinct scan on this ResultSet tree.
*/
@Override
boolean isPossibleDistinctScan(Set distinctColumns)
{
if (restriction != null ||
(restrictionList != null && restrictionList.size() != 0))
{
return false;
}
HashSet columns = new HashSet();
for (int i = 0; i < resultColumns.size(); i++) {
ResultColumn rc = (ResultColumn) resultColumns.elementAt(i);
BaseColumnNode bc = rc.getBaseColumnNode();
if (bc == null) return false;
columns.add(bc);
}
return columns.equals(distinctColumns) && childResult.isPossibleDistinctScan(distinctColumns);
}
public static void main(String[] args) throws Exception {
final ConcurrentHashMap<String, String> concurrentHashMap =
new ConcurrentHashMap<>();
concurrentHashMap.put("One", "Un");
concurrentHashMap.put("Two", "Deux");
concurrentHashMap.put("Three", "Trois");
Set<Map.Entry<String, String>> entrySet = concurrentHashMap.entrySet();
HashSet<Map.Entry<String, String>> hashSet = new HashSet<>(entrySet);
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
public static void main(String[] args) throws Exception {
final IdentityHashMap<String, String> identityHashMap =
new IdentityHashMap<>();
identityHashMap.put("One", "Un");
identityHashMap.put("Two", "Deux");
identityHashMap.put("Three", "Trois");
Set<Map.Entry<String, String>> entrySet = identityHashMap.entrySet();
HashSet<Map.Entry<String, String>> hashSet = new HashSet<>(entrySet);
// NB: These comparisons are valid in this case because none of the
// keys put into 'identityHashMap' above are equal to any other.
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
public static void main(String[] args) throws Exception {
final EnumMap<TestEnum, String> enumMap = new EnumMap<>(TestEnum.class);
for (TestEnum e : TestEnum.values()) {
enumMap.put(e, e.name());
}
Set<Map.Entry<TestEnum, String>> entrySet = enumMap.entrySet();
HashSet<Map.Entry<TestEnum, String>> hashSet = new HashSet<>(entrySet);
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
public boolean equals(Object obj) {
if (obj instanceof MapWritable) {
MapWritable map = (MapWritable) obj;
if (fSize != map.fSize) return false;
HashSet<KeyValueEntry> set1 = new HashSet<KeyValueEntry>();
KeyValueEntry e1 = fFirst;
while (e1 != null) {
set1.add(e1);
e1 = e1.fNextEntry;
}
HashSet<KeyValueEntry> set2 = new HashSet<KeyValueEntry>();
KeyValueEntry e2 = map.fFirst;
while (e2 != null) {
set2.add(e2);
e2 = e2.fNextEntry;
}
return set1.equals(set2);
}
return false;
}
public static void main(String[] args) throws Exception {
final EnumMap<TestEnum, String> enumMap = new EnumMap<>(TestEnum.class);
for (TestEnum e : TestEnum.values()) {
enumMap.put(e, e.name());
}
Set<Map.Entry<TestEnum, String>> entrySet = enumMap.entrySet();
HashSet<Map.Entry<TestEnum, String>> hashSet = new HashSet<>(entrySet);
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
public static void main(String[] args) throws Exception {
final IdentityHashMap<String, String> identityHashMap =
new IdentityHashMap<>();
identityHashMap.put("One", "Un");
identityHashMap.put("Two", "Deux");
identityHashMap.put("Three", "Trois");
Set<Map.Entry<String, String>> entrySet = identityHashMap.entrySet();
HashSet<Map.Entry<String, String>> hashSet = new HashSet<>(entrySet);
// NB: These comparisons are valid in this case because none of the
// keys put into 'identityHashMap' above are equal to any other.
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
public static void main(String[] args) throws Exception {
final ConcurrentHashMap<String, String> concurrentHashMap =
new ConcurrentHashMap<>();
concurrentHashMap.put("One", "Un");
concurrentHashMap.put("Two", "Deux");
concurrentHashMap.put("Three", "Trois");
Set<Map.Entry<String, String>> entrySet = concurrentHashMap.entrySet();
HashSet<Map.Entry<String, String>> hashSet = new HashSet<>(entrySet);
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
public static void main(String[] args) throws Exception {
final EnumMap<TestEnum, String> enumMap = new EnumMap<>(TestEnum.class);
for (TestEnum e : TestEnum.values()) {
enumMap.put(e, e.name());
}
Set<Map.Entry<TestEnum, String>> entrySet = enumMap.entrySet();
HashSet<Map.Entry<TestEnum, String>> hashSet = new HashSet<>(entrySet);
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
public static void main(String[] args) throws Exception {
final IdentityHashMap<String, String> identityHashMap =
new IdentityHashMap<>();
identityHashMap.put("One", "Un");
identityHashMap.put("Two", "Deux");
identityHashMap.put("Three", "Trois");
Set<Map.Entry<String, String>> entrySet = identityHashMap.entrySet();
HashSet<Map.Entry<String, String>> hashSet = new HashSet<>(entrySet);
// NB: These comparisons are valid in this case because none of the
// keys put into 'identityHashMap' above are equal to any other.
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
public static void main(String[] args) throws Exception {
final EnumMap<TestEnum, String> enumMap = new EnumMap<>(TestEnum.class);
for (TestEnum e : TestEnum.values()) {
enumMap.put(e, e.name());
}
Set<Map.Entry<TestEnum, String>> entrySet = enumMap.entrySet();
HashSet<Map.Entry<TestEnum, String>> hashSet = new HashSet<>(entrySet);
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
public static void main(String[] args) throws Exception {
final EnumMap<TestEnum, String> enumMap = new EnumMap<>(TestEnum.class);
for (TestEnum e : TestEnum.values()) {
enumMap.put(e, e.name());
}
Set<Map.Entry<TestEnum, String>> entrySet = enumMap.entrySet();
HashSet<Map.Entry<TestEnum, String>> hashSet = new HashSet<>(entrySet);
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
public static void main(String[] args) throws Exception {
final ConcurrentHashMap<String, String> concurrentHashMap =
new ConcurrentHashMap<>();
concurrentHashMap.put("One", "Un");
concurrentHashMap.put("Two", "Deux");
concurrentHashMap.put("Three", "Trois");
Set<Map.Entry<String, String>> entrySet = concurrentHashMap.entrySet();
HashSet<Map.Entry<String, String>> hashSet = new HashSet<>(entrySet);
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
@SuppressWarnings("unchecked")
private int findSample(Object[] u, Object[] samp) {
for (int i = 0; i < u.length; i++) {
HashSet<Object> set = (HashSet<Object>) u[i];
HashSet<Object> sampSet = new HashSet<Object>();
for (int j = 0; j < samp.length; j++) {
sampSet.add(samp[j]);
}
if (set.equals(sampSet)) {
return i;
}
}
fail("sample not found:{" + samp[0] + "," + samp[1] + "}");
return -1;
}
@SuppressWarnings("unchecked")
private int findSample(Object[] u, Object[] samp) {
for (int i = 0; i < u.length; i++) {
HashSet<Object> set = (HashSet<Object>) u[i];
HashSet<Object> sampSet = new HashSet<Object>();
for (int j = 0; j < samp.length; j++) {
sampSet.add(samp[j]);
}
if (set.equals(sampSet)) {
return i;
}
}
fail("sample not found:{" + samp[0] + "," + samp[1] + "}");
return -1;
}
public static void main(String[] args) {
int failures = 0;
for (int i = 0; i < NUM_SETS; i++) {
HashSet<Integer> hashSet = createHashSet();
HashSet<Integer> result = null;
try {
result = serDeser(hashSet);
} catch (IOException ioe) {
System.err.println(ioe);
failures++;
} catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
failures++;
}
if (!hashSet.equals(result)) {
System.err.println("Unequal HashSets!");
printHashSet(hashSet);
System.err.println();
failures++;
}
}
if (failures != 0) {
throw new RuntimeException("HashSet/Serialzation failed with "+
failures+" failures!");
}
}
@SuppressWarnings("unchecked")
private int findSample(Object[] u, Object[] samp) {
for (int i = 0; i < u.length; i++) {
HashSet<Object> set = (HashSet<Object>) u[i];
HashSet<Object> sampSet = new HashSet<Object>();
for (int j = 0; j < samp.length; j++) {
sampSet.add(samp[j]);
}
if (set.equals(sampSet)) {
return i;
}
}
fail("sample not found:{" + samp[0] + "," + samp[1] + "}");
return -1;
}
@SuppressWarnings("unchecked")
private int findSample(Object[] u, Object[] samp) {
for (int i = 0; i < u.length; i++) {
HashSet<Object> set = (HashSet<Object>) u[i];
HashSet<Object> sampSet = new HashSet<Object>();
for (int j = 0; j < samp.length; j++) {
sampSet.add(samp[j]);
}
if (set.equals(sampSet)) {
return i;
}
}
fail("sample not found:{" + samp[0] + "," + samp[1] + "}");
return -1;
}
@NoWarning("EC_UNRELATED_TYPES")
public boolean test(HashSet<Integer> s1, TreeSet<Integer> s2) {
return s1.equals(s2);
}