我正在尝试将“映射值”对象更改为“列表值”对象,以便单个键可以存储多个值。请找到代码片段。

  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>>,则必须更改方法的声明并抛出很多错误。我从很久以来就尝试过。

最佳答案

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

07-25 22:44