根据另一个类中可用的参数对列表进行排序 [英] sort a List based on a parameter available in another class
问题描述
我在使用Java 比较器
接口时遇到一些设计问题。
I have some design problems with Java Comparator
Interface.
我有一个包含<$的类c $ c>设置一个简单的自定义数据结构:
I have a class which contains a Set
of a simple custom data structure:
class data {
Long ID;
int Priority;
...
}
ID
是唯一的,因此可以使用 ID
来获取全部数据。
ID
s are unique, so it is possible to get the whole data using ID
.
和容器类:
class Container {
Set<data> mySet = ...;
List<Long> myList = ...;
...
}
出于某些不可避免的原因,我需要保持排序的数据
ID的排序后的列表
。我需要列表
要按 Priority
排序。
for some inevitable reasons, I need to keep a sorted List
of data
IDs in parallel. I need the List
to be sorted by Priority
.
因为,比较器
应该比较优先级
应该实现 Comparator< int>
。但是 List
仅包含 ID
s和 Priority
s不能直接使用。
Since, the Comparator
should compare Priority
s it should implements Comparator<int>
. But the List
only contains ID
s and the Priority
s are not available directly.
这是问题所在。 列表
中只有 ID
。因此,Comparator类无法访问 Priority
。
This is the problem. There is only ID
in the List
. Therefore, the Comparator class has no access to Priority
.
如何设计这种概念?
推荐答案
您可以使用闻起来像高阶函数的东西。也就是说,创建一个静态函数,该函数接受从Long到int(这是优先级)或数据的排序映射,并返回一个新的Comparator。
You could use something that smells like higher order functions. That is, make a static function that takes a map of sorts from Long to int (which is the priority) or data and returns a new Comparator.
Foo类具有静态方法 getComparator
,该方法需要使用Orange。 Orange是具有方法 getPriority
的类,该方法获取ID并返回相应的优先级。 getComparator
方法构造一个新的 Comparator
对象。新的 Comparator
对象的 compare
方法具有两个ID。它会查找两个ID的相应优先级并进行比较。
The class Foo has a static method getComparator
which takes an Orange. An Orange is a class that has a method getPriority
which takes an ID an return the corresponding priority. The getComparator
method constructs a new Comparator
object. The new Comparator
object's compare
method takes two IDs. It looks up the corresponding priorities of the two IDs and compares them.
public interface Orange {
// Looks up id and returns the corresponding Priority.
public int getPriority(Long id);
}
public class Foo {
public static Comparator<Long> getComparator(final Orange orange) {
return new Comparator<Long>() {
public int compare(Long id1, Long id2) {
// Get priority through orange, or
// Make orange juice from our orange.
// You may want to compare them in a different way.
return orange.getPriority(id1) - orange.getPriority(id2);
};
}
}
我的Java有点生锈,所以代码可能是有缺陷的。
My java is a bit rusty so the code may be flawed. The general idea should work, though.
用法:
// This is defined somewhere. It could be a local variable or an instance
// field or whatever. There's no exception (except is has to be in scope).
Collection c = ...;
...
Orange orange = new Orange() {
public int getPriority(Long id) {
// Insert code that searches c.mySet for an instance of data
// with the desired ID and return its Priority
}
};
Collections.sort(c.myList, Foo.getComparator(orange));
我没有举一个橘子看起来的例子。
I have not given an example on how an Orange could look.
这篇关于根据另一个类中可用的参数对列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!