Regarding counting the total of different keys

岁酱吖の 提交于 2019-12-23 05:38:14

问题


I have a Map which stores script No. and patient Id in map the script No. is key sine it is always unique ,as shown below

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 we can see above total 5 different scripts are there with patient Id 23 , so I need to calculate total different scripts and scripts No. also associacated with patient Id 23 that is 123,323,153,623,125

I have shown the value as Example but in my situation dynamic values will populate in Map so please advise.


回答1:


First off, you should use generics, they're much easier to work with.

Now all you have to do is go through each key and see if the associated value is what you're looking for:

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

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



回答2:


Sounds like you need a map in the other direction, that maps patient id to count. Every time you insert a new record, get the entry for that patient, and increment it.




回答3:


If LinkedHashMap is the only data structure you have, the only way to do this is by iterating over it, and comparing every patient id to 23.

If for some reason this is unsatisfactory, you could build an auxiliary data structure. However, it's hard to make specific recommendations in this direction without knowing more about your problem.




回答4:


Either iterate through your Map or create a Map where the 'Patient Id' is the key and the value is a list of 'script No's.

    //Field to store the data in:
    Map<PatientId, List<ScriptId>> partientScriptMap = new HashMap<PatientId, 

    // method to add data
    void add(PatientId p, ScriptId s) {
        List<ScriptId> scriptIds = partientScriptMap.get(p);
        if (null == p) {
            scriptIds = new ArrayList<ScriptId>();
        }
        scriptIds.add(s);
        partientScriptMap.put(p, scriptIds);
   }

   // get all ScriptIds related to a PationId 
   List<ScriptId> getScriptIds(PatientId p) {
        return partientScriptMap.get(p);
   }

Is your ScriptIds and PatientIds are all Strings, replace them in the above code by String and than you would use it like this:

    add("123", "23"); 
    add("323", "23");
    add("153", "23");
    add("623", "23");
    add("125", "23");
    add("122", "24");
    add("167", "24");
    add("173", "24");
    add("113", "25");


    System.out.println("scriptIds for 24: "+getScriptIds("24");



回答5:


int count = 0;
for (int i = 0; i < m.size(); i ++) {
    if (m.get(i) == 23) count ++;
}

Just loop through and check each result against 23.



来源:https://stackoverflow.com/questions/14242951/regarding-counting-the-total-of-different-keys

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!