gpt4 book ai didi

java - 将 Map 转换为 Map>

转载 作者:行者123 更新时间:2023-12-02 06:29:21 24 4
gpt4 key购买 nike

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

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

关于java - 将 Map<String, Object> 转换为 Map<String, List<Object>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20220888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com