将列表中对象的属性值设置为等于Java中Map中的字符串值

时间:2016-01-29 18:54:40

标签: java list dictionary

所以我有一张看起来像这样的地图:

Map<String, String> properties = {"item0.label": "item0LabelValue", "item0.value":"item0Value", "item0.foregroundColor":"-1", "item0.backgroundColor":"-16738048",
"item1.label": "item1LabelValue", "item1.value":"item1Value", "item1.foregroundColor":"-1", "item1.backgroundColor":"-16738048",
"item2.label": "item2LabelValue", "item2.value":"item2Value", "item2.foregroundColor":"-1", "item2.backgroundColor":"-16738048"};

键和值都是字符串。换句话说,对于这张地图:

"item1.label" is a string, not an attribute of an object (essentially poor naming convention)

列表的大小不一定相同。现在我需要做的是将所有这些字符串转移到包含与.label,.value,.foregroundColor,.backgroundColor相对应的属性的对象列表中。

在这种情况下,我会有一个清单:

List<Object> objects = Arrays.asList(object0, object1, object2);

我需要这些对象的值与地图中的字符串匹配,所以:

object0.label = "item0LabelValue";
object0.value = "item0Value";
object0.foregroundColor = "-1";
object0.backgroundColor = "-16738048";
object1.label = "item1LabelValue";
...

需要注意的是,并非所有属性都必须给出。换句话说,如果没有给出字符串“item0.value”,那么object0.value =“item0.labelValue”

这样做有干净的方法吗?我正在考虑(这看起来很丑陋)这样做的方法是为每个包含“item0。”的元素创建一个列表,为包含“item1。”等的每个项创建一个列表,然后循环遍历每个列表到设置相应对象的值。不过,这似乎是一种糟糕的做法。

环境: Java 8, Windows 7

3 个答案:

答案 0 :(得分:0)

您需要创建一个对象来保存您的属性。该对象将允许您定义一个构造函数,您可以在其中为null或空值定义规则。与equals和hashCode方法一起使用。这里有一些示例代码可以帮助您实现目标

List<PropOject> propObjects = new ArrayList<>();

public void createObjects() {
    propObjects.add(new PropOject("label1", "value1"));
    propObjects.add(new PropOject("label2", null));
}


public class PropOject {
    private String label;
    private String value;


    public PropOject(String label, String value) {
        this.label = label;
        this.value = value != null ? value : label;
    }


    public String getLabel() {
        return label;
    }


    public void setLabel(String label) {
        this.label = label;
    }


    public String getValue() {
        return value;
    }


    public void setValue(String value) {
        this.value = value;
    }
}

答案 1 :(得分:0)

 HashMap<Integer,MyObject> mapObjects = new HashMap<Integer,MyObject>();
 for (String key: properties.keys()){
      String[] splits = key.split('.');
      int id = Integer.valueOf(splits[0].substring(4, splits[0].length()));
      if (mapObjects.containsKey(id)){
          mapObjects.get(id).set(splits[1],properties.get(key));
      }
      else{
          MyObject myObject = new MyObject();
          myObject.set(splits[1],proterties.get(key));
          mapObjects.put(id,myObject);
      }
 }

 //Convert to list if necesseray
 List<MyObject> listObjects = new ArrayList<MyObject>(mapObjects.values());

 //MyObject contains a method set(String fieldStr, String value) which based on fieldStr sets the correct field to value (or use reflection) 

答案 2 :(得分:0)

这真的不应该这样做吗?这将要求班级有像denov发布的设置器,我就是在那里创建对象。

int i = 0;
while (i < properties.size()) {
    MyObject object = new MyObject();
    object.setLabel(properties.containsKey("item" + i + ".label") ? properties.get("item" + i + ".label") : "someDefault");
    object.setValue(properties.containsKey("item" + i + ".value") ? properties.get("item" + i + ".value") : properties.get("item" + i + ".label"));
    object.setForegroundColor(properties.containsKey("item" + i + ".foregroundColor") ? properties.get("item" + i + ".foregroundColor") : DEFAULT_FOREGROUND_COLOR);
    object.setBackgroundColor(properties.containsKey("item" + i + ".backgroundColor") ? properties.get("item" + i + ".backgroundColor") : DEFAULT_BACKGROUND_COLOR);
    sendObjectElsewhere(object);
    i++;
}
相关问题