Subject: [PATCH JGIT] Inefficient use of keySet iterator instead of entrySet iterator

From: Yann Simon <[email-masked]>
Date: Thu, 19 Mar 2009 10:17:56 +0100

>>From FindBugs:
This method accesses the value of a Map entry, using a key that
was retrieved from a keySet iterator. It is more efficient to use
an iterator on the entrySet of the map, to avoid the Map.get(key)
lookup.

Signed-off-by: Yann Simon <[email-masked]>
---
 .../src/org/spearce/jgit/lib/ObjectIdMap.java      |    5 +++--
 .../src/org/spearce/jgit/transport/AmazonS3.java   |    5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectIdMap.java b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectIdMap.java
index d3c7f1d..10feaf9 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectIdMap.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectIdMap.java
@@ -183,8 +183,9 @@ public V put(ObjectId key, V value) {
 	}
 
 	public void putAll(Map<? extends ObjectId, ? extends V> arg0) {
-		for (ObjectId k : arg0.keySet()) {
-			V v=arg0.get(k);
+		for (Map.Entry<? extends ObjectId, ? extends V> entry : arg0.entrySet()) {
+			ObjectId k = entry.getKey();
+			V v=entry.getValue();
 			put(k,v);
 		}
 	}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/AmazonS3.java b/org.spearce.jgit/src/org/spearce/jgit/transport/AmazonS3.java
index 59337f8..3d8bdca 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/AmazonS3.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/AmazonS3.java
@@ -572,9 +572,10 @@ private HttpURLConnection open(final String method, final String bucket,
 	private void authorize(final HttpURLConnection c) throws IOException {
 		final Map<String, List<String>> reqHdr = c.getRequestProperties();
 		final SortedMap<String, String> sigHdr = new TreeMap<String, String>();
-		for (final String hdr : reqHdr.keySet()) {
+		for (final Map.Entry<String, List<String>> entry : reqHdr.entrySet()) {
+			final String hdr = entry.getKey();
 			if (isSignedHeader(hdr))
-				sigHdr.put(hdr.toLowerCase(), toCleanString(reqHdr.get(hdr)));
+				sigHdr.put(hdr.toLowerCase(), toCleanString(entry.getValue()));
 		}
 
 		final StringBuilder s = new StringBuilder();
-- 
1.6.1.2