2014-09-26 83 views

回答

6

基於MultiMap裏執行狀態:

/** Creates a new set. 
* 
* Classes that use this trait as a mixin can override this method 
* to have the desired implementation of sets assigned to new keys. 
* By default this is `HashSet`. 
* 
* @return An empty set of values of type `B`. 
*/ 
protected def makeSet: Set[B] = new HashSet[B] 

你可以簡單的定義:

trait OrderedMultimap[A, B] extends MultiMap[A, B] { 
    override def makeSet: Set[B] = new LinkedHashSet[B] 
} 
+0

是否還需要重寫其他方法?檢查出源代碼......謝謝! :) – matanster 2014-09-26 20:46:52

+0

不,實現需要一個''Set'',無論實現是什麼 – 2014-09-26 20:47:43

0

一種方式很可能會回落到正規的地圖(不multimap)來說,使用集合的值類型,而該集合將是一個集合類型,可以強制執行訂單(即不是集合)。據我所知,爲了保持允許元素重複的廣義插入順序,要使用的自然Scala集合將是一個Seq實現(例如Vector或Queue,取決於訪問模式)。