I am trying to change the Map value Object to List Value Object, so that a single key can store multiple values. Please find the code snippet.
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;
}
}
Sorry the code is little messy. So the trouble I am facing here is, If I change the declaration of subscribersAttributeMap
to Map<String,List<Object>>
I have to change the method declaration and throwing me so many errors. I did try it from long time.
probably something like:
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()));
}
}
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();
}
You may want using/re-implementing/copying Guava's Multimap , which has the functionality you need and the interface similar to java.util.Map
. So this would minimize changes in the code. Example from their documentation:
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 can be converted back to a Map
:
// java.util.Map representation
Map<String,Collection<Object>> stringCollectionMap = multimap.asMap();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.