How to retrieve the first key from Gee.SortedMap in Vala?

42 Views Asked by At

How do I retrieve the first key from Gee.SortedMap in Vala? For example if I have

Gee.SortedMap<int, string> foo = new Gee.TreeMap<int, string> ();

I want to get the first key, i.e. the lowest int in foo. In Java we have java.util.SortedMap.firstKey(). I cannot find an equivalent in Vala.

1

There are 1 best solutions below

0
On

The SortedMap has an ascending_keys property that returns a SortedSet. Then you can get the first() item from the SortedSet:

void main () {
    Gee.SortedMap<int, string> foo = new Gee.TreeMap<int, string> ();
    foo.set(2, "two");
    foo.set(1, "one");
    foo.set(0, "zero");
    print(@"First sorted key: $(foo.ascending_keys.first())\n");
}