Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check HashMap Values with a String

Status
Not open for further replies.

kondakindi

IS-IT--Management
Apr 18, 2005
31
US
Hi,
I have a Hashmap of arraylist which means the values of the hashmap are arraylist of 1,2 ,3,5 etc
when i read the specfic value of the has map based on get and check it with a string it returns false even if the string is the same.
eg:

HashMap hm=new Hashmap();
Assume this has key like "job_cat" and multiple values in form of(ArrayList) like ['a,b']
Sytem.out.println(hm.get("job_cat"));
it returns - [a,b]

if i check using a string which has 'a' value
String str="a";
if (str.equals(hm.get("job_cat")))
or if (str==hm.get("job_cat"))

Both the above statment returns false.
Could you tell me how should i check if String value is equal to Hashmap .
It doesn't even work with containsValue();

Thanks ,
 
If you're putting ArrayLists into the HashMap, you're gonna get ArrayLists out again. You can't compare a String directly with an ArrayList like you are trying to do. If you're wanting to see if the particular String (eg. "a" is in the returned ArrayList, you'll need to use something like the ArrayList.contains(Object) method.

Code:
String str = "a";
ArrayList list = (ArrayList)hm.get("job_cat");
if ( list != null ){
   if ( list.contains(str) ){
      // do something here
   }
}

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top