Java - How to create new Entry (key, value)
我想创建与
问题是我无法实例化
有谁知道如何为Map.Entry创建新的通用键/值对象?
有
它是
1 2 |
如另一个答案所述,番石榴还具有方便使用的
你说:
I can't use
Map.Entry itself because apparently it's a read-only object that I can't instantiate newinstanceof
这并不完全正确。您无法直接实例化它(即使用
注意和提示
如文档中所述,
要查找
Interface Map.Entry
All Known Implementing Classes:
AbstractMap.SimpleEntry ,AbstractMap.SimpleImmutableEntry
不幸的是1.5版本没有列出您可以使用的任何已知实现类,因此您可能会陷入自己实现的困境。
您可以自己实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.Map; final class MyEntry<K, V> implements Map.Entry<K, V> { private final K key; private V value; public MyEntry(K key, V value) { this.key = key; this.value = value; } @Override public K getKey() { return key; } @Override public V getValue() { return value; } @Override public V setValue(V value) { V old = this.value; this.value = value; return old; } } |
然后使用它:
1 2 3 |
从Guava尝试Maps.immutableEntry
这具有与Java 5兼容的优势(不同于
AbstractMap.SimpleEntry的示例:
1 2 3 | import java.util.Map; import java.util.AbstractMap; import java.util.AbstractMap.SimpleEntry; |
实例:
添加行:
1 2 3 | arr.add(new AbstractMap.SimpleEntry(2, 3)); arr.add(new AbstractMap.SimpleEntry(20, 30)); arr.add(new AbstractMap.SimpleEntry(2, 4)); |
获取行:
1 2 3 4 5 6 |
应打印:
1 2 3 4 5 6 | 2 3 20 30 2 4 |
定义图结构的边缘非常有用。就像大脑中神经元之间的神经元一样。
从Java 9开始,有一个新的实用程序方法允许创建一个不可变条目
这是一个简单的示例:
由于它是不可变的,因此调用
注意:如果您需要直接创建一个0到最多10个(键,值)对的
为什么
使用
您实际上可以选择:
就像其他人提到的那样,番石榴的
我更喜欢
我定义了一个我一直使用的通用Pair类。这很棒。另外,通过定义静态工厂方法(Pair.create),我只需要经常写一次类型参数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | public class Pair<A, B> { private A component1; private B component2; public Pair() { super(); } public Pair(A component1, B component2) { this.component1 = component1; this.component2 = component2; } public A fst() { return component1; } public void setComponent1(A component1) { this.component1 = component1; } public B snd() { return component2; } public void setComponent2(B component2) { this.component2 = component2; } @Override public String toString() { return"<" + component1 +"," + component2 +">"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((component1 == null) ? 0 : component1.hashCode()); result = prime * result + ((component2 == null) ? 0 : component2.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Pair<?, ?> other = (Pair<?, ?>) obj; if (component1 == null) { if (other.component1 != null) return false; } else if (!component1.equals(other.component1)) return false; if (component2 == null) { if (other.component2 != null) return false; } else if (!component2.equals(other.component2)) return false; return true; } public static <A, B> Pair<A, B> create(A component1, B component2) { return new Pair<A, B>(component1, component2); } } |
如果您正在使用Clojure,则还有另一种选择:
1 2 3 | (defn map-entry [k v] (clojure.lang.MapEntry/create k v)) |