[英]How to get max value by specified key from Map in java
我有来自文档的列表分数语句,每个最大分数将插入到每个索引中。 例如 :
索引0 = 0.1
指数1 = 0.3
索引2 = 0.2
指数3 = 0.5
指数4 = 0.4
指数5 = 0.6
列表得分的索引将作为value
设置到Map
,列表文档的key
为:
文件1
文件2
文件3
使用Map<listDocuments, Index>
循环后,我得到如下列表
文件1 =索引0
Doc 1 =索引1
Doc 1 =索引2
Doc 2 =索引3
Doc 2 =索引4
Doc 3 =索引5
问题是,我无法显示具有最大价值的文档,并且我只能像这样获得每个文档的最后一个value
。
Doc 1 =索引2
Doc 2 =索引4
Doc 3 =索引5
虽然我想根据下面的最大值显示它。
文件1 =索引1(0.3)
文件2 =索引3(0.5)
文件3 =索引5(0.6)
这是我的代码
int index = 0;
int index2 = 0;
double max = 0.0;
double max2 = 0.0;
LinkedHashMap< List<Integer>, Integer> data = new LinkedHashMap< List<Integer>, Integer>();
for (int d1 = 0; d1 < d - 1; d1++) {
for (int d2 = d1 + 1; d2 < d; d2++) {
//...Other code.....
}
listSentence.add(ste.maxScore());
listSentence2.add(ste.maxScore2());
max = Collections.max(listSentence);
max2 = Collections.max(listSentence2);
index = listSentence.indexOf(max);
index2 = listSentence.indexOf(max2);
data.put(d1, index);
data.put(d2, index2);
}
//code for displaying it
for (Map.Entry list : data.entrySet()) {
System.out.println("" + stemmings
.get(list.getKey().hashCode())
.get(list.getValue().hashCode()));
}
任何答案将不胜感激。 谢谢
好吧,一个简单的例子:
Map<Integer, Set<Integer>> data = new HashMap<Integer, Set<Integer>>();
for(;;) // use your loop here
{
int document;
int index;
// Do some stuff to get index and so on
if(!data.containsKey(document))
{
data.put(document, new HashSet<Integer>());
}
data.get(document).add(index);
}
// Output
for(Map.Entry<Integer, Set<Integer>> entry : data.entrySet())
{
int maxIndex = -1;
int maxScore = Integer.MIN_VALUE;
for(Integer index : entry.getValue())
{
int score;
//get the score for index
if(score > maxScore)
{
maxScore = score;
maxIndex = index;
}
}
System.out.println("Document: "+entry.getKey()+" Index with max score: "+maxIndex);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.