下面列出了java.util.IdentityHashMap#keySet ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Get a list of expressions that mark boundaries into a constant space.
* @param e
* @return
*/
public static Set<LogicalExpression> getConstantExpressionSet(LogicalExpression e){
IdentityHashMap<LogicalExpression, Object> map = new IdentityHashMap<>();
ConstantExpressionIdentifier visitor = new ConstantExpressionIdentifier();
if(e.accept(visitor, map) && map.isEmpty()){
// if we receive a constant value here but the map is empty, this means the entire tree is a constant.
// note, we can't use a singleton collection here because we need an identity set.
map.put(e, true);
return map.keySet();
}else if(map.isEmpty()){
// so we don't continue to carry around a map, we let it go here and simply return an empty set.
return Collections.emptySet();
}else{
return map.keySet();
}
}
/**
* Get a list of expressions that mark boundaries into a constant space.
* @param e
* @return
*/
public static Set<LogicalExpression> getConstantExpressionSet(LogicalExpression e){
IdentityHashMap<LogicalExpression, Object> map = new IdentityHashMap<>();
ConstantExpressionIdentifier visitor = new ConstantExpressionIdentifier();
if(e.accept(visitor, map) && map.isEmpty()){
// if we receive a constant value here but the map is empty, this means the entire tree is a constant.
// note, we can't use a singleton collection here because we need an identity set.
map.put(e, true);
return map.keySet();
}else if(map.isEmpty()){
// so we don't continue to carry around a map, we let it go here and simply return an empty set.
return Collections.emptySet();
}else{
return map.keySet();
}
}
@Override
@SuppressWarnings("unchecked")
Iterator<Annotation> iteratorRecursive(SpanNode node) {
IdentityHashMap<SpanNode, SpanNode> nodes = new IdentityHashMap<SpanNode, SpanNode>();
nodes.put(node, node);
{
Iterator<SpanNode> childrenIt = node.childIteratorRecursive();
while (childrenIt.hasNext()) {
SpanNode child = childrenIt.next();
nodes.put(child, child);
}
}
List<Collection<Annotation>> annotationLists = new ArrayList<Collection<Annotation>>(nodes.size());
for (SpanNode includedNode : nodes.keySet()) {
Collection<Annotation> includedAnnotations = spanNode2Annotation.get(includedNode);
if (includedAnnotations != null) {
annotationLists.add(includedAnnotations);
}
}
return new AnnotationCollectionIterator(annotationLists);
}
/**
* Parse an apk file and extract all classes, methods, fields and optionally instructions
*/
public void loadAll(Scope scope) {
IdentityHashMap<MethodInfo, MethodImplementation> collector = new IdentityHashMap<MethodInfo, MethodImplementation>();
for (DexFile dexFile: dexFiles) {
for (final ClassDef classDef : dexFile.getClasses()) {
ClassInfo ci = Dalvik.findOrCreateClass(scope, classDef.getType());
ClassDetail detail = translateClassDef(ci, classDef, collector);
setDetail(ci, detail);
}
}
if (translateInstructions) {
for (MethodInfo mi: collector.keySet()) {
final MethodImplementation impl = collector.get(mi);
// Decode instructions
if (impl != null) {
new MethodImplementationTranslator(scope).translate(mi, impl);
}
}
}
}
/**
* java.util.IdentityHashMap#keySet()
*/
public void test_keySet_retainAll() {
IdentityHashMap map = new IdentityHashMap();
for (int i = 0; i < 1000; i++) {
map.put(new Integer(i), new Integer(i));
}
Set set = map.keySet();
// retain all the elements
boolean result = set.retainAll(set);
assertTrue("retain all should return false", !result);
assertEquals("did not retain all", 1000, set.size());
// send empty set to retainAll
result = set.retainAll(new TreeSet());
assertTrue("retain all should return true", result);
assertEquals("did not remove all elements in the map", 0, map.size());
assertTrue("did not remove all elements in the keyset", set.isEmpty());
Iterator it = set.iterator();
assertTrue("keySet iterator still has elements", !it.hasNext());
}
@GuardedBy("watchLock")
private void notifyWatchers(String service, @Nullable ServingStatus status) {
HealthCheckResponse response = getResponseForWatch(status);
IdentityHashMap<StreamObserver<HealthCheckResponse>, Boolean> serviceWatchers =
watchers.get(service);
if (serviceWatchers != null) {
for (StreamObserver<HealthCheckResponse> responseObserver : serviceWatchers.keySet()) {
responseObserver.onNext(response);
}
}
}
/**
* Returns a modifiable copy of the list of all i-th atom from all tuples in
* some arbitrary order (0 is first atom, 1 is second atom...)
*
* @throws ErrorAPI if this tupleset contains at least one tuple whose length is
* less than or equal to i
*/
public List<SimAtom> getAllAtoms(int column) throws ErrorAPI {
if (empty())
return new ArrayList<SimAtom>(0);
if (column < 0 || column >= arity())
throw new ErrorAPI("This tupleset does not have an \"" + column + "th\" column.");
IdentityHashMap<SimAtom,Boolean> ans = new IdentityHashMap<SimAtom,Boolean>();
for (SimTuple x : this)
ans.put(x.get(column), Boolean.TRUE);
return new ArrayList<SimAtom>(ans.keySet());
}
Collection<ArrayList<Wire>> getMergeSets() {
IdentityHashMap<ArrayList<Wire>, Boolean> lists;
lists = new IdentityHashMap<ArrayList<Wire>, Boolean>();
for (ArrayList<Wire> list : map.values()) {
lists.put(list, Boolean.TRUE);
}
return lists.keySet();
}
/**
* java.util.IdentityHashMap#clone()
*/
public void test_clone() {
// Test for method java.lang.Object java.util.IdentityHashMap.clone()
IdentityHashMap hm2 = (IdentityHashMap) hm.clone();
assertTrue("Clone answered equivalent IdentityHashMap", hm2 != hm);
for (int counter = 0; counter < hmSize; counter++)
assertTrue("Clone answered unequal IdentityHashMap", hm
.get(objArray2[counter]) == hm2.get(objArray2[counter]));
IdentityHashMap map = new IdentityHashMap();
map.put("key", "value");
// get the keySet() and values() on the original Map
Set keys = map.keySet();
Collection values = map.values();
assertEquals("values() does not work",
"value", values.iterator().next());
assertEquals("keySet() does not work",
"key", keys.iterator().next());
AbstractMap map2 = (AbstractMap) map.clone();
map2.put("key", "value2");
Collection values2 = map2.values();
assertTrue("values() is identical", values2 != values);
// values() and keySet() on the cloned() map should be different
assertEquals("values() was not cloned",
"value2", values2.iterator().next());
map2.clear();
map2.put("key2", "value3");
Set key2 = map2.keySet();
assertTrue("keySet() is identical", key2 != keys);
assertEquals("keySet() was not cloned",
"key2", key2.iterator().next());
}
/**
* java.util.IdentityHashMap#entrySet()
* java.util.IdentityHashMap#keySet()
* java.util.IdentityHashMap#values()
*/
public void test_sets() {
// tests with null keys and values
IdentityHashMap map = new IdentityHashMap();
// null key and null value
map.put("key", "value");
map.put(null, null);
map.put("a key", null);
map.put("another key", null);
Set keyset = map.keySet();
Collection valueset = map.values();
Set entries = map.entrySet();
Iterator it = entries.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
assertTrue("EntrySetIterator can not find entry ", entries
.contains(entry));
assertTrue("entry key not found in map", map.containsKey(entry
.getKey()));
assertTrue("entry value not found in map", map.containsValue(entry
.getValue()));
assertTrue("entry key not found in the keyset", keyset
.contains(entry.getKey()));
assertTrue("entry value not found in the valueset", valueset
.contains(entry.getValue()));
}
}
/**
* java.util.IdentityHashMap#keySet()
* java.util.IdentityHashMap#clear()
*/
public void test_keySet_clear() {
IdentityHashMap map = new IdentityHashMap();
for (int i = 0; i < 1000; i++) {
map.put(new Integer(i), new Integer(i));
}
Set set = map.keySet();
set.clear();
assertEquals("did not remove all elements in the map", 0, map.size());
assertTrue("did not remove all elements in the keyset", set.isEmpty());
Iterator it = set.iterator();
assertTrue("keySet iterator still has elements", !it.hasNext());
}
/**
* java.util.IdentityHashMap#keySet()
* java.util.IdentityHashMap#remove(java.lang.Object)
*/
public void test_keySet_removeAll() {
IdentityHashMap map = new IdentityHashMap();
for (int i = 0; i < 1000; i++) {
map.put(new Integer(i), new Integer(i));
}
Set set = map.keySet();
set.removeAll(set);
assertEquals("did not remove all elements in the map", 0, map.size());
assertTrue("did not remove all elements in the keyset", set.isEmpty());
Iterator it = set.iterator();
assertTrue("keySet iterator still has elements", !it.hasNext());
}
/**
* java.util.IdentityHashMap#keySet()
* java.util.IdentityHashMap#remove(java.lang.Object)
*/
public void test_keyset_remove() {
IdentityHashMap map = new IdentityHashMap();
Integer key = new Integer(21);
map.put(new Integer(1), null);
map.put(new Integer(11), null);
map.put(key, null);
map.put(new Integer(31), null);
map.put(new Integer(41), null);
map.put(new Integer(51), null);
map.put(new Integer(61), null);
map.put(new Integer(71), null);
map.put(new Integer(81), null);
map.put(new Integer(91), null);
Set set = map.keySet();
Set newset = new HashSet();
Iterator it = set.iterator();
while (it.hasNext()) {
Object element = it.next();
if (element == key) {
it.remove();
} else
newset.add(element);
}
int size = newset.size();
assertTrue("keyset and newset don't have same size",
newset.size() == size);
assertTrue("element is in newset ", !newset.contains(key));
assertTrue("element not removed from keyset", !set.contains(key));
assertTrue("element not removed from map", !map.containsKey(key));
assertTrue("newset and keyset do not have same elements 1", newset
.equals(set));
assertTrue("newset and keyset do not have same elements 2", set
.equals(newset));
}
public void test_spliterator_keySet() {
IdentityHashMap<String, String> hashMap = new IdentityHashMap<>();
hashMap.put("a", "1");
hashMap.put("b", "2");
hashMap.put("c", "3");
hashMap.put("d", "4");
hashMap.put("e", "5");
hashMap.put("f", "6");
hashMap.put("g", "7");
hashMap.put("h", "8");
hashMap.put("i", "9");
hashMap.put("j", "10");
hashMap.put("k", "11");
hashMap.put("l", "12");
hashMap.put("m", "13");
hashMap.put("n", "14");
hashMap.put("o", "15");
hashMap.put("p", "16");
Set<String> keys = hashMap.keySet();
ArrayList<String> expectedKeys = new ArrayList<>(keys);
SpliteratorTester.runBasicIterationTests(keys.spliterator(), expectedKeys);
SpliteratorTester.runBasicSplitTests(keys, expectedKeys);
SpliteratorTester.testSpliteratorNPE(keys.spliterator());
SpliteratorTester.assertSupportsTrySplit(keys);
}
@GuardedBy("watchLock")
private void notifyWatchers(String service, @Nullable ServingStatus status) {
HealthCheckResponse response = getResponseForWatch(status);
IdentityHashMap<StreamObserver<HealthCheckResponse>, Boolean> serviceWatchers =
watchers.get(service);
if (serviceWatchers != null) {
for (StreamObserver<HealthCheckResponse> responseObserver : serviceWatchers.keySet()) {
responseObserver.onNext(response);
}
}
}