恢复另一个哈希映射中的哈希映射的值



我想检索另一个哈希图中的哈希映射中的键值,

static HashMap<String , HashMap<String, Float>> terms = new HashMap(); 
static String date;
public static void main(String[] args) throws FileNotFoundException, ParseException, IOException {
         InputStream ips=new FileInputStream(filePath);
         InputStreamReader ipsr=new InputStreamReader(ips);
         BufferedReader br=new BufferedReader(ipsr);
         String ligne;
            while ((ligne=br.readLine())!=null){
              JSONParser jsonParser = new JSONParser();
          JSONObject jsonObject = (JSONObject) jsonParser.parse(ligne);
              date =  (String) jsonObject.get("created_at");
              String text =  (String) jsonObject.get("text");
              Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);

              List<String> ss=TokenizewithAnalyzer.tokenizeString(analyzer, text);
              for(String s : ss){
                  ajoutFrequence(s, date);}
              System.out.print("==>"+ss+" n");}
         for(Entry <String, HashMap<String, Float>> entry : terms.entrySet()){
        // float res=entry.getValue().get(date).floatValue();
         System.out.println(entry.getValue().get(date).floatValue());
           }
         br.close();
           }
    static void ajoutFrequence(String token, String date){
            if(terms.containsKey(token)){
                HashMap<String, Float> freqdate = terms.get(token);
                if(freqdate.containsKey(date)){
                    freqdate.put( date, freqdate.get(date)+1);
                }else{
                    freqdate.put(date, Float.valueOf(1));
                }
            }else{
                HashMap<String, Float> freqdate = new HashMap<>();
                freqdate.put(date, Float.valueOf(1));
                terms.put(token, freqdate);
           } }}

在输出中,我在列表中获得了频率,例如:零零1.0零零零我想做这样的事情:float freq=entry.getValue((.values((;但这是不可能的。提前谢谢你。

在第二张地图中使用日期作为键。所以我假设你可以执行以下操作:

for(Entry <String, HashMap<String, Float>> entry : terms.entrySet()){
    System.out.println(entry.getValue().get(date));
 }

若要访问第二个映射中的特定值,需要为此值提供键。在您的情况下,此键是日期。

事实上我

解决了这个问题,我使用了两个循环来遍历哈希图,我恢复了第二个哈希图的值,如下所示:

for(Entry <String, HashMap<String, Float>> entry : terms.entrySet()){
                     HashMap<String, Float> d=entry.getValue();
                     for(Entry<String,Float>ent:d.entrySet()){
                     float dd=ent.getValue();
                     System.out.println(dd);}}

最后我得到了漂浮物。谢谢。

最新更新