-1
LinkedHashMap<String, Double> testMap = (LinkedHashMap<String, Double>) sortByValue(commands.get(commandWithMaxNegativeOffset).getDataUsageCriteria());

从上面看,testMap将包含类似{New=30.0, Previous=70.0}值升序排列的东西,所以我想要做的是 if/else 循环中的下面的东西,因为目前我现在已经硬编码只是为了更有意义,但我想使用 testMap 而不是硬编码. key如果条件匹配,我想设置为值by using key/value pair from map

double percent = r.nextDouble()*100;

if(percent > 1.0 && percent < 70.0(how can I use 70 from testMap instead of hardcoding)) {
//what to put below in place of Previous
    commands.get(commandWithMaxNegativeOffset).setDataCriteria(Use the Key of 70.0 i.e Previous);
} else if(percent > 71 && percent < 100){
    commands.get(commandWithMaxNegativeOffset).setDataCriteria(Use the Key of 30.0 i.e New);
}
4

2 回答 2

1

如果我理解正确,以下可能是您正在寻找的内容:

final LinkedHashMap<String, Double> testMap = new LinkedHashMap<String, Double>();
testMap.put("New", Double.valueOf(30.0));
testMap.put("Previous", Double.valueOf(70.0));

// The map contains two entries.
Set<Map.Entry<String, Double>> entries = testMap.entrySet();
Iterator<Map.Entry<String, Double>> it = entries.iterator();
Map.Entry<String, Double> firstEntry = it.next();
Map.Entry<String, Double> secondEntry = it.next();

Random r = new Random();
double percent = r.nextDouble() * 100;

// percent is a number between 0.0 and 100.0
if (percent < secondEntry.getValue().doubleValue()) // between 0.0 and 70.0 exclusive
{
    commands.get(commandWithMaxNegativeOffset).setDataCriteria(secondEntry.getKey());
}
else // between 70.0 inclusive to 100.0 (100.0 - 70.0 = 30.0)
{
    commands.get(commandWithMaxNegativeOffset).setDataCriteria(firstEntry.getKey());
}
于 2012-05-22T23:15:08.877 回答
0

只是if(percent > map.get("new") && percent < map.get("previous"))?如果这实际上是您要问的,那么您应该在 javadoc 中停下来查看地图,然后再继续。

于 2012-05-22T22:46:13.013 回答