我需要实现Map,因为普通的hashmap不允许多个键值对。
def createRecommendationMap(List recommendation, Map recommendationMap)
{
if(!(allMaps.containsKey(RECOMMENDATION_MAP)))
{
recommendation.each{record ->
def javaRecord = KdTypeUtil.convertRecordFromKDTypes(record)
def key = // returns just a string
recommendationMap.put(key, javaRecord)
}
allMaps.put(RECOMMENDATION_MAP, recommendationMap)
}
return recommendationMap;
}
我在这里想念的是什么?
我希望大小为9,但只能获得8的大小(删除重复的键值)
我期待以下两个键值对,但第二个被删除
“NONMEDIAOUTBOUND24.0P10”:[“CHANNEL”:“OUTBOUND”,“STOCK_LEVEL”:0.0, “PRODUCT_ID”: “SAM-STD-I9100W-1”, “OFFER_ID”:“FBUN-MVP-VME-VIRGIN-29-12-05”,“P_BAND”:“P10”, “CONTRACT_LENGTH”:24.0,“INCENTIVE_POINTS”:15.0, “CUST_TYPE”:“NONMEDIA”],
“NONMEDIAOUTBOUND24.0P10”:[“CHANNEL”:“OUTBOUND”,“STOCK_LEVEL”:0.0, “PRODUCT_ID”: “LGE-STD-E960-1” “OFFER_ID”:“FBUN-MVP-VME-VIRGIN-29-12-05”,“P_BAND”:“P10”, “CONTRACT_LENGTH”:24.0,“INCENTIVE_POINTS”:10.0, “CUST_TYPE”: “非媒介”],
答案 0 :(得分:0)
看起来你正在重新实现MultiMap。 Apache Commons和Guava都有广泛使用且经过全面测试的实现。
答案 1 :(得分:0)
非常确定您可以用以下代码替换所有代码:
Map createRecommendationMap( List recommendation ) {
println 'inside createRecommendationMap of Maps **************** '
//If not already created, create the map. Stick to KD types, not many
//fields required.
//Keyed on Tariff type desc, commitment duration, p_band.
if( !allMaps.containsKey(RECOMMENDATION_MAP) ) {
allMaps[ RECOMMENDATION_MAP ] = recommendation.inject( [:] ) { map, r ->
def k = "${r.CUST_TYPE}${r.CHANNEL}${r.CONTRACT_LENGTH}${r.P_BAND}".toString()
def m = map[ k ]
if( !m ) {
m = []
map[ k ] = m
}
m << r
map
}
}
allMaps[ RECOMMENDATION_MAP ]
}
我在Groovy 1.5.6中运行的完整测试应用程序是:
class Test {
Map allMaps = [:]
String RECOMMENDATION_MAP = 'SOME_KEY'
Map createRecommendationMap( List recommendation ) {
println 'inside createRecommendationMap of Maps **************** '
//If not already created, create the map. Stick to KD types, not many
//fields required.
//Keyed on Tariff type desc, commitment duration, p_band.
if( !allMaps.containsKey(RECOMMENDATION_MAP) ) {
allMaps[ RECOMMENDATION_MAP ] = recommendation.inject( [:] ) { map, r ->
def k = "${r.CUST_TYPE}${r.CHANNEL}${r.CONTRACT_LENGTH}${r.P_BAND}".toString()
def m = map[ k ]
if( !m ) {
m = []
map[ k ] = m
}
m << r
map
}
}
allMaps[ RECOMMENDATION_MAP ]
}
}
def recommendation = [ ["CHANNEL":"OUTBOUND", "STOCK_LEVEL":0.0, "PRODUCT_ID":"SAM-STD-I9100W-1", "OFFER_ID":"FBUN-MVP-VME-VIRGIN-29-12-05", "P_BAND":"P10", "CONTRACT_LENGTH":24.0, "INCENTIVE_POINTS":15.0, "CUST_TYPE":"NONMEDIA"],
["CHANNEL":"OUTBOUND", "STOCK_LEVEL":0.0, "PRODUCT_ID":"LGE-STD-E960-1", "OFFER_ID":"FBUN-MVP-VME-VIRGIN-29-12-05", "P_BAND":"P10", "CONTRACT_LENGTH":24.0, "INCENTIVE_POINTS":10.0, "CUST_TYPE":"NONMEDIA"] ]
def recommendationMap = new Test().createRecommendationMap( recommendation )
recommendationMap.each { key, value ->
println key
value.each { println " $it" }
}
打印:
NONMEDIAOUTBOUND24.0P10
["CHANNEL":"OUTBOUND", "STOCK_LEVEL":0.0, "PRODUCT_ID":"SAM-STD-I9100W-1", "OFFER_ID":"FBUN-MVP-VME-VIRGIN-29-12-05", "P_BAND":"P10", "CONTRACT_LENGTH":24.0, "INCENTIVE_POINTS":15.0, "CUST_TYPE":"NONMEDIA"]
["CHANNEL":"OUTBOUND", "STOCK_LEVEL":0.0, "PRODUCT_ID":"LGE-STD-E960-1", "OFFER_ID":"FBUN-MVP-VME-VIRGIN-29-12-05", "P_BAND":"P10", "CONTRACT_LENGTH":24.0, "INCENTIVE_POINTS":10.0, "CUST_TYPE":"NONMEDIA"]