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)

3 Answers 3

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

@mdehghani, here you are.
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.

4 Comments

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).
James B: the method you have provided seems to solely retrieve the value based on key and returns null if I insert the index
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) : "";
thx. but that is not the answer of my question. keys are different from indexes.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.