Good day. I need advice on how to find all the values ​​in the TreeMap with an "incomplete" key. Suppose there are some values ​​with the keys "Book", "Boom", "Trap" ... With the key "Boo" should output the values ​​at which the key begins on this piece.

Tried to use the methods ceilingKey(), floorKey(), higherKey(), lowerKey() , but ran into a problem, displays only one value. I tried to delete items after receiving it, then it displays the entire TreeMap until it is empty.

Maybe there is some more suitable approach for this task?

    2 answers 2

    There are several such answer questions in English-language SO.

    The idea is this: we use the subMap method, the first parameter is our prefix (or as you say "incomplete key"), the second parameter is the same partial key, only with the last character changed, which is one more than the previous one (or you can just end key "add Character.MAX_VALUE ).

      Judging by the en-SO, you can:

       public SortedMap<String, Object> getByPreffix(NavigableMap<String, Object> myMap, String preffix ) { return myMap.subMap( preffix, preffix + Character.MAX_VALUE ); }