Scala中缺少多集吗? [英] Are Multisets missing in Scala?

查看:114
本文介绍了Scala中缺少多集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Scala尝试了2013年Facebook黑客杯资格考试,而对于第三个问题,我觉得需要订购的Multiset,但是在scala的(2.10)收藏集中找不到一个. scala的集合中是否缺少此数据结构?它将在将来的版本中实现吗?如果您已经实现了多集,则真的不需要多集吗?

解决方案

多集是一种非常特殊且不常见的数据结构.例如,它也不是Java标准库的一部分.番石榴确实有一个,Boost也有,但是Boost基本上都有.

如果只想计算元素的出现次数,则可以求一个SortedMap从元素开始计数.另一方面,如果您要求元素在排序规则下是不同的,可检索的但等效的,则可以使用SortedMap从元素(不重要的哪一个)到Set区分元素.

I was trying the Facebook Hacker Cup 2013 Qualification Problems in Scala, and for the 3rd problem I felt the need of an ordered Multiset but could not find one in scala's (2.10) collections. Is this data structure missing in scala's collections. Is it going to be implemented in a future version? Is the Multiset not really necessary if you have already a set implemented?

解决方案

A multiset is a rather peculiar and uncommon data structure. It is not, for instance, part of Java's standard library either. Guava does have one, and so does Boost, but Boost has basically everything.

If all you want is to count the number of occurrences of the elements, you could resort to a SortedMap from element to count instead. If you require, on the other hand, for the elements to be distinct, retrievable, but equivalent under sorting rules, you could use a SortedMap from element (not important which one) to a Set of distinguished elements.

这篇关于Scala中缺少多集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆