Re: [Vala] Problem with Gee



Hi Nicolas,

Nicolas <c r n a wanadoo fr> wrote:
Hi,

I'm trying to use the Gee library, but i have a problem;
This is the code taken from the vala site:

using Gee;

static int main (string[] args) {

    var map = new HashMap<string, int> ();
    map.set ("one", 1);
    map.set ("two", 2);
    map.set ("three", 3);
    map["four"] = 4;            // same as map.set ("four", 4)
    map["five"] = 5;
    foreach (string key in map.keys) {
        stdout.printf ("%d\n", map[key]);       // same as map.get (key)
    }

    return 0;
}

My question is, why when i execute this, the result is:
4
3
1
2
5

And not:
1
2
3
4
5

HashMap do not maintain order for its keys. In contradiction to that, TreeMap is
a map that naturally maintains ordering of its entries, by using the supplied
comparison function, of the standard comparison function for the key type.

Thus you could do:
Map<string, int> map = new TreeMap<string,int> ();

On that map, you can do insertion of the entries in any order, the resulting
entries will always be sorted by its keys.

I hope this is what you wanted.

Best regards, Didier.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]