【问题标题】:Conversion of Map<String, String> to List of Objects将 Map<String, String> 转换为对象列表
【发布时间】:2021-11-17 06:52:39
【问题描述】:

以下是我想要实现的目标。

"timezones": [
    {
        "name" : "America/New_York",
        "label" : "US Eastern Time"
    },
    {
        "name" : "America/Chicago",
        "label" : "US Central Time"
    },
    
    {
        "name" : "America/Denver",
        "label" : "US Mountain Time"
    },
    {
        "name" : "America/Los_Angeles",
        "label" : "US Pacific Time"
    },

 ]

下面是我的代码 sn-p。

    Map<String,String> tz = new HashMap<>();
    tz.put("America/New_York", "US Eastern Time");
    tz.put("America/Chicago", "US Central Time");
    tz.put("America/Denver", "US Mountain Time");
    tz.put("America/Los_Angeles", "US Pacific Time");
    

    List<Timezone> timezoneList = new ArrayList<>();
    for (String key : tz.keySet()) {
        Timezone timezone = new Timezone();
        String value = tz.get(key);
        timezone.setName(key);
        timezone.setLabel(value);
        timezoneList.add(timezone);
    }

在这里,我基于键集迭代映射,然后从中获取值,然后创建一个对象并将其添加到列表中。这看起来像很多过程。

有没有更好的方法来做到这一点?

【问题讨论】:

  • 我会迭代 tz.entrySet 以避免不必要的密钥查找。我还会在 ArrayList 上设置初始容量。但除此之外,没关系。

标签: java spring-boot performance collections java-stream


【解决方案1】:

这是一种“现代”方式,使用流和record,但你写的很好。

public class SO69307363 {
    record TimeZone(String name, String label){};

    public static void main(String[] args) {
        Map<String,String> tz = new HashMap<>();
        tz.put("America/New_York", "US Eastern Time");
        tz.put("America/Chicago", "US Central Time");
        tz.put("America/Denver", "US Mountain Time");
        tz.put("America/Los_Angeles", "US Pacific Time");

        List<TimeZone> timeZones = tz.entrySet().stream()
                .map(e -> new TimeZone(e.getKey(), e.getValue()))
                .collect(Collectors.toList());
    }
}

我认为这种方式更容易阅读和理解,只是因为代码更少。

【讨论】:

    【解决方案2】:
    1. 如果输入数据以上述 JSON 字符串的形式提供,最好实现 POJO,然后将该 JSON 反序列化为列表/数组。

    2. 如果输入数据以映射的形式提供,则应为Timezone 类实现构造函数/映射器方法/构建器来转换映射条目:

    Map<String,String> tz = Map.of(
        "America/New_York", "US Eastern Time",
        "America/Chicago", "US Central Time",
        "America/Denver", "US Mountain Time",
        "America/Los_Angeles", "US Pacific Time"
    );    
    
    List<Timezone> timezoneList = tz.entrySet()
        .stream()
        .map(e -> new Timezone(e.getKey(), e.getValue())) // constructor
    //  .map(e -> new TimezoneBuilder().withName(e.getKey()).withLabel(e.getValue()).build()
        .collect(Collectors.toList());
    

    【讨论】:

      【解决方案3】:

      如果您使用 Apache 公共库,那么这里有一个适合您的单行解决方案。

      List&lt;Timezone&gt; collect = CollectionUtils.collect(tz.entrySet().iterator(), entry -&gt; new Timezone(entry.getKey(), entry.getValue()), new ArrayList&lt;&gt;());

      如果您更喜欢使用 Java-8 语法,请按照上面 tgdavies 提供的答案进行操作。

      【讨论】:

        【解决方案4】:

        所有答案都是正确的,如果你使用的是JDK16+,toList()可以直接使用,并且会返回unmodifiableList,

        List<Timezone> timezones = tz.entrySet()
                                             .stream()
                                             .map(a -> new Timezone(a.getKey(), a.getValue()))
                                             .toList();
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-30
          • 1970-01-01
          • 2016-07-29
          • 2013-05-24
          • 2014-01-29
          • 1970-01-01
          • 1970-01-01
          • 2016-07-21
          相关资源
          最近更新 更多