如何在不使用getKey方法的情况下从Java中的Object List创建Maps列表? [英] How to create list of Maps from List of Object in java without having getKey method?

查看:63
本文介绍了如何在不使用getKey方法的情况下从Java中的Object List创建Maps列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建地图列表,其中从类属性的名称推断每个键的名称,并使用getter方法放置值

How to create a list of maps, where each key name is inferred from name of the class attribute, and value is to be put by getter method

我在Java中有以下课程

I am having following class in java

class DTA {
  private String id;
  private String age;

  @Override
  public String toString() {
    return "DTA{" +
            "id='" + id + '\'' +
            ", age='" + age + '\'' +
            '}';
  }

  public DTA(String id, String age) {
    this.id = id;
    this.age = age;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getAge() {
    return age;
  }

  public void setAge(String age) {
    this.age = age;
  }
}

我有一个DTA类型的对象列表

I am having a list of objects of type DTA

List<DTA> listA = new ArrayList<>();
listA.add(new DTA("A", "15"));
listA.add(new DTA("B", "25"));

我想创建一个包含以下内容的有序地图列表(有点像scala).

I want to create an ordered list of maps (somewhat like scala) which has following content.

List<? extends Map<String, String>>

List(Map("id"->"A", "age"->"15"), Map("id"->"B", "age"->"25"))

推荐答案

没有动力学",简单的事情看起来像:

Without "dynamics", the straight forward thing might look like:

List<Map<String, String>> x = listA
            .stream()
            .map(this::toMap)
            .collect(Collectors.toList());

使用本地帮助程序,例如:

with a local helper, such as:

private Map<String, String> toMap(DTA dta) {
    Map<String, String> rv = new HashMap<>();
    rv.put("id", dta.getId());
    rv.put("age", dta.getAge());
    return rv;
}

要在此处完全动态,您必须使用反射以查询字段名称.您可以在此处找到示例.

In order to be fully dynamic here, you would have to use reflection to query the field names. You can find examples how to do that here.

但是我强烈建议您这样做:反思应该永远是您的最后选择. DTA 的概念表明,无论如何,您都有来自某些服务"的对象数据.如果是这样,为什么要先序列化为特定的DTA类,然后将该信息拼合"为某种通用Map结构?!

But I strongly suggest to not do that: reflection should always be your last resort. The notion of DTA suggests that you have that object data coming from some "service" anyway. If so, why first serialize into a specific DTA class, to then "flatten" that information into some generic Map structure?!

含义:当该服务为您提供序列化为JSON或XML的对象时...那么,最好使用gson或jackson之类的库直接直接反序列化该对象数据放入此类通用的扁平"基于Map的对象中.例如,杰克逊有一个 JsonNode类.当反序列化为此类对象时,您将免费获得该字段名称的映射!请参见此处更多示例代码.

Meaning: when that service gives you objects that are serialized as, say JSON, or XML ... then it would be much better to simply use a library like gson or jackson to directly deserialize that data into such generic "flat" Map-based objects. Jackson for example has a JsonNode class. When you deserialize into such objects, you get that mapping of field names for free! See here more example code.

重点是:可以使用反射来识别字段.但是 reflection 代码总是很乏味,并且容易出错.如果可能,请不要自己这样做.

The point is: identifying fields using reflection is possible. But reflection code is always tedious, and error prone. If possible, stay away from doing that yourself.

这篇关于如何在不使用getKey方法的情况下从Java中的Object List创建Maps列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆