Skip to content

Commit 0d9e3ad

Browse files
igor-suhorukovjavanna
authored andcommitted
Loop replace with Collection.removeIf() (#36351)
1 parent 6e6e63d commit 0d9e3ad

File tree

5 files changed

+6
-36
lines changed

5 files changed

+6
-36
lines changed

server/src/main/java/org/elasticsearch/common/settings/SettingsFilter.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.Collection;
2929
import java.util.Collections;
3030
import java.util.HashSet;
31-
import java.util.Iterator;
3231
import java.util.List;
3332
import java.util.Set;
3433

@@ -104,13 +103,7 @@ private static Settings filterSettings(Iterable<String> patterns, Settings setti
104103
}
105104
if (!simpleMatchPatternList.isEmpty()) {
106105
String[] simpleMatchPatterns = simpleMatchPatternList.toArray(new String[simpleMatchPatternList.size()]);
107-
Iterator<String> iterator = builder.keys().iterator();
108-
while (iterator.hasNext()) {
109-
String key = iterator.next();
110-
if (Regex.simpleMatch(simpleMatchPatterns, key)) {
111-
iterator.remove();
112-
}
113-
}
106+
builder.keys().removeIf(key -> Regex.simpleMatch(simpleMatchPatterns, key));
114107
}
115108
return builder.build();
116109
}

server/src/main/java/org/elasticsearch/discovery/zen/ElectMasterService.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.util.ArrayList;
3434
import java.util.Arrays;
3535
import java.util.Collection;
36-
import java.util.Iterator;
3736
import java.util.List;
3837
import java.util.Objects;
3938
import java.util.stream.Collectors;
@@ -206,12 +205,7 @@ private List<DiscoveryNode> sortedMasterNodes(Iterable<DiscoveryNode> nodes) {
206205
return null;
207206
}
208207
// clean non master nodes
209-
for (Iterator<DiscoveryNode> it = possibleNodes.iterator(); it.hasNext(); ) {
210-
DiscoveryNode node = it.next();
211-
if (!node.isMasterNode()) {
212-
it.remove();
213-
}
214-
}
208+
possibleNodes.removeIf(node -> !node.isMasterNode());
215209
CollectionUtil.introSort(possibleNodes, ElectMasterService::compareNodes);
216210
return possibleNodes;
217211
}

server/src/main/java/org/elasticsearch/gateway/AsyncShardFetch.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,7 @@ private void fillShardCacheWithDataNodes(Map<String, NodeEntry<T>> shardCache, D
245245
}
246246
}
247247
// remove nodes that are not longer part of the data nodes set
248-
for (Iterator<String> it = shardCache.keySet().iterator(); it.hasNext(); ) {
249-
String nodeId = it.next();
250-
if (nodes.nodeExists(nodeId) == false) {
251-
it.remove();
252-
}
253-
}
248+
shardCache.keySet().removeIf(nodeId -> !nodes.nodeExists(nodeId));
254249
}
255250

256251
/**

server/src/main/java/org/elasticsearch/indices/store/IndicesStore.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import java.util.ArrayList;
6666
import java.util.EnumSet;
6767
import java.util.HashSet;
68-
import java.util.Iterator;
6968
import java.util.List;
7069
import java.util.Set;
7170
import java.util.concurrent.TimeUnit;
@@ -135,12 +134,7 @@ public void clusterChanged(ClusterChangedEvent event) {
135134
// remove entries from cache that don't exist in the routing table anymore (either closed or deleted indices)
136135
// - removing shard data of deleted indices is handled by IndicesClusterStateService
137136
// - closed indices don't need to be removed from the cache but we do it anyway for code simplicity
138-
for (Iterator<ShardId> it = folderNotFoundCache.iterator(); it.hasNext(); ) {
139-
ShardId shardId = it.next();
140-
if (routingTable.hasIndex(shardId.getIndex()) == false) {
141-
it.remove();
142-
}
143-
}
137+
folderNotFoundCache.removeIf(shardId -> !routingTable.hasIndex(shardId.getIndex()));
144138
// remove entries from cache which are allocated to this node
145139
final String localNodeId = event.state().nodes().getLocalNodeId();
146140
RoutingNode localRoutingNode = event.state().getRoutingNodes().node(localNodeId);

server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,14 +2188,8 @@ protected void afterAdd() throws IOException {
21882188
Collections.sort(writtenOperations, (a, b) -> a.location.compareTo(b.location));
21892189
assertFalse(translog.isOpen());
21902190
final Checkpoint checkpoint = Checkpoint.read(config.getTranslogPath().resolve(Translog.CHECKPOINT_FILE_NAME));
2191-
Iterator<LocationOperation> iterator = writtenOperations.iterator();
2192-
while (iterator.hasNext()) {
2193-
LocationOperation next = iterator.next();
2194-
if (checkpoint.offset < (next.location.translogLocation + next.location.size)) {
2195-
// drop all that haven't been synced
2196-
iterator.remove();
2197-
}
2198-
}
2191+
// drop all that haven't been synced
2192+
writtenOperations.removeIf(next -> checkpoint.offset < (next.location.translogLocation + next.location.size));
21992193
try (Translog tlog =
22002194
new Translog(config, translogUUID, createTranslogDeletionPolicy(),
22012195
() -> SequenceNumbers.NO_OPS_PERFORMED, primaryTerm::get);

0 commit comments

Comments
 (0)