Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala可变多映射addBinding和插入顺序保持_Scala - Fatal编程技术网

Scala可变多映射addBinding和插入顺序保持

Scala可变多映射addBinding和插入顺序保持,scala,Scala,MultiMap的addBinding似乎不保留绑定到同一个键的值的插入顺序,因为它使用的底层机制是哈希集。使用多重映射保留插入顺序的惯用方法是什么?根据实现的状态: /** 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

MultiMap的addBinding似乎不保留绑定到同一个键的值的插入顺序,因为它使用的底层机制是哈希集。使用多重映射保留插入顺序的惯用方法是什么?

根据实现的状态:

/** 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]
}

一种方法可能是退回到常规映射(而不是多重映射),使用值类型的集合,而该集合将是可以强制执行顺序的集合类型(即,不是集合)。据我所知,为了在更广泛的意义上保留允许元素重复的插入顺序,自然使用的Scala集合将是一个Seq实现(例如,向量或队列,取决于访问模式)。

是否也需要覆盖其他方法?正在签出源代码。。。谢谢!:)不,无论实现是什么,实现都需要一个
集合