【问题标题】:Iterate HashMap<String, HashMap<String, Integer>> [closed]迭代 HashMap<String, HashMap<String, Integer>> [关闭]
【发布时间】:2016-06-11 15:32:32
【问题描述】:

我从 json 查询中获得了三个值,我认为我需要将其存储在 HashMap&lt;String, HashMap&lt;String, Integer&gt;&gt; 中以供稍后,迭代它并存储在 sqlite 数据库中,但我不知道如何迭代它。

我有 3 个值:名称、产品和数量,我需要将其存储在:

db.execSQL("Insert into table(name, product , quantity) values ('" + name + "', '" + product + "' + " + quantity + ")");

有什么想法吗?

谢谢(对不起我的英语)

【问题讨论】:

    标签: java android mysql sqlite hashmap


    【解决方案1】:

    在 Java 8 中:

     mainHash.forEach ((name, products) -> {
           products.forEach ((product, quantity) -> {
                 db.execSQL("Insert into table(name, product , quantity) values ('" + name + "', '" + product + "' + " + quantity + ")");
           }
     });
    

    在旧版 Java 中:

    for (Map.Entry <String, HashMap <String, Integer>> nameEntry: mainHash) {
         String name = nameEntry.getKey();
         for (Map.Entry <String, Integer> productEntry: nameEntry.getValue ()) {
             String product = productEntry.getKey();
             Integer quantity = productEntry.getValue();
              db.execSQL("Insert into table(name, product , quantity) values ('" + name + "', '" + product + "' + " + quantity + ")");
         }
    }
    

    【讨论】:

    • 谢谢,但是,如何在 android 中做到这一点?
    • 谢谢,成功了! :)
    猜你喜欢
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 2015-02-19
    • 2011-12-02
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多