Java : Map.Entry 示例
Java 中的 Map.Entry (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 Map.Entry<K,V> 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
final var entry1 = Map.entry("a", 10);
System.out.println(entry1); // a=10
final var entry2 = Map.entry("b", 20);
System.out.println(entry2); // b=20
final var entry3 = Map.entry("c", 30);
System.out.println(entry3); // c=30
final var map = Map.ofEntries(entry1, entry2, entry3);
System.out.println(map); // {b=20, c=30, a=10}
final var map = new HashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
System.out.println("-- entries --");
for (final var entry : map.entrySet()) {
System.out.printf("key = %s : value = %d%n",
entry.getKey(), entry.getValue());
}
// Result
// ↓
//-- entries --
//key = a : value = 10
//key = b : value = 20
//key = c : value = 30
Methods
static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey ()
final var map = Map.ofEntries(
Map.entry("b", 10),
Map.entry("a", 20),
Map.entry("d", 30),
Map.entry("c", 40)
);
{
final var comparator = Map.Entry.<String, Integer>comparingByKey();
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [a=20, b=10, c=40, d=30]
}
{
final var comparator = Map.Entry.<String, Integer>comparingByKey(
Comparator.reverseOrder());
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [d=30, c=40, b=10, a=20]
}
static <K, V> Comparator<Map.Entry<K,V>> comparingByKey (Comparator<? super K> cmp)
final var map = Map.ofEntries(
Map.entry("b", 10),
Map.entry("a", 20),
Map.entry("d", 30),
Map.entry("c", 40)
);
{
final var comparator = Map.Entry.<String, Integer>comparingByKey();
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [a=20, b=10, c=40, d=30]
}
{
final var comparator = Map.Entry.<String, Integer>comparingByKey(
Comparator.reverseOrder());
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [d=30, c=40, b=10, a=20]
}
static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue ()
final var map = Map.ofEntries(
Map.entry("a", 20),
Map.entry("b", 10),
Map.entry("c", 40),
Map.entry("d", 30)
);
{
final var comparator = Map.Entry.<String, Integer>comparingByValue();
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [b=10, a=20, d=30, c=40]
}
{
final var comparator = Map.Entry.<String, Integer>comparingByValue(
Comparator.reverseOrder());
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [c=40, d=30, a=20, b=10]
}
static <K, V> Comparator<Map.Entry<K,V>> comparingByValue (Comparator<? super V> cmp)
final var map = Map.ofEntries(
Map.entry("a", 20),
Map.entry("b", 10),
Map.entry("c", 40),
Map.entry("d", 30)
);
{
final var comparator = Map.Entry.<String, Integer>comparingByValue();
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [b=10, a=20, d=30, c=40]
}
{
final var comparator = Map.Entry.<String, Integer>comparingByValue(
Comparator.reverseOrder());
final var sorted = map.entrySet().stream().sorted(comparator).toList();
System.out.println(sorted); // [c=40, d=30, a=20, b=10]
}
static <K, V> Map.Entry<K,V> copyOf (Map.Entry<? extends K,? extends V> e)
final var entry = new AbstractMap.SimpleEntry<>("a", 10);
System.out.println(entry); // a=10
final var copied = Map.Entry.copyOf(entry);
System.out.println(copied); // a=10
entry.setValue(999);
System.out.println(entry); // a=999
System.out.println(copied); // a=10
boolean equals (Object o)
final var entry1 = Map.entry("a", 10);
final var entry2 = Map.entry("a", 10);
System.out.println(entry1.equals(entry2)); // true
final var entry1 = Map.entry("a", 10);
final var entry2 = Map.entry("a", 20);
final var entry3 = Map.entry("b", 20);
System.out.println(entry1.equals(entry2)); // false
System.out.println(entry1.equals(entry3)); // false
System.out.println(entry2.equals(entry3)); // false
K getKey ()
final var map = new HashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
System.out.println("-- entries --");
for (final var entry : map.entrySet()) {
System.out.printf("key = %s : value = %d%n",
entry.getKey(), entry.getValue());
}
// Result
// ↓
//-- entries --
//key = a : value = 10
//key = b : value = 20
//key = c : value = 30
V getValue ()
final var map = new HashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
System.out.println("-- entries --");
for (final var entry : map.entrySet()) {
System.out.printf("key = %s : value = %d%n",
entry.getKey(), entry.getValue());
}
// Result
// ↓
//-- entries --
//key = a : value = 10
//key = b : value = 20
//key = c : value = 30
int hashCode ()
final var entry1 = Map.entry("a", 10);
System.out.println(entry1.hashCode()); // 107
final var entry2 = Map.entry("a", 20);
System.out.println(entry2.hashCode()); // 117
final var entry3 = Map.entry("b", 20);
System.out.println(entry3.hashCode()); // 118
V setValue (V value)
final var entry = Map.entry("a", 10);
System.out.println(entry); // a=10
try {
var _ = entry.setValue(999);
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException! : " + e.getMessage());
}
// Result
// ↓
//UnsupportedOperationException! : not supported
final var entry = new AbstractMap.SimpleEntry<>("a", 10);
System.out.println(entry); // a=10
final var ret = entry.setValue(999);
System.out.println(ret); // 10
System.out.println(entry); // a=999
相关文章
- API 示例