【问题标题】:How to create objects from ArrayList and add them to a hashmap in Java?如何从 ArrayList 创建对象并将它们添加到 Java 中的 hashmap?
【发布时间】:2019-04-26 12:17:17
【问题描述】:

所以我有一个名为 Tokens 的 ArrayList,其中包含一系列字符串。我正在尝试从 ArrayList 中的每个字符串创建一个对象并将其添加到 HashMap。我将如何在 for 循环或其他方式中做到这一点?

hashmap 参数将是 (String, Object),其中 string(key) 是 ArrayList 索引处的字符串,而 Object 是我从该索引创建的字符串。我只需要帮助逻辑/大纲来做到这一点。

【问题讨论】:

    标签: arrays object collections hashmap


    【解决方案1】:
    List<String> tokens = new ArrayList<>();
    Map<String, Object> map = new HashMap<>();
    
    for (String s : tokens) {
        map.put(s, new Object());
    }
    

    【讨论】:

    • 请使用答案下方的edit 链接添加信息,而不是将其放在可能看不到的评论中。请将您的代码格式化为代码。
    猜你喜欢
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2012-01-01
    相关资源
    最近更新 更多