4

i'm new to java.. i've made a linked hashmap like :

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);

I know how to find each item's index using (for example):

String duemonth="1394/08";
            int indexduemonth = new ArrayList<String>(MonthlyCPIMenu.keySet()).indexOf(duemonth);

but I don't know how to find the value using index. (I know how to get the value using key but in this case i should use index for some reason)

mdehghani
  • 504
  • 1
  • 7
  • 23

3 Answers3

3

A crude way to do it would be

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

but LinkedHashMap generally doesn't support efficient indexed retrieval, and it doesn't provide any API for the purpose. The best algorithm to do it is just to take MonthlyCPIMenu.keySet().iterator(), call next() index times, and then return the result of one final next():

<K, V> K getKey(LinkedHashMap<K, V> map, int index) {
    Iterator<K> itr = map.keySet().iterator();
    for (int i = 0; i < index; i++) {
        itr.next();
    }
    return itr.next();
}
Sajal Narang
  • 397
  • 1
  • 14
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

First, do you have a specific reason you are using a LinkedHashMap? Generally speaking, iterating over keys is cheap and lookups are 0(1). Why does order of the values matter?

You can retrieve values from a map using the get(key) method.

Map.get(key);

You can protect against nulls with:

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

This will return the value if the key is found, else return an empty string. You can replace the empty string with whatever you want.

B... James B.
  • 136
  • 1
  • 8
  • James B:the reason is that i'm using this map related to a spinner and i need the next row that a user chooses, and i think the only way is to find the index and get the value of (index+1). – mdehghani Jan 09 '16 at 21:06
  • James B: the method you have provided seems to solely retrieve the value based on key and returns null if I insert the index – mdehghani Jan 09 '16 at 21:08
  • Correct, that is how you get a value out of a key,value pair. If you want to protect against nulls then do: Map.get(key) != null ? Map.get(key) : ""; – B... James B. Jan 09 '16 at 21:09
  • thx. but that is not the answer of my question. keys are different from indexes. – mdehghani Jan 09 '16 at 22:06
0

If you are set on getting the value then use the List interface and create your own type

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;
}

}

Then use the List interface:

List<MyValue> list = new ArrayList<>();
// put all you values in the list
// get the values out by index in the list
B... James B.
  • 136
  • 1
  • 8