0

I am trying to write a generic function that return the last added value in a hashmap. i created the same method for the ArrayList but i do not know how to do it for the HashMap

public <T><T> T lastAddedObj(HashMap<Integer, T> list) {
        // TODO Auto-generated method stub
        if (list != null) {
            if (!list.isEmpty()) {
                return list.get(( list.size() - 1) );
            } else {
                Log.E(TAG, "lastAddedObj", "the generic object list received is empty!!");
                return null;
            }
        } else {
            Log.E(TAG, "lastAddedObj", "the generic object list received is null!!");
            return null;
        }
    }
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • 8
    HashMap is not ordered. There is no way to get last added value. – alex Apr 24 '15 at 12:52
  • 4
    you can't. HashMap has not notion of the insertion order. You should keep track of last inserted key, and retrieve the object trough it – Blackbelt Apr 24 '15 at 12:52
  • 3
    Please make use of Linkedhashmap. – Pratik Apr 24 '15 at 12:54
  • The whole point of a hash collection is to arrange the data randomly. What you could do is use LinkedHashMap which does keep the order of insertion, but it would perform pretty badly. – Peter Lawrey Apr 24 '15 at 12:54
  • You may like to have look at this post its awesome answer..http://stackoverflow.com/questions/3527216/accessing-the-last-entry-in-a-map – Viraj Nalawade Apr 24 '15 at 12:55
  • 2
    @PeterLawrey: "The whole point of a hash collection is to arrange the data randomly." No, I wouldn't say that at all. Surely the whole point of a hash collection is to store values for efficient lookup by key (or presence detection). The hard-and-implementation-specific arrangement is simply a result of the implementation. To put it another way: if `HashMap` had been implemented like `LinkedHashMap` (which then wouldn't exist) I don't think that would defy "the whole point" of the type... – Jon Skeet Apr 24 '15 at 12:57
  • @JonSkeet LinkedHashMap is an extension of HashMap which also maintains a linked list to give it an order. The point of hash function is to give as close to a one to one mapping as possible even for a restricted range of values and a hash function is key to how a HashMap works. The random arrangement is needed to minimise collisions in the general use case which would otherwise turn a Map access from O(1) into O(N) in Java 7, or O (log N) with the use of a tree. – Peter Lawrey Apr 24 '15 at 13:06
  • 1
    @PeterLawrey: Yes, I know the inheritance here - but I'm saying that the *purpose* of `HashMap` isn't to arrange data randomly - it's to get at data quickly by key. The random arrangement is a by-product of the way that's achieved. I agree with your description of the point of a hash function, but that's *not* the same as saying that the purpose of `HashMap` is to arrange data randomly, IMO. Put it this way - it's easy to arrange data randomly without using hash functions - but a `HashMap` implementation which did that and nothing else simply wouldn't meet the *real* purpose of a HashMap IMO. – Jon Skeet Apr 24 '15 at 13:12

2 Answers2

2

There is no direct way to do what you want. Map is an unordered construct. However, there are some options:

  • Keep a reference to the last item added somewhere
  • Create your own key type that includes information about when the key was added. This would require looping over the key set to see which key was added after some time/value.
  • Since you are using integers as a key, without knowing anything else, you could add items by using an incremented variable. Then you're back to the same situation as with the second bullet point.
MadConan
  • 3,749
  • 1
  • 16
  • 27
1

maybe you can use Arraylist for getting last value.

something like,

List<String> list = new ArrayList<>();

then after adding the items you can get its size,

int size = list.size();

and then to get last item,

String last_item = list.get(size - 1);
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
Akshay Shinde
  • 945
  • 10
  • 23