I want to check previous (value) of HashMap before adding the current key?
rosan samuel
Ranch Hand
Joined: Nov 17, 2010
Posts: 37
posted
0
Hai friends..
Assume a HashMap has set of key / value pairs.
I need to put new key/Value pair.
But one condition i have to check first ,before adding the Key to HashMap it hasto be checked with previous stored value.
check if it is available or not ?
It sounds like the wrong data type. I'm guessing you're looking for a stack or a Map of <String,List> or similar
rosan samuel
Ranch Hand
Joined: Nov 17, 2010
Posts: 37
posted
0
I am Having .Properties file like
AA=AB,AC,AM
AB=AC,AD
Left side of values always be unique.
AA=AB means AA act as Parent AB,AC,AM act as a children
I need to plot the relationship like this ,here i used
First this relationship stored in HashMap => AA is Key AB,AC,AM is value(ArrayList) of HashMap So i need to check the current key (i.e I am going to insert new one) ,I have to check previous Value what i stored in map.
Likewise I am going to insert second Line Value AC,AD.I need to check with previous Key and Value?if present means no need for new insertion .only i take previous same reference .So do you suggest any idea
Is this homework?
Can I suggest the following: Yes, you have a Map where the key is a String value (eg AA, AB) and then each key can contain a Set of Strings.
This would then be Map<String, Set<String>>
rosan samuel wrote:So i need to check the current key (i.e I am going to insert new one) ,I have to check previous Value what i stored in map.
Likewise I am going to insert second Line Value AC,AD.I need to check with previous Key and Value?if present means no need for new insertion .only i take previous same reference .So do you suggest any idea
It is not exactly clear to me what you are asking.
But look at the API documentation for interface Map - it contains a method to check if the map contains a key. And as you can see in the description of the put() method, if you put a value in a map with a key that already exists, then the new value will overwrite the old value.
Maybe you want to do something like this: (1) check if the map already contains an entry with a specific key, (2) if yes: then add the value to the existing list for that key, (3) if no: create a new list, add the value to it and put it in the map.
Does that help? Please let us know if you solved the problem or if you have any more questions.
Thank you for your all suggestions.
It is not a Homework.
I am developing a small project.
I think "Mr.jesper" got my idea little.
i want to explain one more time.
AA=AB,AC,AD
AB=AM,AC
AV=AK
Friends look these three lines
This lines are resides in the File.It represents relationship between "Parent" and "Child" .Left side of equal symbol called as "Parent" ,right side of equal symbol called as "children". I am having one line in the HashMap in the type of <String><ArrayList>=> "KEY"="String" (Parent) "VALUE"="ArrayList" (Child elements). Now I want to insert second line , So i need to check current parent (KEY=AB) with previous children (VALUE=AB,AC,AD) .If it present means i can use that children reference as a "Parent" or if not there means i have to create new map entry.Then i took a current children (VALUE=AM,AC),again i have to compare with Previous parent (KEY=AA) and next previous children (VALUE=AB,AC,AD).After finishing checking whether i have to create new entry or use the previous reference.If you find any logic please tell me?
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: I want to check previous (value) of HashMap before adding the current key?