• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing hashmaps inside ArrayList for keys and changing values accordingly  RSS feed

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Team,
I have a Problem that seems to elude my grasp.

My starting Point is this:
I have four HashMaps inside an Arraylist.

The first Hashmap has as its key the Integer value of a Mac Adress in String Format and the Mac Adress as the value.
The second Hashmap has as its key the Integer value of a Mac Adress in String Format and a PortIndex number also in String Format as the value.
The third Hashmap has as its key the PortIndex number and as its value the Instance of the ifIndex object (a 4-digit number)
The fourth HashMap has as its key the Instance of the ifIndex object (a 4-digit number) and as its value the Physical Port Description.

IntegerMac -> StringMac -> Portindex -> InstanceOfifIndexObject -> PhysPortDescr

I Need to get from StringMac -> PhysPortDescr and put it in a separate HashMap

All the keys that bind this together are there, I just dont know how I could accomplish this.

Thanks for any Help!
 
s ravi chandran
Ranch Hand
Posts: 572
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if this will solve you problem, but somewhere I had seen a solution to a similar problem.

Why don't you create multiple arrays for each of these value types which relate to each other based on index.

Suppose I make the arrays like this for any index position K:



now all you have to do is find the index of the object type you are searching for, then take the corresponsting result object type from respective array.

Cheers
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds a really bad design, and the multiple arrays thing sounds no better. I think you should create a MacAddress class which encapsulates all those data. Since you are using the data as “K”s in the Map, you shou‍ld probably make the MacAddress class immutable.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:Not sure if this will solve you problem, but somewhere I had seen a solution to a similar problem.

Why don't you create multiple arrays for each of these value types which relate to each other based on index.

Suppose I make the arrays like this for any index position K:



now all you have to do is find the index of the object type you are searching for, then take the corresponsting result object type from respective array.

Cheers


Hi,
thanks for replying and that is a really good Idea.
But does it still work when the Lists are different lengths?
For example the InstanceOfifIndexObject lists each Instance of ifIndex Objects only once, while the IntegerMac to Portindex has Portindex occuring multiple times, making the list longer.
What are your thoughts on this?
 
s ravi chandran
Ranch Hand
Posts: 572
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:. . . But does it still work when the Lists are different lengths?
For example the InstanceOfifIndexObject lists each Instance of ifIndex Objects only once, while the IntegerMac to Portindex has Portindex occuring multiple times, making the list longer.
What are your thoughts on this?


How does the length vary? Possiblity you have some value types  missing for a mapping. in that case, just keep it blank(or null). The size of array would be equal to the value array with largest size.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:All the keys that bind this together are there, I just dont know how I could accomplish this.

Well, since they all bind on a 1:1 basis (unless I've completely misunderstood), why not just stick them all in a single  MacInfo object?

Unless of course you need fast lookup by any of those keys, in which case you might need to explain your problem a bit more.

Also: A String Mac address and a numeric Mac address sounds redundant to me. Store one or the other - and my preference would be the number.

Winston
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:the IntegerMac to Portindex has Portindex occuring multiple times

Seems like this is a one-to-many relationship.  Dennis, you might want to use a simple DB like SQLite.
 
s ravi chandran
Ranch Hand
Posts: 572
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding of the problem was that all value types have some sort of relation. The objective is to find a particular value type based on any value type as key.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:My understanding of the problem was that all value types have some sort of relation.

Yes, but if it's based on HashMaps - or indeed, arrays - then it can only be a 1:1 relationship. Like Knute, I suspect that the actual relationship between a mac address and a port is 1:many, so you would need something like a MultiMap - or, as he suggests, a database.

@Dennis: Any chance you could clarify WHAT you're trying to do here? Right now, all we know is HOW you're trying to do it ... and that's plainly not working.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!