Java Code Examples for java.util.Set#hashCode()

The following examples show how to use java.util.Set#hashCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DistinctEntrySetElements.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 2
Source File: DistinctEntrySetElements.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 3
Source File: DistinctEntrySetElements.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 4
Source File: DistinctEntrySetElements.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 5
Source File: DistinctEntrySetElements.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 6
Source File: Smooth_Composite.java    From symja_android_library with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Constructor from several AQ-pairs.
 * @param aqPairs
 */
public Smooth_Composite(Set<AQPair> aqPairs) {
	this.aqPairs = new AQPair[aqPairs.size()];
	HashSet<Integer> smallFactorsWithOddExp = new HashSet<Integer>();
	int aqPairCount = 0;
	for (AQPair aqPair : aqPairs) {
		this.aqPairs[aqPairCount++] = aqPair;
		for (int i=0; i<aqPair.smallFactors.length; i++) {
			if ((aqPair.smallFactorExponents[i]&1)==1) {
				// add via xor
				Integer oddExpSmallFactor = aqPair.smallFactors[i];
				if (!smallFactorsWithOddExp.remove(oddExpSmallFactor)) smallFactorsWithOddExp.add(oddExpSmallFactor);
			}
		}
	}
	this.oddExpElements = smallFactorsWithOddExp.toArray(new Integer[smallFactorsWithOddExp.size()]);
	
	this.hashCode = aqPairs.hashCode();
}
 
Example 7
Source File: SerializationContext.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public int hashCode() {
	ParserRule rule = getParserRule();
	Action action = getAssignedAction();
	Set<Parameter> parameterValues = getEnabledBooleanParameters();
	EClass type = getType();
	int result = 1;
	if (rule != null)
		result = 31 * result + rule.hashCode();
	if (action != null)
		result = 31 * result + action.hashCode();
	if (type != null)
		result = 31 * result + type.hashCode();
	if (parameterValues != null)
		result = 31 * result + parameterValues.hashCode();
	return result;
}
 
Example 8
Source File: DistinctEntrySetElements.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 9
Source File: DistinctEntrySetElements.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 10
Source File: DistinctEntrySetElements.java    From native-obfuscator with GNU General Public License v3.0 6 votes vote down vote up
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.");
    }
}
 
Example 11
Source File: DistinctEntrySetElements.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 12
Source File: DistinctEntrySetElements.java    From native-obfuscator with GNU General Public License v3.0 6 votes vote down vote up
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.");
    }
}
 
Example 13
Source File: DistinctEntrySetElements.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 14
Source File: Sets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public int hashCode() {
  // Warning: this is broken if size() == 0, so it is critical that we
  // substitute an empty ImmutableSet to the user in place of this

  // It's a weird formula, but tests prove it works.
  int adjust = size() - 1;
  for (int i = 0; i < axes.size(); i++) {
    adjust *= 31;
    adjust = ~~adjust;
    // in GWT, we have to deal with integer overflow carefully
  }
  int hash = 1;
  for (Set<E> axis : axes) {
    hash = 31 * hash + (size() / axis.size() * axis.hashCode());
    hash = ~~hash;
  }
  hash += adjust;
  return ~~hash;
}
 
Example 15
Source File: DistinctEntrySetElements.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 16
Source File: DistinctEntrySetElements.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 17
Source File: Sets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public int hashCode() {
  // Warning: this is broken if size() == 0, so it is critical that we
  // substitute an empty ImmutableSet to the user in place of this

  // It's a weird formula, but tests prove it works.
  int adjust = size() - 1;
  for (int i = 0; i < axes.size(); i++) {
    adjust *= 31;
    adjust = ~~adjust;
    // in GWT, we have to deal with integer overflow carefully
  }
  int hash = 1;
  for (Set<E> axis : axes) {
    hash = 31 * hash + (size() / axis.size() * axis.hashCode());

    hash = ~~hash;
  }
  hash += adjust;
  return ~~hash;
}
 
Example 18
Source File: DistinctEntrySetElements.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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.");
    }
}
 
Example 19
Source File: SubjectNullTests.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void testSecureSetHashCode() {
    System.out.println("------ SecureSet.hashCode() -----");

    Subject subj = makeSubj(false, false, false);

    // Make sure two other Set types that we know are equal per
    // SecureSet.equals() and verify their hashCodes are also the same
    Set<Principal> equalHashSet = new HashSet<>(Arrays.asList(princVals));

    if (subj.getPrincipals().hashCode() != equalHashSet.hashCode()) {
        throw new RuntimeException(
                "SecureSet and HashSet hashCodes() differ");
    }
    System.out.println("SecureSet.hashCode() tests passed");
}
 
Example 20
Source File: MemCacheKeys.java    From seldon-server with Apache License 2.0 4 votes vote down vote up
public static String getExplicitItemsIncluderKey(String client,Set<Long> items)
{
	return ""+keys.ExplicitItemsIncluder+":"+client+":"+items.hashCode();
}