简体   繁体   English

列表的映射值<CustomObject>到具有属性的对象

[英]Map values of List<CustomObject> to an object with attributes

I have a class which has 2 attributes我有一个有 2 个属性的类

public class MyClass
{
     String name;
     String value;

     public MyClass(String name, String value)
     {
     this.name = name;
     this.value = value;
     }

}

I have another class which has many attributes我有另一个类有很多属性

public class MyAttributeClass
{
     String attribute1;
     String attribute2;
     String attribute3;
     String attribute4;
           .
           .
           .

}

Now I have a List which is something like below现在我有一个类似于下面的列表

List<MyClass> list = new ArrayList<>();
list.add(new MyClass("attribute1", "value1"));
list.add(new MyClass("attribute2", "value2"));
list.add(new MyClass("attribute3", "value3"));
list.add(new MyClass("attribute4", "value4"));

I have to populate the values of all 4 attributes of MyAttributeClass which I get from the list.我必须填充从列表中获得的 MyAttributeClass 的所有 4 个属性的值。 In the end, my MyAttributeClass should have something like最后,我的 MyAttributeClass 应该有类似的东西

public class MyAttributeClass
{
     String attribute1=value1;
     String attribute2=value2;
     String attribute3=value3;
     String attribute4=value4;

}

I can iterate over the list and create a map and then do a contains check and populate the object like below.我可以遍历列表并创建地图,然后进行包含检查并填充如下所示的对象。 Is there any better/efficient way to do this?有没有更好/有效的方法来做到这一点?

public void myMethod(List<MyClass> list)
{
        Map<String, String> mapresult = list.stream().collect(Collectors.toMap(MyClass::getName, MyClass::getValue));

        MyAttributeClass myAttributeClass = new MyAttributeClass();
        if (mapresult.containsKey("attribute1"))
        {
            myAttributeClass.setAttribute1(mapresult.get("attribute1"));
        }
        if (mapresult.containsKey("attribute2"))
        {
            myAttributeClass.setAttribute1(mapresult.get("attribute2"));
        }
        if (mapresult.containsKey("attribute3"))
        {
            myAttributeClass.setAttribute1(mapresult.get("attribute3"));
        }
        if (mapresult.containsKey("attribute4"))
        {
            myAttributeClass.setAttribute1(mapresult.get("attribute4"));
        }

    }

}

Is there any better/efficient way to do this in Kotlin/Java?在 Kotlin/Java 中有没有更好/更有效的方法来做到这一点?

   public static void main(String[] args) throws IllegalAccessException {
        List<MyClass> list = new ArrayList<>();
        list.add(new MyClass("attribute1", "value1"));
        list.add(new MyClass("attribute2", "value2"));
        list.add(new MyClass("attribute3", "value3"));
        list.add(new MyClass("attribute4", "value4"));

        Map<String, String> mapresult = list.stream().collect(Collectors.toMap(MyClass::getName, MyClass::getValue));

        MyAttributeClass attributeClass = new MyAttributeClass();
        for (Field field : attributeClass.getClass().getDeclaredFields()) {
           field.setAccessible(true);
           String  res = mapresult.get(field.getName());
           if (nonNull(res)) {
               field.set(attributeClass, res);
           }
        }
    }

Very simple example based on reflection基于反射的非常简单的例子

public class TestMap {

Map<String, Object> map = new HashMap<>();

public Attributes toAttributes() {
    Attributes attributes = new Attributes();
    for (Field field : attributes.getClass().getDeclaredFields()) {
        map.forEach((s, o) -> {
            if (field.getName().equals(s) && field.getType().equals(String.class)) {
                try {
                    field.set(attributes, o);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    return attributes;
}

public static void main(String[] args) {
    TestMap testMap = new TestMap();
    testMap.map.put("attribute1", "attribute1Value");
    testMap.map.put("attribute2", "attribute2Value");
    testMap.map.put("attribute3", "attribute3Value");
    testMap.map.put("attribute4", "attribute4Value");
    System.out.println(testMap.toAttributes().toString());
}


static class Attributes {
    String attribute1;
    String attribute2;
    String attribute3;
    String attribute4;

    @Override
    public String toString() {
        return "Attributes{" +
                "attribute1='" + attribute1 + '\'' +
                ", attribute2='" + attribute2 + '\'' +
                ", attribute3='" + attribute3 + '\'' +
                ", attribute4='" + attribute4 + '\'' +
                '}';
    }
}

} }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM