【问题标题】:Mapping array of strings to map将字符串数组映射到映射
【发布时间】:2012-01-23 07:20:51
【问题描述】:

我有一个字符串数组。您如何将其转换为 Map(String, Object)。我需要将其转换为数据对象。

【问题讨论】:

  • 你想将每个字符串值映射到什么
  • 您能否详细说明您正在尝试进行哪种映射?也许可以举一些例子。

标签: java string object map mapping


【解决方案1】:

Map 的使用等同于associative array。如果我们将您的案例应用于此,您最终会得到每个 String 指向另一个 Object

如果这是你想要的,这会起作用:

String[] strs = new String[]; //Your string array, initialized elsewhere
Object[] os = new Object[];   //The objects that you want mapped.
Map<String, Object> m = new HashMap<String, Object>(); // I use HashMap because it is the most generic

for(int i = 0; i < strs.length; i++) {
  m.put(strs[i], os[i]); //Add each object, os[i], to the map at position str[i]
}

如果你只是想让String[] 变成一个可调整大小的数组,你可以使用这个:

List<String> a = Arrays.asList(strs);

【讨论】:

    猜你喜欢
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 2017-01-31
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2016-08-09
    相关资源
    最近更新 更多