nil*_*esh 164 java collections
Entry<K,V>如果密钥未知,是否有一种优雅的方式只从HashMap 中获取一个,而不进行迭代.
由于进入的顺序并不重要,我们可以说类似的东西
hashMapObject.get(zeroth_index);
Run Code Online (Sandbox Code Playgroud)
虽然我知道没有这样的get by index方法.
如果我尝试下面提到的方法,它仍然必须得到hashmap的所有条目集.
for(Map.Entry<String, String> entry : MapObj.entrySet()) {
return entry;
}
Run Code Online (Sandbox Code Playgroud)
欢迎提出建议.
编辑:请建议任何其他数据结构满足要求.
Jes*_*per 247
地图不是有序的,因此没有"第一个条目"这样的东西,这也是为什么在Map(或HashMap)上没有get-by-index方法的原因.
你可以这样做:
Map<String, String> map = ...; // wherever you get this from
// Get the first entry that the iterator returns
Map.Entry<String, String> entry = map.entrySet().iterator().next();
Run Code Online (Sandbox Code Playgroud)
(注意:省略了检查空地图).
您的代码不会获取地图中的所有条目,它会立即返回(并且会突破循环)并找到第一个条目.
要打印第一个元素的键和值:
System.out.println("Key: "+entry.getKey()+", Value: "+entry.getValue());
Run Code Online (Sandbox Code Playgroud)
注意:调用iterator()并不意味着您在整个地图上进行迭代.
小智 91
Jesper的答案很好.另一种解决方案是使用TreeMap(您要求其他数据结构).
TreeMap<String, String> myMap = new TreeMap<String, String>();
String first = myMap.firstEntry().getValue();
String firstOther = myMap.get(myMap.firstKey());
Run Code Online (Sandbox Code Playgroud)
TreeMap有一个开销,所以HashMap更快,但只是作为替代解决方案的一个例子.
cad*_*ian 28
我想迭代器可能是最简单的解决方案.
return hashMapObject.entrySet().iterator().next();
Run Code Online (Sandbox Code Playgroud)
另一个解决方案(不太漂亮):
return new ArrayList(hashMapObject.entrySet()).get(0);
Run Code Online (Sandbox Code Playgroud)
或者(不是更好):
return hashMapObject.entrySet().toArray()[0];
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Java 8,它就像findFirst()一样简单:
快速举例:
Optional<Car> theCarFoundOpt = carMap.values().stream().findFirst();
if(theCarFoundOpt.isPresent()) {
return theCarFoundOpt.get().startEngine();
}
Run Code Online (Sandbox Code Playgroud)
如果你真的想要你建议的API,你可以继承HashMap并跟踪List中的键,例如.真的没有看到这一点,但它给你你想要的.如果您解释预期的用例,也许我们可以提出更好的解决方案.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("unchecked")
public class IndexedMap extends HashMap {
private List<Object> keyIndex;
public IndexedMap() {
keyIndex = new ArrayList<Object>();
}
/**
* Returns the key at the specified position in this Map's keyIndex.
*
* @param index
* index of the element to return
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException
* if the index is out of range (index < 0 || index >= size())
*/
public Object get(int index) {
return keyIndex.get(index);
}
@Override
public Object put(Object key, Object value) {
addKeyToIndex(key);
return super.put(key, value);
}
@Override
public void putAll(Map source) {
for (Object key : source.keySet()) {
addKeyToIndex(key);
}
super.putAll(source);
}
private void addKeyToIndex(Object key) {
if (!keyIndex.contains(key)) {
keyIndex.add(key);
}
}
@Override
public Object remove(Object key) {
keyIndex.remove(key);
return super.remove(key);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我故意没有深入研究这个泛型方面......
| 归档时间: |
|
| 查看次数: |
252339 次 |
| 最近记录: |