Java - How to create new Entry (key, value)
2016-12-12 21:11
309 查看
I'd like to create new item that similarly to Util.Map.Entry that will contain the structure key, value.
The problem is that I can't instantiate a Map.Entry because it's an interface.
Does anyone know how to create a new generic key/value object for Map.Entry?
You can just implement the Map.Entry<K, V> interface yourself:
java中如何创建Entry对象?因为Map.Entry是一个接口,所以无法直接new。
答案:需要自己去创建一个对象,实现Map.Entry接口
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;
}
}
使用:
Map.Entry<String, Object> entry = new MyEntry<String, Object>("Hello", 123);
System.out.println(entry.getKey());
System.out.println(entry.getValue());
The problem is that I can't instantiate a Map.Entry because it's an interface.
Does anyone know how to create a new generic key/value object for Map.Entry?
You can just implement the Map.Entry<K, V> interface yourself:
java中如何创建Entry对象?因为Map.Entry是一个接口,所以无法直接new。
答案:需要自己去创建一个对象,实现Map.Entry接口
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;
}
}
使用:
Map.Entry<String, Object> entry = new MyEntry<String, Object>("Hello", 123);
System.out.println(entry.getKey());
System.out.println(entry.getValue());
相关文章推荐
- How to set value for webpart’s properties when I create a new sharepoint template
- How to add a new key from kernel level to java level
- How to sort a Map<Key, Value> on the values in Java?
- How-to: resolve " java.lang.OutOfMemoryError: unable to create new native thread" for hbase thrift
- How to chage the default opstion value when adding new products?
- java.lang.OutOfMemoryError: unable to create new native thread
- How to create new geodatabases-workspace
- Java中OutOfMemoryError与unable to create new native thread(JVM创建大量线程)的关系
- 【转】解决 - java.lang.OutOfMemoryError: unable to create new native thread
- How To Create A Data Entry Screen
- how to loop through the Params property for a page and how to display each key/value pair
- how to sign an assembly with a strong name & how to create a pair key怎样通过强名给程序集签名以及如何创建钥匙对?
- How to deal with javadoc createProcess issue?
- tomcat 配置 OutOfMemoryError: Java heap space OutOfMemoryError: PermGen space OutOfMemoryError: unable to create new native thread
- linux java.lang.OutOfMemoryError: unable to create new native thread
- 【转】tomcat 配置 OutOfMemoryError: Java heap space OutOfMemoryError: PermGen space OutOfMemoryError: unable to create new native thr
- How to Create a site at the specified URL and new database (Command-Line Operation)
- How to create new geodatabases
- How to create a new routine
- Java中OutOfMemoryError与unable to create new native thread(JVM创建大量线程)的关系