【问题标题】:Recover the value of a hashmap that is in another hashmap恢复另一个哈希图中的哈希图的值
【发布时间】:2017-06-11 21:17:18
【问题描述】:

我想在另一个Hashmap中检索一个Hashmap中key的值,

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();但这是不可能的。提前谢谢你。

【问题讨论】:

  • 你想做什么?您想打印条目中所有地图的所有值吗?
  • 我需要得到float类型的频率,因为以后我必须对这个float进行计算(乘法,除法)。
  • 您不能将数组转换为浮点数...您的 entry.getValue().values() 将返回浮点数集合,
  • 以及如何恢复这个浮动?

标签: java


【解决方案1】:

您在第二张地图中使用日期作为键。所以我假设你可以做到以下几点:

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

要访问第二个地图中的特定值,您需要为该值提供一个键。在你的情况下,这个键是一个日期。

【讨论】:

  • 我到处都是 null。
  • @celia 你在哪里有一个空值?在第二张地图中使用日期作为键的目的是什么?
  • 我在输出中得到 null 。我将 date 声明为全局变量,因为它无法识别它。
  • 您需要使用与“ajoutFrequence(s, date);”中相同的日期
  • 我得到这个 1.0 null null null
【解决方案2】:

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

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);}}

最后我得到了浮动。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 2013-07-21
    相关资源
    最近更新 更多