本文整理汇总了Java中java.util.Set.hashCode方法的典型用法代码示例。如果您正苦于以下问题:Java Set.hashCode方法的具体用法?Java Set.hashCode怎么用?Java Set.hashCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Set
的用法示例。
在下文中一共展示了Set.hashCode方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.util.Set; //导入方法依赖的package包/类
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.");
}
}
示例2: hashCode
import java.util.Set; //导入方法依赖的package包/类
@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;
}
示例3: _calculateStableHashCode
import java.util.Set; //导入方法依赖的package包/类
/**
* Because Enums don't have stable hash codes, we can't use their hash code directly. Instead
* we want to use the has code of the enum's name, which should be stable. Here we essentially
* duplicate the hash code calculation of Map, using the stable hash code instead
* @return stable hash code
*/
private static int _calculateStableHashCode(final Map<TrinidadAgent.Application, Set<AgentMatcher>> selectorAgents,
final Set<AgentMatcher> capTouchMatchers)
{
int hashCode = 0;
// Map hash code is defined as the additive hash code of the entries
for (Map.Entry<TrinidadAgent.Application, Set<AgentMatcher>> entry : selectorAgents.entrySet())
{
// use the enum's name to have a stable hash code
int stableKeyHashCode = entry.getKey().name().hashCode();
// entry hash code is defined as the XOR of the key and value.
int entryHashCode = stableKeyHashCode ^ entry.getValue().hashCode();
hashCode += entryHashCode;
}
hashCode += capTouchMatchers.hashCode();
return hashCode;
}
示例4: main
import java.util.Set; //导入方法依赖的package包/类
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.");
}
}
示例5: main
import java.util.Set; //导入方法依赖的package包/类
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.");
}
}
示例6: hashCode
import java.util.Set; //导入方法依赖的package包/类
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
Set<TypeLabel> labelSet = this.labelSet;
result = prime * result + ((labelSet == null) ? 0 : labelSet.hashCode());
result = prime * result + (this.negated ? 1231 : 1237);
result = prime * result + this.var.hashCode();
return result;
}
示例7: testSecureSetHashCode
import java.util.Set; //导入方法依赖的package包/类
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");
}