diff --git a/libs/core/src/main/java/org/elasticsearch/common/collect/Map.java b/libs/core/src/main/java/org/elasticsearch/common/collect/Map.java index caa3aacd97f..3697f01abea 100644 --- a/libs/core/src/main/java/org/elasticsearch/common/collect/Map.java +++ b/libs/core/src/main/java/org/elasticsearch/common/collect/Map.java @@ -19,6 +19,7 @@ package org.elasticsearch.common.collect; +import java.util.AbstractMap; import java.util.Collections; import java.util.HashMap; @@ -148,6 +149,13 @@ public class Map { } } + /** + * Returns an unmodifiable Map.Entry for the provided key and value. + */ + public static java.util.Map.Entry entry(K k, V v) { + return new AbstractMap.SimpleImmutableEntry<>(k, v); + } + /** * Returns an unmodifiable {@code Map} containing the entries of the given {@code Map}. * diff --git a/libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java b/libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java index f9badb14c2e..c213518ab2e 100644 --- a/libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java +++ b/libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java @@ -112,6 +112,13 @@ public class Map { return java.util.Map.ofEntries(entries); } + /** + * Delegates to the Java9 {@code Map.entry()} method. + */ + public static java.util.Map.Entry entry(K k, V v) { + return java.util.Map.entry(k, v); + } + /** * Delegates to the Java10 {@code Map.copyOf()} method. */ diff --git a/libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java b/libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java index 84034fcbec4..49e92008988 100644 --- a/libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java +++ b/libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java @@ -21,8 +21,6 @@ package org.elasticsearch.common.collect; import org.elasticsearch.test.ESTestCase; -import java.util.AbstractMap; - import static org.hamcrest.CoreMatchers.equalTo; public class MapTests extends ESTestCase { @@ -101,9 +99,9 @@ public class MapTests extends ESTestCase { public void testOfEntries() { final java.util.Map map = Map.ofEntries( - new AbstractMap.SimpleEntry<>(numbers[0], 0), - new AbstractMap.SimpleEntry<>(numbers[1], 1), - new AbstractMap.SimpleEntry<>(numbers[2], 2) + Map.entry(numbers[0], 0), + Map.entry(numbers[1], 1), + Map.entry(numbers[2], 2) ); validateMapContents(map, 3); }