Java 如何使用索引获取linkedhashmap值?

Java 如何使用索引获取linkedhashmap值?,java,android,Java,Android,我是java新手。。我制作了一个链接的hashmap,如: Map<String, Double> MonthlyCPIMenu = new LinkedHashMap<String, Double>(); MonthlyCPIMenu.put("1394/10", 0.0); MonthlyCPIMenu.put("1394/09", 231.6); MonthlyCPIMenu.put("1394/08", 228.7)

我是java新手。。我制作了一个链接的hashmap,如:

Map<String, Double> MonthlyCPIMenu = new LinkedHashMap<String, Double>();
        MonthlyCPIMenu.put("1394/10", 0.0);
        MonthlyCPIMenu.put("1394/09", 231.6);
        MonthlyCPIMenu.put("1394/08", 228.7);
        MonthlyCPIMenu.put("1394/07", 227.0);
        MonthlyCPIMenu.put("1394/06", 225.7);
Map monthlycpimenus=newlinkedhashmap();
月平均投入产出(“1394/10”,0.0);
每月的PIMENUS.put(“1394/09”,231.6);
每月的PIMENUS.put(“1394/08”,228.7);
每月的PIMENUS.put(“1394/07”,227.0);
每月的PIMENUS.put(“1394/06”,225.7);
我知道如何使用(例如)查找每个项目的索引:

String duemount=“1394/08”;
int indexduemount=newarraylist(monthlycpimenus.keySet()).indexOf(duemount);

但我不知道如何使用索引查找值。(我知道如何使用键获取值,但在这种情况下,出于某种原因,我应该使用索引)

一种粗略的方法是

new ArrayList<String>(MonthlyCPIMenu.keySet()).get(index);

首先,您是否有使用LinkedHashMap的特定原因?一般来说,迭代键是便宜的,查找是0(1)。为什么价值顺序很重要

可以使用get(key)方法从映射中检索值

您可以通过以下方式防止出现空值:

Map.get(key) != null ? Map.get(key) : "";

如果找到键,则返回值,否则返回空字符串。您可以将空字符串替换为您想要的任何内容。

如果您设置了获取值,则使用列表界面并创建您自己的类型

public class MyValue {
String date;
String value;

public MyValue(String d, String v) {
    this.date = d;
    this.value = v;
}

public String getDate() {
    return date;
}

public String getValue() {
    return value;
}
}

然后使用列表界面:

List<MyValue> list = new ArrayList<>();
// put all you values in the list
// get the values out by index in the list
List List=new ArrayList();
//将所有您需要的值放入列表中
//按列表中的索引获取值

James B:原因是我正在使用与微调器相关的映射,我需要用户选择的下一行,我认为唯一的方法是找到索引并获取(index+1)的值。James B:您提供的方法似乎只基于键检索值,如果插入indexCorrect,则返回null,这就是如何从键、值对中获取值的方法。如果您想防止空值,请执行以下操作:Map.get(key)!=无效的Map.get(键):“”;谢谢。但这不是我问题的答案。键与索引不同。@mdehghani,给你。
Map.get(key) != null ? Map.get(key) : "";
public class MyValue {
String date;
String value;

public MyValue(String d, String v) {
    this.date = d;
    this.value = v;
}

public String getDate() {
    return date;
}

public String getValue() {
    return value;
}
List<MyValue> list = new ArrayList<>();
// put all you values in the list
// get the values out by index in the list