【发布时间】:2016-01-25 12:47:07
【问题描述】:
我正在使用以下代码创建 HashMap 条目并搜索存储的最大 值。以下剪辑作品::
//key value
LongStream.rangeClosed( 2 , 1_000_000 ).mapToInt( i -> hm.createAndGet( i) ).max().getAsInt();
如何更改它以返回与最大值关联的 key?换句话说,如何使用流来编写这个循环?
//creates entries and searches for the maximum value stored in hm, and returns the key assosiated with the highest value
int maxValue = -1;
long maxKey = -1;
for(long currentKey = maxNum; currentKey > 0; currentKey --)
{
int currentValue = hm.createAndGet( currentKey );
if( maxValue < currentValue )
{
maxKey = currentKey ;
maxStart = currStart;
}
}
return maxKey;
【问题讨论】:
标签: java functional-programming hashmap java-stream