/* * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import android.os.Bundle; import androidx.collection.ArrayMap; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; class Test { void inefficient_loop_bad(HashMap testMap) { for (String key : testMap.keySet()) { testMap.get(key); } } void inefficient_loop_itr_bad(HashMap testMap) { Iterator itr2 = testMap.keySet().iterator(); while (itr2.hasNext()) { String key = (String) itr2.next(); testMap.get(key); } } void inefficient_loop_itr_heur_bad_FN(HashMap testMap) { Iterator itr2 = testMap.keySet().iterator(); int i = 0; int j = 1; int k = 2; int l = 3; while (itr2.hasNext()) { String key = (String) itr2.next(); testMap.get(key); } } void inefficient_loop_itr_heur_bad(HashMap testMap) { Iterator itr2 = testMap.keySet().iterator(); int i = 0; while (itr2.hasNext()) { String key = (String) itr2.next(); testMap.get(key); } } void inefficient_loop_itr_heur_btw_bad(HashMap testMap) { Set keySet = testMap.keySet(); int i = 0; int j = 1; int l = 3; Iterator itr2 = keySet.iterator(); while (itr2.hasNext()) { String key = (String) itr2.next(); testMap.get(key); } } void efficient_loop_itr_ok(HashMap testMap) { Iterator> itr1 = testMap.entrySet().iterator(); while (itr1.hasNext()) { Map.Entry entry = itr1.next(); entry.getKey(); entry.getValue(); } } void efficient_loop_ok(HashMap testMap) { for (Map.Entry entry : testMap.entrySet()) { entry.getKey(); entry.getValue(); } } void negative_loop_ok(HashMap testMap1, HashMap testMap2) { for (String key : testMap1.keySet()) { testMap2.get(key); } } // Bundle doesn't implement Map hence have any entrySet public void from_bundle_ok(Bundle extras) { for (String key : extras.keySet()) { Object t = extras.get(key); } } // ArrayMap extends SimpleMap. void inefficient_arraymap_loop_bad(ArrayMap arrayMap) { for (String key : arrayMap.keySet()) { arrayMap.get(key); } } }