You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.5 KiB
61 lines
1.5 KiB
6 years ago
|
/*
|
||
|
* Copyright (c) 2019-present, Facebook, Inc.
|
||
|
*
|
||
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
|
*/
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.Map;
|
||
|
|
||
|
class Test {
|
||
|
|
||
|
void inefficient_loop_bad(HashMap<String, Integer> testMap) {
|
||
|
for (String key : testMap.keySet()) {
|
||
|
testMap.get(key);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void inefficient_loop_itr_bad(HashMap<String, Integer> testMap) {
|
||
|
|
||
|
Iterator itr2 = testMap.keySet().iterator();
|
||
|
while (itr2.hasNext()) {
|
||
|
String key = (String) itr2.next();
|
||
|
testMap.get(key);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void inefficient_loop_itr_bad_FN(HashMap<String, Integer> testMap) {
|
||
|
|
||
|
Iterator itr2 = testMap.keySet().iterator();
|
||
|
int i = 0;
|
||
|
while (itr2.hasNext()) {
|
||
|
String key = (String) itr2.next();
|
||
|
testMap.get(key);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void efficient_loop_itr_ok(HashMap<String, Integer> testMap) {
|
||
|
|
||
|
Iterator<Map.Entry<String, Integer>> itr1 = testMap.entrySet().iterator();
|
||
|
while (itr1.hasNext()) {
|
||
|
Map.Entry<String, Integer> entry = itr1.next();
|
||
|
entry.getKey();
|
||
|
entry.getValue();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void efficient_loop_ok(HashMap<String, Integer> testMap) {
|
||
|
for (Map.Entry<String, Integer> entry : testMap.entrySet()) {
|
||
|
entry.getKey();
|
||
|
entry.getValue();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void negative_loop_ok(HashMap<String, Integer> testMap1, HashMap<String, Integer> testMap2) {
|
||
|
for (String key : testMap1.keySet()) {
|
||
|
testMap2.get(key);
|
||
|
}
|
||
|
}
|
||
|
}
|