【问题标题】:Writing Hashmap to a file将 Hashmap 写入文件
【发布时间】:2020-05-25 09:02:12
【问题描述】:

我在将 Hashmap 写入文件时遇到问题。

这里是代码

    public HashMap<String, String> itemMap;

    public void SaveMapItem()
    {
        this.itemMap = new HashMap<String,String>();
        for (Item item:itemList)
        {
            itemMap.put(item.Name,item.Selected);
            Log.d(TAG,item.Name +" "+ item.Selected);
        }


        File file = new File(getDir("item", MODE_PRIVATE), "itemMap");
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
            outputStream.writeObject(itemMap);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public class Item
    {
    String Name;
    String Nickname;
    String Selected;

    public Item()
    {
        Name = "default";
        Nickname = "default";
        Selected = "false";
    }

    }

这是输出(itemMap):

    �� sr java.util.HashMap���`� F 
    loadFactorI     thresholdxp?@     w      t 
    ITEM-00015t falset 
    ITEM-00019q ~ t 
    ITEM-00018q ~ t 
    ITEM-00002q ~ x

在这个例子中,所有的项目都应该是假的。我什至尝试使用布尔值作为哈希图(HashMap&lt;String, String&gt;)的值,但他们建议我使用字符串。对吗?

【问题讨论】:

    标签: java android hashmap


    【解决方案1】:

    我发现了问题。没有问题。 值都相同,系统对所有相同的条目使用值 ~。

    【讨论】:

      猜你喜欢
      • 2018-11-15
      • 2016-09-28
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多