- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
鉴于以下 Scala 2.9.2 代码:
更新了非工作示例
import collection.immutable.SortedSet
case class Bar(s: String)
trait Foo {
val stuff: SortedSet[String]
def makeBars(bs: Map[String, String])
= stuff.map(k => Bar(bs.getOrElse(k, "-"))).toList
}
case class Bazz(rawStuff: List[String]) extends Foo {
val stuff = SortedSet(rawStuff: _*)
}
// test it out....
val b = Bazz(List("A","B","C"))
b.makeBars(Map("A"->"1","B"->"2","C"->"3"))
// List[Bar] = List(Bar(1), Bar(2), Bar(3))
// Looks good?
// Make a really big list not in order. This is why we pass it to a SortedSet...
val data = Stream.continually(util.Random.shuffle(List("A","B","C","D","E","F"))).take(100).toList
val b2 = Bazz(data.flatten)
// And how about a sparse map...?
val bs = util.Random.shuffle(Map("A" -> "1", "B" -> "2", "E" -> "5").toList).toMap
b2.makeBars(bs)
// res24: List[Bar] = List(Bar(1), Bar(2), Bar(-), Bar(5))
makeBars
扩展类的方法
Foo
不返回排序列表。实际上,列表排序并不反射(reflect)
SortedSet
的排序。
SortedSet
到
List
元素按
SortedSet
排序订购?
最佳答案
您对隐式解析感到惊讶。map
方法需要一个 CanBuildFrom
与目标集合类型(在简单情况下,与源集合类型相同)和映射器函数的返回类型兼容的实例。
在 SortedSet
的特殊情况下,其隐含 CanBuildFrom
需要一个 Ordering[A]
(其中 A 是映射器函数的返回类型)可用。当您的 map 函数返回编译器已经知道如何查找 Ordering
的内容时因为,你很好:
scala> val ss = collection.immutable.SortedSet(10,9,8,7,6,5,4,3,2,1)
ss: scala.collection.immutable.SortedSet[Int] = TreeSet(1, 2, 3, 4, 5,
6, 7, 8, 9, 10)
scala> val result1 = ss.map(_ * 2)
result1: scala.collection.immutable.SortedSet[Int] = TreeSet(2, 4, 6, 8, 10,
12, 14, 16, 18, 20)
// still sorted because Ordering[Int] is readily available
scala> val result2 = ss.map(_ + " is a number")
result2: scala.collection.immutable.SortedSet[String] = TreeSet(1 is a number,
10 is a number,
2 is a number,
3 is a number,
4 is a number,
5 is a number,
6 is a number,
7 is a number,
8 is a number,
9 is a number)
// The default Ordering[String] is an "asciibetical" sort,
// so 10 comes between 1 and 2. :)
SortedSet
上的隐式不匹配(具体来说,找不到其隐式参数的值),因此编译器看起来“向上”以寻找兼容的
CanBuildFrom
并从
Set
中找到通用的.
scala> case class Foo(i: Int)
defined class Foo
scala> val result3 = ss.map(Foo(_))
result3: scala.collection.immutable.Set[Foo] = Set(Foo(10), Foo(4), Foo(6), Foo(7), Foo(1), Foo(3), Foo(5), Foo(8), Foo(9), Foo(2))
// The default Set is a hash set, therefore ordering is not preserved
Ordering[Foo]
的实例来解决这个问题。它可以满足您的期望:
scala> implicit val fooIsOrdered: Ordering[Foo] = Ordering.by(_.i)
fooIsOrdered: Ordering[Foo] = scala.math.Ordering$$anon$9@7512dbf2
scala> val result4 = ss.map(Foo(_))
result4: scala.collection.immutable.SortedSet[Foo] = TreeSet(Foo(1), Foo(2),
Foo(3), Foo(4), Foo(5),
Foo(6), Foo(7), Foo(8),
Foo(9), Foo(10))
// And we're back!
关于scala - SortedSet 映射并不总是保留结果中的元素排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19528357/
有人能解释一下为什么这段代码编译并运行良好,尽管 SortedSet是接口(interface)而不是具体类: public static void main(String[] args) {
鉴于以下 Scala 2.9.2 代码: 更新了非工作示例 import collection.immutable.SortedSet case class Bar(s: String) trait
我有一个对象 Test,它有两个属性:double x 和 double y。我想将这些对象添加到 SortedSet 中,使集合在 Test 的 x 上按 ASC 顺序排序。如果 Test 的两个实
我正在考虑用 SortedSet 替换 HashSet,因为它更适合我存储的数据。 但是,到目前为止我看到的所有示例都与存储简单对象有关 - int、字符串等。 我想为具有许多属性的自定义类实现此功能
我想知道如何更改 SortedSet 确定两个对象是否相等的方式。 我有SortedSet>(new Helpers.EdgeDistanceComparer())比较器方法是: public cla
我有一个类的实例,我想按特定顺序对其进行排序,但也能够使用不同的标准判断一个实例是否存在于集合中。示例: public class Foo { int x; int y; pu
我需要将 sourceList 中的对象添加到一个集合中,该集合在我们将对象添加到集合中时对集合进行排序。我正在考虑使用TreeSet . TreeSet bookSet 根据某些条件,我需要获取bo
尝试如下所示编写我的类会出现编译错误 public class CustomTreeSet> implements SortedSet> { } 错误: Syntax error on token "
以下代码创建了一个排序集,它按其值而不是键进行排序。 vertexRank 是一个对象,负责获取值。一切正常,除了代码:vertexCentralities.addAll(vMap.entrySet(
如 SortedSet 中所述 Represents a collection of objects that is maintained in sorted order SortedSet 中的“有
来自 SortedSet文档: several methods return subsets with restricted ranges. Such ranges are half-open, th
假设一个应用程序产生了一系列 HashMap数据结构,每个包含几十到几百个Comparable MyClass 类型的对象,需要以单个形式结束并排序 Collection . 此功能的两个可能实现返回
我试图使用排序集解决运行中值问题(在 hackerrank 上)。只有它的元素没有正确排序。 在此处查看实际效果:http://rextester.com/NGBN25779 public class
我想使用一个排序的集合,但我可以通过索引访问其中的元素,即我想要一些同时具有 Set 和 List 特征的东西。 Java.util.TreeSet 非常接近我的需要,但不允许通过索引访问。 我可以想
考虑这个类 public class A { float Order string Name public A(float order, string name)
在 SortedSet 的 Julia 文档中,有一个对“排序对象”的引用,可以在构造函数中使用。我正在做一个项目,我需要在一组结构上实现自定义排序。我想为此使用仿函数,因为我需要额外的状态来进行比较
我正在尝试解决 LeetCode https://leetcode.com/problems/nth-magical-number/ 的问题。我可以提交我的解决方案,但我想加快速度,我想这样做的方法之
这更多的是出于好奇,因为我从未注意到性能问题。假定设置大小介于 1-1000 之间。这是一个案例: private static SortedSet sortAuthorities( fina
在 SortedSet 的 Julia 文档中,有一个对“排序对象”的引用,可以在构造函数中使用。我正在做一个项目,我需要在一组结构上实现自定义排序。我想为此使用仿函数,因为我需要额外的状态来进行比较
我正在开发一个文字游戏,它的方法之一是打乱方法,它应该采用 String s,然后使用 Collections.shuffle(ListOfSChars); 对其进行洗牌;并检查 scramble 是
我是一名优秀的程序员,十分优秀!