- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个对象 Test
,它有两个属性:double x
和 double y
。我想将这些对象添加到 SortedSet
中,使集合在 Test
的 x 上按 ASC 顺序排序。如果 Test
的两个实例具有相同的 x 值,我希望它们在集合中按 y 值排序。
我认为以下内容可以解决问题:
private SortedSet<Test> tests = new TreeSet<Test>(new Comparator<Test>() {
@Override
public int compare(Test o1, Test o2) {
if (o1.getXpos() < o2.getXpos()) {
return -1;
}
if (o1.getXpos() > o2.getXpos()) {
return 1;
}
if (o1.getXpos() == o2.getXpos()) {
if (o1.getYpos() < o2.getYpos()) {
return -1;
}
if (o1.getYpos() > o2.getYpos()) {
return 1;
}
if (o1.getYpos() == o2.getYpos()) {
return 0;
}
}
return 0;
}
});
相反,这对实际的 x 和 y 值进行排序;即
testA: x=200, y=200,
testB: x=200, y=400
插入测试
后:
testA: x=200, y=200,
testB: x=400, y=200
而不是测试
中的实例。
最佳答案
你的比较器是正确的。但是,如果将测试对象添加到集合中会更改其成员变量,例如,您会遇到更大的问题。 “测试B:x = 400,y = 200” ->“测试B:x = 200,y = 400”。我猜你的问题在于你没有包含的代码(也许是一个拙劣的构造函数?)。
关于java - 根据属性保留对象的 SortedSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9797079/
有人能解释一下为什么这段代码编译并运行良好,尽管 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 是
我是一名优秀的程序员,十分优秀!