将Map <string,object =“”>转换为Map <string,list <object =“”>&gt; </string,> </string,>

时间:2013-11-26 15:13:40

标签: java list map

我正在尝试将Map值Object更改为List Value Object,以便单个键可以存储多个值。请找到代码段。

  protected Subscriber updateSubscriberAttributes(Subscriber subscriber, Collection<SubscriberAttributeDto> subscriberAttributesCollection)
        throws ErrorException {
    // Do we have any custom attributes to store?
    if ((subscriberAttributesCollection != null) && !subscriberAttributesCollection.isEmpty()) {
        // Yes! Convert to a Map first.
        Map<String, Object> subscriberAttributesMap = new HashMap<String, Object>(subscriberAttributesCollection.size());
        for (SubscriberAttributeDto subscriberAttribute : subscriberAttributesCollection) {
            // Convert the input attribute value to a database-appropriate value
            SubscriberAttributeMetadata attrMetadata = subscriberAttribute.getAttributeMetadata();
            if (attrMetadata != null) {
                subscriberAttributesMap.put(attrMetadata.getColumnName(),
                        attrMetadata.convertToDatabaseValue((String) subscriberAttribute.getValue()));
            }
        }

        // Perform the update
        return updateSubscriberAttributes(subscriber, subscriberAttributesMap);
    } else {
        return subscriber;
    }
}

对不起代码有点乱。所以我在这里遇到的麻烦是,如果我将subscribersAttributeMap的声明更改为Map<String,List<Object>>,我必须更改方法声明并给我带来如此多的错误。我很久就试过了。

3 个答案:

答案 0 :(得分:1)

可能是这样的:

 Map<String,List<Object>>subscriberAttributesMap = new HashMap<String, List<Object>>(subscriberAttributesCollection.size());
                for (SubscriberAttributeDto subscriberAttribute : subscriberAttributesCollection) {
                    // Convert the input attribute value to a database-appropriate value
                    SubscriberAttributeMetadata attrMetadata = subscriberAttribute.getAttributeMetadata();
                    if (attrMetadata != null) {
                        List<Object> lst = subscriberAttributesMap.get(attrMetadata.getColumnName());
                        if(lst == null){
                            lst = new ArrayList<Object>();
                            subscriberAttributesMap.put(attrMetadata.getColumnName(), lst);
                        }
                        lst.add(attrMetadata.convertToDatabaseValue((String) subscriberAttribute.getValue()));

                    }
                }

答案 1 :(得分:1)

protected Subscriber updateSubscriberAttributes(Subscriber subscriber, Collection<SubscriberAttributeDto> subscriberAttributesCollection) throws ErrorException {
    // Do we have any custom attributes to store?
    if ((subscriberAttributesCollection != null) && !subscriberAttributesCollection.isEmpty()) {
        // Yes! Convert to a Map first.
        Map<String, List<Object>> subscriberAttributesMap = new HashMap<String, List<Object>>(subscriberAttributesCollection.size());
        for (SubscriberAttributeDto subscriberAttribute : subscriberAttributesCollection) {
            // Convert the input attribute value to a database-appropriate value
            SubscriberAttributeMetadata attrMetadata = subscriberAttribute.getAttributeMetadata();
            if (attrMetadata != null) {
                //
                String columnName = attrMetadata.getColumnName();
                List<Object> list = subscriberAttributesMap.get(columnName);
                if (list == null) {
                    list = new ArrayList<Object>();
                    subscriberAttributesMap.put(columnName, list);
                }
                //
                list.add(attrMetadata.convertToDatabaseValue((String) subscriberAttribute.getValue()));
            }
        }

        // Perform the update
        return updateSubscriberAttributes(subscriber, subscriberAttributesMap);
    } else {
        return subscriber;
    }
}

protected Subscriber updateSubscriberAttributes(Subscriber subscriber,  Map<String, List<Object>> map) throws ErrorException {
    // do some logic hiere ...
    return new Subscriber();
}

答案 2 :(得分:0)

您可能希望使用/重新实现/复制Guava的Multimap,它具有您需要的功能和类似于java.util.Map的界面。因此,这将最小化代码中的更改。他们的文档示例:

ListMultimap<String, Object> multimap = ArrayListMultimap.create();

for (President pres : US_PRESIDENTS_IN_ORDER) {
    multimap.put(pres.firstName(), pres.lastName());
}

for (String firstName : multimap.keySet()) {
    List<Object> lastNames = multimap.get(firstName);
    out.println(firstName + ": " + lastNames);
}

Multimap可以转换回Map

// java.util.Map representation
Map<String,Collection<Object>> stringCollectionMap = multimap.asMap();