aspose file tools*
The moose likes Java in General and the fly likes I want to check previous (value) of HashMap before adding the current key? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "I want to check previous (value) of HashMap before adding the current key?" Watch "I want to check previous (value) of HashMap before adding the current key?" New topic
Author

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

rosan samuel
Ranch Hand

Joined: Nov 17, 2010
Posts: 37
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 ?


Java Bee
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
rosan samuel
Ranch Hand

Joined: Nov 17, 2010
Posts: 37
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?


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I want to check previous (value) of HashMap before adding the current key?
 
Similar Threads
NX: unlock()-call in the finally-block of the book-method
Confirmation on HashMap
Hashmap in Struts
Question on hasMap
Overriding Hashcode() and Equals()