0

Please don't consider this question as duplicate although I have a similiar question earlier but this time it is different..

Map m = new LinkedHashMap();
m.put ("123", "23"); 
m.put ("323", "23");
m.put ("153", "23");
m.put ("623", "23");
m.put ("125", "23");
m.put ("122", "24");
m.put ("167", "24");
m.put ("173", "24");
m.put ("113", "25");

Now my query is is that I need to find out the that how many scripts are there associated with patient Id 23 as seen above 5 total different scripts are there,

I have done this..

List<String> keys = new ArrayList<String>();
        for(String str: m.keySet())
        {
            if(m.get(str).equals("23")) {
                keys.add(str);
            }
        }

but it shows complier error cannot convert object to string please advise.

TN888
  • 7,659
  • 9
  • 48
  • 84

3 Answers3

3

Change

Map m = new LinkedHashMap();

to

Map <String,String>m = new LinkedHashMap<String,String>();

So it knows that it's a map of strings pointing to strings.

dashrb
  • 952
  • 4
  • 10
1

"cannot convert object to string please advise" <-- this is normal since your Map declaration does not allow for this.

You should declare:

Map<String, String> m = new LinkedHashMap<String, String>();
fge
  • 119,121
  • 33
  • 254
  • 329
0

You haven't defined your Map as a Map of Strings. Therefore, the keyset you're iterating over is a set of Objects. Try changing your Map declaration to:

Map<String, String> m = new LinkedHashMap<String, String>();
Adrián
  • 6,135
  • 1
  • 27
  • 49