-1

I have Question regarding the best practice of using a Collection regarding the memory. I need to call a method which returns pairs of (Key,Value) frequently, so which way is the best, using HashMap or creating an Object that contains Key and value and save this object in a HashSet?

Thanks & Regards.

Eran
  • 387,369
  • 54
  • 702
  • 768
Hazim
  • 805
  • 11
  • 24
  • 3
    The answer to "which data structure should I use" is always "depends what you need to do with it". Can you elaborate on your needs? – Mureinik Dec 08 '14 at 06:51
  • Please refer this tutorial, hope it will help you better. http://www.java2novice.com/java-collections-and-util/ – Krunal Indrodiya Dec 08 '14 at 06:52
  • Both approaches have their uses, and it may depend on whether the key is conceptually a "part" of the value (HashSet) or independent of it (HashMap). – Matt Coubrough Dec 08 '14 at 06:52
  • Thanks All, My need is save the key and values without duplication, and the objects which saved up-to 10 objects per method call, but the calling will be frequently 1 per second, so I want to use the way which consume less for memory. – Hazim Dec 08 '14 at 07:09
  • @Hazim - You are going to need to be more precise and specific than that. – Stephen C Dec 08 '14 at 07:39
  • 1
    A `HashSet` is a thin wrapper *around* a `HashMap` (look at the source code, search for `private transient HashMap map`) so theoretically, `HashMap` consumes less memory. But the overhead is minimal. – Erwin Bolwidt Dec 08 '14 at 09:24

1 Answers1

4

It depends on whether you need to search the data structure based on the key alone or both the key and the value.

  • If you search by the key alone (i.e. map.containsKey(key)), you should use a HashMap.

  • If you search for existence of a key-value pair (i.e. set.contains(new Pair(key,value)), you should use a HashSet that contains those pairs.

Another thing to consider is how you determine the uniqueness of the elements. If it is determined by the key alone, you should use a HashMap. If it is determined by both key and value (i.e. you can have the same key appear twice with different values), you must use HashSet, since HashhMap doesn't allow the same key to appear more than once.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • you can also add some special functionality to your "key value" object if needed – Milkmaid Dec 08 '14 at 06:51
  • Thanks All, My need is save the key and values without duplication, and the objects which saved up-to 10 objects per method call, but the calling will be frequently 1 per second, so I want to use the way which consume less for memory. – Hazim Dec 08 '14 at 07:09
  • 2
    @Hazim But how are you going to access this data structure? Are you going to iterate over the entire data, or search for the existence of specific values? And do you allow the same key to appear twice with different values? – Eran Dec 08 '14 at 07:12
  • i iterate the key value, no need to search only save object and iterator. i already override the hashcode of keyvalue class to prevent the duplication – Hazim Dec 08 '14 at 08:17
  • @Hazim Based on your comment, I can assume that your collection needs to allow the same key to appear twice for different values, and therefore you must use `HashSet`, and can't use `HashMap`. – Eran Dec 08 '14 at 08:20