简体   繁体   中英

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

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, ie the lowest int in foo. In Java we have java.util.SortedMap.firstKey(). I cannot find an equivalent in Vala.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM