fast searching

Which of the util-structures should I use for:few-normal inserts, few deletions, many searches.I will allways need to store 3 values, and the two first will allways be used to search for the third.
[218 byte] By [_Yonder_a] at [2007-11-26 18:10:08]
# 1
Based on the information you presented I would suggest a Map?
zadoka at 2007-7-9 5:42:19 > top of Java-index,Core,Core APIs...
# 2

ok, I see there are several maps though, hashmap, sortedmap, etc...

I get one ip-address, and two mac-adresses every 11th ms (on average) and on every intervall I need to store this post.

What I need to do is:

1. Check if there is a post with the ip-address and first mac-address already.

If there is - return the second mac-address.

If there is not:

2. See if the ip-address exists in the collection

if it does:

3. See if the first and second mac differs from what is in the post

if it does:

4. Overwrite the existing post on that ip-address

end if.

if the ip-address does not exist:

5. Insert post in the collection

end if.

_Yonder_a at 2007-7-9 5:42:19 > top of Java-index,Core,Core APIs...
# 3

> ok, I see there are several maps though, hashmap,

> sortedmap, etc...

Look at the implementions in the table here:

http://java.sun.com/docs/books/tutorial/collections/implementations/index.html

Then look at your requirements and determine which map would be most appropriate.

zadoka at 2007-7-9 5:42:20 > top of Java-index,Core,Core APIs...
# 4

Thanks alot. Now I know I should definately use a HashMap. The problem I have now is that a HashMap can only take one key. I figured I could concat the ipaddress and the first macaddress as one string and use that as a key (or the hash of the string), but it turns out that I sometimes need to know just if the ipaddress exists in the collection, and I can't enter a "dummy"-mac1address.... I'm not sure how to solve this.

I need to be able to tell whether the ip-address is in the collection, and if it is i need to be able to tell whether the first mac is the same (as the input) or not (together with that ip). With the hashmap I will only be able to tell if the ip is there with exactly that mac1, not if is there with another mac1. As for clarification, I get these three inputs: ip, mac1, mac2.

_Yonder_a at 2007-7-9 5:42:20 > top of Java-index,Core,Core APIs...
# 5
The same ip may have several (mac1, mac2) pairs?If so, one design option is, a master HashMap that maps the ip to a sub-HashMap, and the sub-HashMap maps mac1 to mac2 for this particular ip.
OleVVa at 2007-7-9 5:42:20 > top of Java-index,Core,Core APIs...
# 6

> The same ip may have several (mac1, mac2) pairs?

>

> If so, one design option is, a master HashMap that

> maps the ip to a sub-HashMap, and the sub-HashMap

> maps mac1 to mac2 for this particular ip.

yes, the same ip may come with a new mac1 (and 2), and then I have to replace the old post with this new one.

so if I make some kind of HashMap<HashMap><String mac1><String mac2>,String ip>.

Where I do: master.get(sub.get(mac1))

and do sub.containsvalue(ip)?

hmmm

not sure I got it right there

for more info I'll be having about 500k-1m posts.

_Yonder_a at 2007-7-9 5:42:20 > top of Java-index,Core,Core APIs...
# 7

Would HashMap<IP, TwoMacAddresses> do, where an object belonging to the TwoMacAddresses class holds mac1 and mac2? You'd have to construct that class yourself, of course.

If I got your problem right, you're getting ip, mac1 and mac2 as parameters. You look up a TwoMacAddresses object from the ip. If you get such an object, you check if mac1 agrees with the one that just came in, and if so, return mac2 from the object. If they don't agree, you set mac1 and 2 in the object based on the input parameters. If you don't get an object from the HashMap, you create one and put it in.

Is it this simple? Or can you modify my sketch to do what you need?

OleVVa at 2007-7-9 5:42:20 > top of Java-index,Core,Core APIs...