0
\$\begingroup\$

I have a scenario where I need to save a string as a key and DateTime as value into a hash map. Also, I need to remove strings older than 24 hours.

At the moment the HashMap is implemented as below:

HashMap<String, DateTime> sessions = new HashMap<>();
//Adding to the HashMap
sessions.put(session.toString(), DateTime.now());
//Removing from the HashMap
sessions.forEach((str, time)-> {
    if(time.clicks() < DateTime.now().minus(86400000).clicks())
        sessions.remove(str);
});

Would it be less overhead to save the DateTime clicks as Long datatype rather than saving the whole DateTime object? Like below:

HashMap<String, Long> sessions = new HashMap<>();
//Adding to the HashMap
sessions.put(session.toString(), DateTime.now().clicks);
//Removing from the HashMap
sessions.forEach((str, time)-> {
    if(time < DateTime.now().minus(86400000).clicks())
        sessions.remove(str);
});
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Why did you replace getServiceTicket by toString? \$\endgroup\$
    – Zeta
    Nov 13, 2017 at 17:35
  • \$\begingroup\$ I rolled back your question to the previous revision. Please have a look at the help-center to see what you are and are not allowed to do after having received an answer. You can always answer your own question, if you find a better way to do something, or ask a follow-up question (where you should link to this question). \$\endgroup\$
    – Graipher
    Nov 13, 2017 at 17:55
  • \$\begingroup\$ I want to change it to toString. Because I accidently put the method name in there. How changing it to toString changes the whole question? \$\endgroup\$
    – Govs
    Nov 21, 2017 at 14:30

2 Answers 2

3
\$\begingroup\$

I'll go out on a limb and say most senior developers would discourage any form of datetime arithmetic involving raw numbers rather than DateTime objects. There are a whole heap of problems you can avoid (leap years, leap seconds, timezone changes etc.) by always using datetime objects rather than timestamps. See for example Falsehoods programmers believe about time. In short, the potential for bugs completely outweighs the tiny memory savings.

\$\endgroup\$
0
\$\begingroup\$

I would suggest the following: a. Use a LinkedHashMap. Since, it keeps elements in insertion order, you should be able to optimize your code to ensure the moment you receive an entry less than 24 hours old, you can break out of the loop. Please refer the doc below : https://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html

  1. You will also definitely need top handle the multithreading issues with the map to avoid ConcurrentModificationException.
  2. Lastly, I agree with response above not to interfere with DateTime object just to have slightly lower memory footprint.
  3. Consider, how frequently you will be clearing out the entries in the map.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.