简体   繁体   English

将 map 解析为 object 的优雅方法是什么?

[英]What is the elegant way to parse a map into an object?

I have a random object and I need to parse Map<String, String>我有一个随机的 object,我需要解析 Map<String, String>

public class ExternalIncome {

    private Long operationId;

    private OffsetDateTime operationDate;

    private String operationCode;

    private String documentNumber;

    private OffsetDateTime documentDate;

    private String correspondentInn;

    private String correspondentName;

    private String correspondentAccount;
}

I've just created it this way, but I think it's not quite elegant, rather ugly.我只是这样创建的,但我认为它不是很优雅,而且很丑陋。 Also, I need to intercept every iterate of the parsing to hold dynamic fields into Map<String, String> inside of the object.此外,我需要拦截解析的每个迭代,以将动态字段保存到 object 内部的 Map<String, String> 中。

public static ExternalIncome create(Map<String, String> fields) {
        ExternalIncome externalIncome = new ExternalIncome();
        fields.forEach((k, v) -> {
            switch (k) {
                case "OPER_ID":
                    externalIncome.setOperationId(nullableLong(v));
                    break;
                case "OPER_DATE":
                    externalIncome.setOperationDate(Utils.toOffsetDateTime(v));
                    break;
               etc

Could you help me to find the best way?你能帮我找到最好的方法吗?

You can do it using Jackson's ObjectMapper class, if you are allowed to use third part library.如果允许使用第三方库,则可以使用 Jackson 的 ObjectMapper class 来完成。

final ObjectMapper mapper = new ObjectMapper();
final ExternalIncome income = mapper.convertValue(fields,ExternalIncome.class)

I don't see any elegant solution in your case, but you get rid of switch/case by using map where the key is the field name from the fields map and value is BiDirectionalConsumer.在你的案例中我没有看到任何优雅的解决方案,但是你通过使用 map 摆脱了 switch/case,其中键是字段 map 中的字段名称,值是 BiDirectionalConsumer。 Probably not the best solution, but still.可能不是最好的解决方案,但仍然如此。 Note: be careful with casts and type conversions.注意:小心强制转换和类型转换。

Let's say you have fields map:假设您有字段 map:

Map<String, String> fields = new HashMap<>();   
fields.put("OPER_ID", "3"); 

You can define the second map with field names and operation you want to perform:您可以使用要执行的字段名称和操作来定义第二个 map:

HashMap<String, BiConsumer<ExternalIncome, Object>> fieldsOperations = new HashMap<>();
fieldsOperations.put("OPER_ID", (extIncome, valueToSet) -> 
    extIncome.setOperationId(Long.valueOf((String) valueToSet))); //add as many as you want

And then where you iterate over your map with fields:然后你在哪里迭代你的 map 字段:

ExternalIncome externalIncome = new ExternalIncome();
fields.forEach((k,v) -> {        
    BiConsumer<ExternalIncome, Object> operation = fieldsOperations.get(k);
    if (operation == null) {    
        //add to dynamic fields map    
        return;
    }
    operation.accept(externalIncome, v);    
});

Or you can reconsider your data structure to get rid of the idea to have dynamic fields, but idk if it's possible with your use case.或者您可以重新考虑您的数据结构以摆脱拥有动态字段的想法,但如果您的用例可能的话,请考虑。

如何以优雅的方式使用 Java8 返回 map<object, long> 来自列表<object>计数发生?<div id="text_translate"><p> 我知道标题很拗口,所以我将尝试用一个例子来解释。</p><p> 我有一个清单。 Chores 有一个 id 字段作为标识符,但列表将包含许多家务,其中一些将具有相同的 ID。 我的目标是返回一个 Map&lt;Chore, Long&gt; ,其中 long 将计算杂务的出现次数。 例如:</p><pre> List&lt;Chore&gt; choreList = new ArrayList&lt;Chore&gt;(); choreList.add(new Chore("1", "vacuum", "7-6-22"); //Numbers 1,2,3 are the ID's for each chore choreList.add(new Chore("2", "dusting", "7-1-22"); choreList.add(new Chore("3", "mowing", "7-15-22"); choreList.add(new Chore("1", "vacuum", "7-14-22"); choreList.add(new Chore("1", "vacuum", "7-18-22"); choreList.add(new Chore("2", "dusting", "7-24-22");</pre><p> 使用此列表和 Java8 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ function,我想返回一个 Map&lt;Chore, Long&gt;,其中包含家务 object 和事件。 因此,对于 ID 为“1”的杂项,长为 3。对于 ID 为“2”的杂项,长为 2……等等。</p><p> 我试图为此找到一个优雅的解决方案,但看起来每个人都可以得到如下内容:</p><pre> Map&lt;String, Long&gt; choresById = chores.stream().collect(Collectors.groupingBy(Chore::getId, Collectors.counting()));</pre><p> 我怎样才能想出 Map&lt;Chore, Long&gt; 而不是 Map&lt;String, Long&gt; (在这种情况下,字符串是 ID?)</p></div></object></object,> - How to use Java8 in an elegant way to return a map of <Object, Long> from a List<Object> counting occurrence?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用Java解析此文本的一种优雅方法是什么? - What's an elegant way to parse this text in java? 在Java中将一个列表映射到另一个列表的最优雅方法是什么? - What is the most elegant way to map one list to another in Java? 用最少的代码编写将Java对象序列化到Map(并解析回)的最快方法是什么? - What is the quickest way to serialize a Java object to Map (and parse back) with minimum code writing? 如何以优雅的方式使用 Java8 返回 map<object, long> 来自列表<object>计数发生?<div id="text_translate"><p> 我知道标题很拗口,所以我将尝试用一个例子来解释。</p><p> 我有一个清单。 Chores 有一个 id 字段作为标识符,但列表将包含许多家务,其中一些将具有相同的 ID。 我的目标是返回一个 Map&lt;Chore, Long&gt; ,其中 long 将计算杂务的出现次数。 例如:</p><pre> List&lt;Chore&gt; choreList = new ArrayList&lt;Chore&gt;(); choreList.add(new Chore("1", "vacuum", "7-6-22"); //Numbers 1,2,3 are the ID's for each chore choreList.add(new Chore("2", "dusting", "7-1-22"); choreList.add(new Chore("3", "mowing", "7-15-22"); choreList.add(new Chore("1", "vacuum", "7-14-22"); choreList.add(new Chore("1", "vacuum", "7-18-22"); choreList.add(new Chore("2", "dusting", "7-24-22");</pre><p> 使用此列表和 Java8 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ function,我想返回一个 Map&lt;Chore, Long&gt;,其中包含家务 object 和事件。 因此,对于 ID 为“1”的杂项,长为 3。对于 ID 为“2”的杂项,长为 2……等等。</p><p> 我试图为此找到一个优雅的解决方案,但看起来每个人都可以得到如下内容:</p><pre> Map&lt;String, Long&gt; choresById = chores.stream().collect(Collectors.groupingBy(Chore::getId, Collectors.counting()));</pre><p> 我怎样才能想出 Map&lt;Chore, Long&gt; 而不是 Map&lt;String, Long&gt; (在这种情况下,字符串是 ID?)</p></div></object></object,> - How to use Java8 in an elegant way to return a map of <Object, Long> from a List<Object> counting occurrence? 在Java中解析控制台命令的优雅方法 - Elegant way to parse console commands in java 使用Apache Commons Configuration 2.5从xml文件读取地图的最优雅方法是什么? - What is the most elegant way to read map from xml file using Apache Commons Configuration 2.5? 有没有一种优雅的方法可以根据用于创建该对象的构造函数来更改类的对象可以使用的方法/变量? - Is there an elegant way to change what methods/variables an object of a class can use based on the constructor used to create this object? 用枚举验证地图键集的优雅方法? - Elegant way to validate keyset of a map with an enum? 有没有一种优雅的方法可以将地图流减少到一个地图 - Is there an elegant way to reduce a stream of maps to one map 在Java 8中将映射连接到字符串的最优雅的方法 - Most elegant way to join a Map to a String in Java 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM