簡體   English   中英

列表的映射值<CustomObject>到具有屬性的對象

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

我有一個有 2 個屬性的類

public class MyClass
{
     String name;
     String value;

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

}

我有另一個類有很多屬性

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

}

現在我有一個類似於下面的列表

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"));

我必須填充從列表中獲得的 MyAttributeClass 的所有 4 個屬性的值。 最后,我的 MyAttributeClass 應該有類似的東西

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

}

我可以遍歷列表並創建地圖,然后進行包含檢查並填充如下所示的對象。 有沒有更好/有效的方法來做到這一點?

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"));
        }

    }

}

在 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);
           }
        }
    }

基於反射的非常簡單的例子

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