How to get part of SortedMap for a given range of keys in Java

1 Answer

0 votes
import java.util.SortedMap;
import java.util.TreeMap;

public class MyClass {
    public static void main(String args[]) {
        SortedMap<Integer, String> smap = new TreeMap<Integer,String>();

        smap.put(3, "java");
        smap.put(5, "c");
        smap.put(6, "c++");
        smap.put(14, "c#");
        smap.put(16, "go");
        smap.put(19, "python");
        smap.put(20, "javascript");
        smap.put(22, "rust");

        System.out.println(smap.subMap(14, 20));
    }
}



/*
run:

{14=c#, 16=go, 19=python}

*/

 



answered Jun 6, 2023 by avibootz

Related questions

1 answer 101 views
101 views asked Jun 6, 2023 by avibootz
3 answers 172 views
1 answer 191 views
1 answer 196 views
...