是否有Scala替代Guava MultiSet和Table概念? [英] Is there a scala replacement for Guava MultiSet and Table concepts?
本文介绍了是否有Scala替代Guava MultiSet和Table概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要在Scala中使用番石榴Table
和Multiset
吗?在scala中已经有不同的概念,而不是为此目的导入番石榴库吗?
To use guava Table
and Multiset
in scala? are there already different concenpts in scala instead of importing guava library for this usage?
推荐答案
您可以使用Map[(R, C), V]
代替Table<R, C, V>
,使用Map[T, Int]
代替Multiset<T>
.您还可以像这样将辅助方法添加到Map[T, Int]
:
You could use Map[(R, C), V]
instead of Table<R, C, V>
and Map[T, Int]
instead of Multiset<T>
. You could also add helper methods to Map[T, Int]
like this:
implicit class Multiset[T](val m: Map[T, Int]) extends AnyVal {
def setAdd(e: T, i: Int = 1) = {
val cnt = m.getOrElse(e, 0) + i
if (cnt <= 0) m - e
else m.updated(e, cnt)
}
def setRemove(e: T, i: Int = 1) = setAdd(e, -i)
def count(e: T) = m.getOrElse(e, 0)
}
val m = Map('a -> 5)
m setAdd 'a
// Map('a -> 6)
m setAdd 'b
// Map('a -> 5, 'b -> 1)
m setAdd ('b, 10)
// Map('a -> 5, 'b -> 10)
m setRemove 'a
// Map('a -> 4)
m setRemove ('a, 6)
// Map()
m count 'b
// 0
(m setAdd 'a) count 'a
// 6
这篇关于是否有Scala替代Guava MultiSet和Table概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文