2

I have a list<MyObject> (java LinkedList). Each object has a key and a value property.

Client 1 need the entire list.

Client 2 will pass a key and expect a value in return

Client 3 will pass a value and expect a key in return.

The question is, since Java collections use pointers to actual objects rather than store the object, would it worthwhile storing two more maps.

Map<key, MyObject> to serve Client 2.(java HashMap)

Map<value, MyObject> to serve Client 3. (java HashMap)

This would save processing time involved in iterating through the entire list (list<MyObject>) and finding the matching key or value.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
Oliver
  • 104
  • 1
  • 9
  • 1
    Are you talking tens, hundreds or thousands of `MyObject` instances? – Steve C Apr 15 '15 at 11:57
  • in addition to what Steve said: **how many times** you will perform this search and in **which scenario**? If you save 1 second before saving 20 GB to disk or 50 ms when user open a form once per day...you may not need any _optimization_... Moreover you may also consider to use HashSet instead of LinkedList. – Adriano Repetti Apr 15 '15 at 11:59
  • Hundreds of objects. These objects are a kind of master data that clients keep looking up often. – Oliver Apr 15 '15 at 12:00
  • "hundred" is pretty small, how often? once per second with 10 clients? 1000 times per second with 1000 clients? You should also **measure** if it's a bottleneck or not (and try with another data structure instead of linked list, for example `HashSet`). Oh BTW if it's loaded once when server starts then clients may also build their own `HashMap` from `LinkedList` when they start (each client stores what it needs, not _original_ list). – Adriano Repetti Apr 15 '15 at 12:01
  • I should add that the list is constant, it will not change, it gets loaded on server start up. – Oliver Apr 15 '15 at 12:01
  • As an approximate measure, there will be about 50-60 such list. Each client can make a 100 calls. These calls happen once when the class in initialized. – Oliver Apr 15 '15 at 12:04
  • I think that the real answer will depend on the cost of your key comparison, so you should consider benchmarking it. You could probably do it in a unit test. – Steve C Apr 15 '15 at 12:05
  • The keys are all Strings, my initial thought was that iterating though a list of 100 values isn't too bad. – Oliver Apr 15 '15 at 12:08
  • Would building two extra Maps take up too much memory? So for 50 list, I'll end up with 100 Maps. – Oliver Apr 15 '15 at 12:13
  • I think this post may help you http://stackoverflow.com/questions/1383797/java-hashmap-how-to-get-key-from-value – quit Apr 15 '15 at 12:21

2 Answers2

0

Having the two maps will make it easier to code and the memory overhead of an extra map is negligible considering the readability benefit. You're right that since they are objects you wouldn't have to duplicate the contents, just the data structure.

If storing key/value pairs without MyObject is on the table you can use a Guava BiMap and get rid of the original list and overhead of MyObject. This implies Client 1 iteration order isn't important though.

Carlos Bribiescas
  • 4,197
  • 9
  • 35
  • 66
0

I think this post may help you Java Hashmap: How to get key from value? And 2 maps to MyObject do not give you much space, because entry object and entry array will be created on each map. You better Map<key, value> and Map<value, key>. If this key values are simple types you can use http://labs.carrotsearch.com/hppc-api-and-code-examples.html which is high performance, memory optimized data structures.

P.S. And i suggest you never use LinkedList

Community
  • 1
  • 1
quit
  • 282
  • 2
  • 9