Performance: Iteration over 'keySet()' may be optimized

This commit is contained in:
TacoTheDank 2020-11-19 15:19:27 -05:00
parent 270e7e873d
commit 6b16170168
21 changed files with 83 additions and 67 deletions

View file

@ -2501,8 +2501,9 @@ public class BinaryMapIndexReader {
}
}
}
for (MapObject e : resMap.keySet()) {
Street s = resMap.get(e);
for (Entry<MapObject, Street> entry : resMap.entrySet()) {
MapObject e = entry.getKey();
Street s = entry.getValue();
if (e instanceof Building && MapUtils.getDistance(e.getLocation(), lat, lon) < 40) {
Building b = (Building) e;
System.out.println(b.getName() + " " + s);

View file

@ -518,8 +518,8 @@ public class TileSourceManager {
}
if (override || !metainfo.exists()) {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(metainfo)));
for (String key : properties.keySet()) {
writer.write("[" + key + "]\n" + properties.get(key) + "\n");
for (Map.Entry<String, String> entry : properties.entrySet()) {
writer.write("[" + entry.getKey() + "]\n" + entry.getValue() + "\n");
}
writer.close();
}

View file

@ -96,8 +96,9 @@ public abstract class MapRenderingTypes {
Map<String, String> common = new HashMap<String, String>();
String ATTACHED_KEY = "seamark:attached";
String type = "";
for (String s : tags.keySet()) {
String value = tags.get(s);
for (Entry<String, String> entry : tags.entrySet()) {
String s = entry.getKey();
String value = entry.getValue();
if (s.equals("seamark:type")) {
type = value;
common.put(ATTACHED_KEY, openSeaType(value));

View file

@ -33,9 +33,10 @@ public class EntityParser {
if (mo.getEnName(false).length() == 0) {
mo.setEnName(tags.get(OSMTagKey.NAME_EN.getValue()));
}
for (String ts : tags.keySet()) {
for (Map.Entry<String, String> entry : tags.entrySet()) {
String ts = entry.getKey();
if (ts.startsWith("name:") && !ts.equals(OSMTagKey.NAME_EN.getValue())) {
mo.setName(ts.substring(("name:").length()), tags.get(ts));
mo.setName(ts.substring(("name:").length()), entry.getValue());
}
}
if (mo.getName().length() == 0) {

View file

@ -63,8 +63,8 @@ public class NetworkUtils {
URL url;
try {
boolean firstPrm =!urlText.contains("?");
for (String key : additionalMapData.keySet()) {
urlText += (firstPrm ? "?" : "&") + key + "=" + URLEncoder.encode(additionalMapData.get(key), "UTF-8");
for (Map.Entry<String, String> entry : additionalMapData.entrySet()) {
urlText += (firstPrm ? "?" : "&") + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8");
firstPrm = false;
}
log.info("Start uploading file to " + urlText + " " +fileToUpload.getName());

View file

@ -152,9 +152,9 @@ public class RenderingRulesStoragePrinter {
for (int i = 0; i < 15; i++) {
out.println("" + indent + ti + "RenderingRule rule" + i + " = null;");
}
for (String s : storage.renderingAttributes.keySet()) {
generateRenderingRule(storage, out, indent + ti, "rule", 0, storage.renderingAttributes.get(s));
out.println("" + indent + ti + "storage.renderingAttributes.put(" + javaString(s) + ", rule0);");
for (Entry<String, RenderingRule> entry : storage.renderingAttributes.entrySet()) {
generateRenderingRule(storage, out, indent + ti, "rule", 0, entry.getValue());
out.println("" + indent + ti + "storage.renderingAttributes.put(" + javaString(entry.getKey()) + ", rule0);");
}
out.println(""+indent +"}");
}
@ -242,9 +242,9 @@ public class RenderingRulesStoragePrinter {
private void printJavaInitConstants(RenderingRulesStorage storage, PrintStream out, String indent, String ti) {
out.println("\n" + indent + "public void initConstants() {");
for (String s : storage.renderingConstants.keySet()) {
out.println("" + indent + ti + "storage.renderingConstants.put(" + javaString(s) + ", "
+ javaString(storage.renderingConstants.get(s)) + ");");
for (Entry<String, String> entry : storage.renderingConstants.entrySet()) {
out.println("" + indent + ti + "storage.renderingConstants.put(" + javaString(entry.getKey()) + ", "
+ javaString(entry.getValue()) + ");");
}
out.println(""+indent +"}");
}

View file

@ -803,10 +803,10 @@ public class GeoPointParserUtil {
if (map.size() > 0)
uriString += "?";
int i = 0;
for (String key : map.keySet()) {
for (Map.Entry<String, String> entry : map.entrySet()) {
if (i > 0)
uriString += "&";
uriString += key + "=" + map.get(key);
uriString += entry.getKey() + "=" + entry.getValue();
i++;
}
return uriString;

View file

@ -305,8 +305,8 @@ public class AndroidNetworkUtils {
try {
boolean firstPrm = !urlText.contains("?");
StringBuilder sb = new StringBuilder(urlText);
for (String key : additionalParams.keySet()) {
sb.append(firstPrm ? "?" : "&").append(key).append("=").append(URLEncoder.encode(additionalParams.get(key), "UTF-8"));
for (Map.Entry<String, String> entry : additionalParams.entrySet()) {
sb.append(firstPrm ? "?" : "&").append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8"));
firstPrm = false;
}
urlText = sb.toString();

View file

@ -190,14 +190,15 @@ class AppVersionUpgradeOnInit {
migrateEnumPreferences();
SharedPreferences globalSharedPreferences = (SharedPreferences) settings.getGlobalPreferences();
Map<String, ?> globalPrefsMap = globalSharedPreferences.getAll();
for (String key : globalPrefsMap.keySet()) {
for (Map.Entry<String, ?> entry : globalPrefsMap.entrySet()) {
String key = entry.getKey();
OsmandPreference<?> pref = settings.getPreference(key);
if (pref instanceof CommonPreference) {
CommonPreference<?> commonPreference = (CommonPreference<?>) pref;
if (!commonPreference.isGlobal()) {
for (ApplicationMode mode : ApplicationMode.allPossibleValues()) {
if (!commonPreference.isSetForMode(mode) && !commonPreference.hasDefaultValueForMode(mode)) {
settings.setPreference(key, globalPrefsMap.get(key), mode);
settings.setPreference(key, entry.getValue(), mode);
}
}
}
@ -205,12 +206,13 @@ class AppVersionUpgradeOnInit {
}
SharedPreferences defaultProfilePreferences = (SharedPreferences) settings.getProfilePreferences(ApplicationMode.DEFAULT);
Map<String, ?> defaultPrefsMap = defaultProfilePreferences.getAll();
for (String key : defaultPrefsMap.keySet()) {
for (Map.Entry<String, ?> entry : defaultPrefsMap.entrySet()) {
String key = entry.getKey();
OsmandPreference<?> pref = settings.getPreference(key);
if (pref instanceof CommonPreference) {
CommonPreference<?> commonPreference = (CommonPreference<?>) pref;
if (commonPreference.isGlobal() && !commonPreference.isSet()) {
settings.setPreference(key, defaultPrefsMap.get(key));
settings.setPreference(key, entry.getValue());
}
}
}

View file

@ -261,10 +261,11 @@ public class FavouritesDbHelper {
private boolean merge(Map<String, FavouritePoint> source, Map<String, FavouritePoint> destination) {
boolean changed = false;
for (String ks : source.keySet()) {
for (Map.Entry<String, FavouritePoint> entry : source.entrySet()) {
String ks = entry.getKey();
if (!destination.containsKey(ks)) {
changed = true;
destination.put(ks, source.get(ks));
destination.put(ks, entry.getValue());
}
}
return changed;
@ -712,9 +713,10 @@ public class FavouritesDbHelper {
public boolean isGroupVisible(String name) {
String nameLowercase = name.toLowerCase();
for (String groupName : flatGroups.keySet()) {
for (Map.Entry<String, FavoriteGroup> entry : flatGroups.entrySet()) {
String groupName = entry.getKey();
if (groupName.toLowerCase().equals(nameLowercase) || FavoriteGroup.getDisplayName(context, groupName).equals(name)) {
return flatGroups.get(groupName).isVisible();
return entry.getValue().isVisible();
}
}
return false;

View file

@ -208,11 +208,12 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
Map<String, GPXFile> data = collectRecordedData();
// save file
for (final String f : data.keySet()) {
for (final Map.Entry<String, GPXFile> entry : data.entrySet()) {
final String f = entry.getKey();
log.debug("Filename: " + f);
File fout = new File(dir, f + IndexConstants.GPX_FILE_EXT);
if (!data.get(f).isEmpty()) {
WptPt pt = data.get(f).findPointToShow();
if (!entry.getValue().isEmpty()) {
WptPt pt = entry.getValue().findPointToShow();
String fileName = f + "_" + new SimpleDateFormat("HH-mm_EEE", Locale.US).format(new Date(pt.time)); //$NON-NLS-1$
Integer trackStorageDirectory = ctx.getSettings().TRACK_STORAGE_DIRECTORY.get();
if (!OsmandSettings.REC_DIRECTORY.equals(trackStorageDirectory)) {
@ -235,13 +236,13 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
}
}
Exception warn = GPXUtilities.writeGpxFile(fout, data.get(f));
Exception warn = GPXUtilities.writeGpxFile(fout, entry.getValue());
if (warn != null) {
warnings.add(warn.getMessage());
return new SaveGpxResult(warnings, new ArrayList<String>());
}
GPXFile gpx = data.get(f);
GPXFile gpx = entry.getValue();
GPXTrackAnalysis analysis = gpx.getAnalysis(fout.lastModified());
GpxDataItem item = new GpxDataItem(fout, analysis);
ctx.getGpxDbHelper().add(item);

View file

@ -25,9 +25,9 @@ public class FileSettingsAPIImpl implements SettingsAPI {
Properties props = new Properties();
FileInputStream fis = new FileInputStream(file);
props.load(fis);
for (Object key : props.keySet()) {
String k = key.toString();
map.put(k, props.get(key));
for (Entry<Object, Object> entry : props.entrySet()) {
String k = entry.getKey().toString();
map.put(k, entry.getValue());
}
}
}

View file

@ -81,7 +81,8 @@ public class SelectWptCategoriesBottomSheetDialogFragment extends MenuBottomShee
Map<String, List<WptPt>> pointsByCategories = gpxFile.getPointsByCategories();
for (String category : pointsByCategories.keySet()) {
for (Map.Entry<String, List<WptPt>> entry : pointsByCategories.entrySet()) {
String category = entry.getKey();
final BottomSheetItemWithCompoundButton[] categoryItem = new BottomSheetItemWithCompoundButton[1];
categoryItem[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder()
.setChecked(!isUpdateMode || (categories != null && categories.contains(category)))
@ -96,7 +97,7 @@ public class SelectWptCategoriesBottomSheetDialogFragment extends MenuBottomShee
}
})
.setCompoundButtonColorId(activeColorResId)
.setDescription(String.valueOf(pointsByCategories.get(category).size()))
.setDescription(String.valueOf(entry.getValue().size()))
.setIcon(getContentIcon(R.drawable.ic_action_folder))
.setTitle(category.equals("") ? getString(R.string.shared_string_waypoints) : category)
.setLayoutId(R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp)

View file

@ -684,9 +684,9 @@ public class OsmEditsFragment extends OsmAndListFragment implements ProgressDial
@Override
public void uploadEnded(Map<OsmPoint, String> loadErrorsMap) {
super.uploadEnded(loadErrorsMap);
for (OsmPoint osmPoint : loadErrorsMap.keySet()) {
if (loadErrorsMap.get(osmPoint) == null) {
osmEdits.remove(osmPoint);
for (Map.Entry<OsmPoint, String> entry : loadErrorsMap.entrySet()) {
if (entry.getValue() == null) {
osmEdits.remove(entry.getKey());
}
}
recreateAdapterData();

View file

@ -59,8 +59,8 @@ public class OsmEditsUploadListenerHelper implements OsmEditsUploadListener {
}
int uploaded = 0;
int pointsNum = loadErrorsMap.keySet().size();
for (OsmPoint point : loadErrorsMap.keySet()) {
if (loadErrorsMap.get(point) == null) {
for (String s : loadErrorsMap.values()) {
if (s == null) {
uploaded++;
}
}
@ -200,13 +200,14 @@ public class OsmEditsUploadListenerHelper implements OsmEditsUploadListener {
boolean[] hasErrors = new boolean[loadErrorsMap.keySet().size()];
ArrayList<OsmPoint> pointsWithErrors = new ArrayList<>();
int i = 0;
for (OsmPoint point : loadErrorsMap.keySet()) {
for (Map.Entry<OsmPoint, String> entry : loadErrorsMap.entrySet()) {
OsmPoint point = entry.getKey();
pointNames[i] = point.getGroup() == OsmPoint.Group.BUG ?
((OsmNotesPoint) point).getText() :
((OpenstreetmapPoint) point).getName();
pointNames[i] = TextUtils.isEmpty(pointNames[i]) ?
"id:" + point.getId() : pointNames[i];
hasErrors[i] = loadErrorsMap.get(point) != null;
hasErrors[i] = entry.getValue() != null;
if (hasErrors[i]) {
pointsWithErrors.add(point);
}

View file

@ -751,14 +751,15 @@ public class PoiFiltersHelper {
}
Map<PoiCategory, LinkedHashSet<String>> types = p.getAcceptedTypes();
SQLiteStatement insertCategories = db.compileStatement("INSERT INTO " + CATEGORIES_NAME + " VALUES (?, ?, ?)");
for (PoiCategory a : types.keySet()) {
if (types.get(a) == null) {
for (Map.Entry<PoiCategory, LinkedHashSet<String>> entry : types.entrySet()) {
PoiCategory a = entry.getKey();
if (entry.getValue() == null) {
insertCategories.bindString(1, p.getFilterId());
insertCategories.bindString(2, a.getKeyName());
insertCategories.bindNull(3);
insertCategories.execute();
} else {
for (String s : types.get(a)) {
for (String s : entry.getValue()) {
insertCategories.bindString(1, p.getFilterId());
insertCategories.bindString(2, a.getKeyName());
insertCategories.bindString(3, s);

View file

@ -535,20 +535,19 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable<PoiUIFilter>
return getName();
}
StringBuilder res = new StringBuilder();
for (PoiCategory p : acceptedTypes.keySet()) {
LinkedHashSet<String> set = acceptedTypes.get(p);
for (Entry<PoiCategory, LinkedHashSet<String>> entry : acceptedTypes.entrySet()) {
LinkedHashSet<String> set = entry.getValue();
if (set == null) {
if (res.length() > 0) {
res.append(", ");
}
res.append(p.getTranslation());
res.append(entry.getKey().getTranslation());
}
if (res.length() > chars) {
return res.toString();
}
}
for (PoiCategory p : acceptedTypes.keySet()) {
LinkedHashSet<String> set = acceptedTypes.get(p);
for (LinkedHashSet<String> set : acceptedTypes.values()) {
if (set != null) {
for (String st : set) {
if (res.length() > 0) {
@ -616,8 +615,8 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable<PoiUIFilter>
public boolean areAllTypesAccepted() {
if (poiTypes.getCategories(false).size() == acceptedTypes.size()) {
for (PoiCategory a : acceptedTypes.keySet()) {
if (acceptedTypes.get(a) != null) {
for (LinkedHashSet<String> strings : acceptedTypes.values()) {
if (strings != null) {
return false;
}
}
@ -711,8 +710,9 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable<PoiUIFilter>
}
private void putAllAcceptedTypes(Map<PoiCategory, LinkedHashSet<String>> types) {
for (PoiCategory category : types.keySet()) {
LinkedHashSet<String> typesSet = types.get(category);
for (Entry<PoiCategory, LinkedHashSet<String>> entry : types.entrySet()) {
PoiCategory category = entry.getKey();
LinkedHashSet<String> typesSet = entry.getValue();
if (acceptedTypes.containsKey(category)) {
if (acceptedTypes.get(category) != null && typesSet != null) {
acceptedTypes.get(category).addAll(typesSet);

View file

@ -311,8 +311,9 @@ public class MapRenderRepositories {
}
boolean containsJapanMapData = false;
boolean useLive = context.getSettings().USE_OSM_LIVE_FOR_ROUTING.get();
for (String mapName : files.keySet()) {
BinaryMapIndexReader fr = files.get(mapName);
for (Map.Entry<String, BinaryMapIndexReader> entry : files.entrySet()) {
String mapName = entry.getKey();
BinaryMapIndexReader fr = entry.getValue();
if (fr != null && (fr.containsMapData(leftX, topY, rightX, bottomY, zoom) ||
fr.containsRouteData(leftX, topY, rightX, bottomY, zoom))) {
if (!nativeFiles.contains(mapName)) {

View file

@ -114,12 +114,13 @@ public class TerrainLayer extends MapTileLayer {
}
private void indexNonCachedResources(Map<String, Long> fileModified, Map<String, SQLiteTileSource> rs) {
for(String filename : fileModified.keySet()) {
for(Map.Entry<String, Long> entry : fileModified.entrySet()) {
String filename = entry.getKey();
try {
log.info("Indexing " + type + " file " + filename);
ContentValues cv = new ContentValues();
cv.put("filename", filename);
cv.put("date_modified", fileModified.get(filename));
cv.put("date_modified", entry.getValue());
SQLiteTileSource ts = rs.get(filename);
QuadRect rt = ts.getRectBoundary(ZOOM_BOUNDARY, 1);
if (rt != null) {

View file

@ -232,20 +232,22 @@ public class ContextMenuLayer extends OsmandMapLayer {
if (!pressedLatLonSmall.isEmpty() || !pressedLatLonFull.isEmpty()) {
textScale = activity.getMyApplication().getSettings().TEXT_SCALE.get();
}
for (LatLon latLon : pressedLatLonSmall.keySet()) {
for (Entry<LatLon, BackgroundType> entry : pressedLatLonSmall.entrySet()) {
LatLon latLon = entry.getKey();
int x = (int) box.getPixXFromLatLon(latLon.getLatitude(), latLon.getLongitude());
int y = (int) box.getPixYFromLatLon(latLon.getLatitude(), latLon.getLongitude());
BackgroundType background = pressedLatLonSmall.get(latLon);
BackgroundType background = entry.getValue();
Bitmap pressedBitmapSmall = background.getTouchBackground(activity, true);
Rect destRect = getIconDestinationRect(
x, y, pressedBitmapSmall.getWidth(), pressedBitmapSmall.getHeight(), textScale);
canvas.drawBitmap(pressedBitmapSmall, null, destRect, paint);
}
for (LatLon latLon : pressedLatLonFull.keySet()) {
for (Entry<LatLon, BackgroundType> entry : pressedLatLonFull.entrySet()) {
LatLon latLon = entry.getKey();
int x = (int) box.getPixXFromLatLon(latLon.getLatitude(), latLon.getLongitude());
int y = (int) box.getPixYFromLatLon(latLon.getLatitude(), latLon.getLongitude());
BackgroundType background = pressedLatLonFull.get(latLon);
BackgroundType background = entry.getValue();
Bitmap pressedBitmap = background.getTouchBackground(activity, false);
int offsetY = background.getOffsetY(activity, textScale);
Rect destRect = getIconDestinationRect(

View file

@ -61,7 +61,8 @@ public class MapTextLayer extends OsmandMapLayer {
@Override
public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) {
TIntHashSet set = new TIntHashSet();
for (OsmandMapLayer l : textObjects.keySet()) {
for (Map.Entry<OsmandMapLayer, Collection<?>> entry : textObjects.entrySet()) {
OsmandMapLayer l = entry.getKey();
MapTextProvider provider = (MapTextProvider) l;
if (!view.isLayerVisible(l) || !provider.isTextVisible()) {
continue;
@ -69,7 +70,7 @@ public class MapTextLayer extends OsmandMapLayer {
updateTextSize();
paintTextIcon.setFakeBoldText(provider.isFakeBoldText());
for (Object o : textObjects.get(l)) {
for (Object o : entry.getValue()) {
LatLon loc = provider.getTextLocation(o);
String name = provider.getText(o);
if (loc == null || TextUtils.isEmpty(name)) {