2

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.

M. Justin
  • 14,487
  • 7
  • 91
  • 130
user1016403
  • 12,151
  • 35
  • 108
  • 137
  • 1
    The last element in which order? Lexicographic? – Tudor Mar 08 '12 at 11:06
  • As you said, there is no ordering on sets (hence the name). How do you define *last* if you have no ordering? The one with the highest `hashCode()` value? Or, the one you put into the map? Or, the one which is in the *deepest* bin? – rlegendi Mar 08 '12 at 11:07
  • Hi rlegendi, Thanks for your reply. i need the one which is in the deepest bin. – user1016403 Mar 08 '12 at 11:08
  • 1
    @rlegendi: Although sets don't *generally* support ordering, several *implementations* do. `LinkedHashMap` maintains insertion order, so I'd *expect* its key and value sets to do likewise. – Jon Skeet Mar 08 '12 at 11:09
  • @JonSkeet Oh, true, sry, I forgot we are speaking of a `LinkedHashMap`. Surely, `SortedSet` implementations support ordering, but I saw not that was used. The ordering of a `LinkedHashMap` might also trick you if an existing element is put into it again (it will be not re-inserted). – rlegendi Mar 08 '12 at 11:25
  • see also http://stackoverflow.com/questions/1936462/java-linkedhashmap-get-first-or-last-entry – rogerdpack Dec 09 '15 at 07:21

3 Answers3

6

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)
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 6
    There are better ways to get the last value of an iterable - ways which *don't* take O(n) space. See Guava's Iterables.getLast for example. – Jon Skeet Mar 08 '12 at 11:29
  • Good point, @Jon Skeet. But I wrapped set with `ArrayList` that does not have such problem. – AlexR Mar 08 '12 at 11:30
  • 2
    Well the code you've given won't even compile, but if it were `new ArrayList(map.getKeys())` then it *would* exhibit that problem - because that constructor copies all the elements. – Jon Skeet Mar 08 '12 at 11:38
  • @JonSkeet can you post a link to a better solution or just post an Answer? I don't see 'Guava's Iterables.getLast' :( – AlikElzin-kilaka Sep 23 '12 at 15:48
  • @kilaka: If you search for Guava Iterables I'm sure you'll find it. – Jon Skeet Sep 24 '12 at 05:45
  • 1
    As I see it, Iterables#getLast calls Iterators#getLast method in case of LinkedHashSet, so that O(n) is guaranteed... LinkedHashSet is not SortedSet – lisak Sep 30 '13 at 15:44
1

The last entry of a LinkedHashMap can be easily accessed using the lastEntry() method which is being added in Java 21 as part of the sequenced collections enhancement. The last key can be retrieved from this Entry.

LinkedHashMap<String,String> map = someDao.getMap(String input);

Entry<String,String> lastValue = map.lastEntry();
String lastKey = lastEntry.getKey();
M. Justin
  • 14,487
  • 7
  • 91
  • 130
0

Answer from a different post helped me with this answer. Pls refer Java HashMap: How to get a key and value by index? for the original post.

Object myKey = myHashMap.keySet().toArray()[0];

where i have replaced the 0

toArray()[0]  - (where 0 represents the first item in the keyset)

with the size of the keyset

toArray()[(keyset().size)-1] 

Note : Without the -1 at the end, you would get a ArrayIndexOutOfBoundsException.

Community
  • 1
  • 1
DiTap
  • 361
  • 2
  • 5