I have LinkedHashMap
returning from DB. From this map I need to get the last key. If I get all keys using keySet
method it returns Set
of keys but Set
does not guarantee the order. I need to take exactly last key from the LinkedHashMap
returned from DB. How can I do that ?
Below is the code how I get data from database.
LinkedHashMap<String,String> map = someDao.getMap(String input);
From this map I need to take last key.
keySet()
being executed on LinkedHashMap
returns LinkedHashSet
that is indeed Set
but "remembers" the order of elements.
You can get the last element as following:
Map<TheType> map = .....
.................
TheType theLastKey = new ArrayList<>(map.keySet()).get(map.size() - 1)