diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldBuilder.java b/java/core/src/main/java/com/google/protobuf/MapFieldBuilder.java index 84ac1b084153..2b0aba94e043 100644 --- a/java/core/src/main/java/com/google/protobuf/MapFieldBuilder.java +++ b/java/core/src/main/java/com/google/protobuf/MapFieldBuilder.java @@ -34,7 +34,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.function.BiConsumer; /** * Internal representation of map fields in generated builders. @@ -79,12 +78,12 @@ public MapFieldBuilder(Converter converter) { } @SuppressWarnings("unchecked") - private void forEachListEntry(BiConsumer f) { - messageList.forEach( - entry -> { - MapEntry typedEntry = (MapEntry) entry; - f.accept(typedEntry.getKey(), typedEntry.getValue()); - }); + private List> getMapEntryList() { + ArrayList> list = new ArrayList<>(messageList.size()); + for (Message entry : messageList) { + list.add((MapEntry) entry); + } + return list; } public Map ensureBuilderMap() { @@ -93,12 +92,16 @@ public Map ensureBuilderMap() { } if (messageMap != null) { builderMap = new LinkedHashMap<>(messageMap.size()); - messageMap.forEach((key, value) -> builderMap.put(key, value)); + for (Map.Entry entry : messageMap.entrySet()) { + builderMap.put(entry.getKey(), entry.getValue()); + } messageMap = null; return builderMap; } builderMap = new LinkedHashMap<>(messageList.size()); - forEachListEntry((key, value) -> builderMap.put(key, value)); + for (MapEntry entry : getMapEntryList()) { + builderMap.put(entry.getKey(), entry.getValue()); + } messageList = null; return builderMap; } @@ -109,21 +112,24 @@ public List ensureMessageList() { } if (builderMap != null) { messageList = new ArrayList<>(builderMap.size()); - builderMap.forEach( - (key, value) -> - messageList.add( - converter.defaultEntry().toBuilder() - .setKey(key) - .setValue(converter.build(value)) - .build())); + for (Map.Entry entry : builderMap.entrySet()) { + messageList.add( + converter.defaultEntry().toBuilder() + .setKey(entry.getKey()) + .setValue(converter.build(entry.getValue())) + .build()); + } builderMap = null; return messageList; } messageList = new ArrayList<>(messageMap.size()); - messageMap.forEach( - (key, value) -> - messageList.add( - converter.defaultEntry().toBuilder().setKey(key).setValue(value).build())); + for (Map.Entry entry : messageMap.entrySet()) { + messageList.add( + converter.defaultEntry().toBuilder() + .setKey(entry.getKey()) + .setValue(entry.getValue()) + .build()); + } messageMap = null; return messageList; } @@ -145,11 +151,15 @@ private Map populateMutableMap() { } if (builderMap != null) { Map toReturn = new LinkedHashMap<>(builderMap.size()); - builderMap.forEach((key, value) -> toReturn.put(key, converter.build(value))); + for (Map.Entry entry : builderMap.entrySet()) { + toReturn.put(entry.getKey(), converter.build(entry.getValue())); + } return toReturn; } Map toReturn = new LinkedHashMap<>(messageList.size()); - forEachListEntry((key, value) -> toReturn.put(key, value)); + for (MapEntry entry : getMapEntryList()) { + toReturn.put(entry.getKey(), entry.getValue()); + } return toReturn; } @@ -194,7 +204,9 @@ public MapFieldBuilder copy() { public MapField build(MapEntry defaultEntry) { MapField mapField = MapField.newMapField(defaultEntry); Map map = mapField.getMutableMap(); - ensureBuilderMap().forEach((key, value) -> map.put(key, converter.build(value))); + for (Map.Entry entry : ensureBuilderMap().entrySet()) { + map.put(entry.getKey(), converter.build(entry.getValue())); + } mapField.makeImmutable(); return mapField; }