• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I want to check previous (value) of HashMap before adding the current key?

 
rosan samuel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

HashMap h1=new HashMap("one",two");

1. Now i am going to insert "two" (key) to the HashMap,before i need to insert i have to check with previous value="two"?

Then next scenario i have to check value of current insertion has to be checked with previous stored Key and value

2. Now i am going to insert value "two" to the HashMap , before in need to insert i have to check with previous Key="one" and previous value="two"

You people suggest any logic for this ?
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get it. What should the contents of the map be afterwards?

If you need to check for presence of a value, Map has a method called containsValue.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>>
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rosan samuel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai friends ,

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?


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic