1

I have LinkedHashMap returning from db. From this map i need to get exactly last element(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 linked hashmap returned from db. how can i do that ?

below is the code how i get data from data base.

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

from this map i need to take last element.

Thanks!

rogerdpack
  • 56,766
  • 33
  • 241
  • 361
user1016403
  • 11,631
  • 34
  • 103
  • 135
  • 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
  • @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

2 Answers2

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
  • 111,884
  • 15
  • 126
  • 200
  • 4
    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
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
  • 351
  • 2
  • 5