- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在寻找一种在 Java 中使用多个字段进行加权排序的有效实现。这个问题有点类似于 How to provide most relevant results with Multiple Factor Weighted Sorting和 Need help maximizing 3 factors in multiple, similar objects and ordering appropriately .但是,我要求提供一些有关有效实现的指南。
在下面的例子中,Person
类有 age
和 income
字段,我想对数组列表 persons 进行排序
具有较低的 age
和较高的 income
组合,基于给定的偏好并按降序排列。我为 age
和 income
提供了相同的偏好。偏好的总和应为 1。
正如您在这个简单的实现中看到的那样,有太多循环需要迭代,最终运行大量输入的成本太高。我还探索了 Guava 的 ComparisonChain 和 Apache Commons CompareToBuilder,但它们似乎没有实现我的目标。
package main.java.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SortingTest {
static double income_preference = 0.5;
static double age_preference = 1 - income_preference;
public static void main(String args[]) {
ArrayList<Person> persons = new ArrayList<Person>();
persons.add(new Person("A", 60, 55.0));
persons.add(new Person("B", 45, 50.0));
persons.add(new Person("C", 20, 50.0));
persons.add(new Person("D", 55, 60.0));
persons.add(new Person("E", 30, 85.0));
// Sort the array list by income (descending order)
Collections.sort(persons, new Comparator<Person>(){
@Override
public int compare(Person p1, Person p2) {
return (((double)p1.income > (double)p2.income) ? -1 :
((double)p1.income < (double)p2.income) ? 1 : 0);
}
});
// Rank based on income
int income_rank = persons.size();
for(int i = 0; i < persons.size(); i++) {
if(i != 0)
if(persons.get(i).income != persons.get(i-1).income)
--income_rank;
persons.get(i).income_rank = income_rank * income_preference;
}
System.out.println("List of persons sorted by their income in descending order: ");
for(Person p : persons)
System.out.println(p.toString());
// Sort the array list by age (ascending order)
Collections.sort(persons, new Comparator<Person>(){
@Override
public int compare(Person p1, Person p2) {
return (((double)p2.age > (double)p1.age) ? -1 :
((double)p2.age < (double)p1.age) ? 1 : 0);
}
});
// Rank based on age
int age_rank = persons.size();
for(int i = 0; i < persons.size(); i++) {
if(i != 0)
if(persons.get(i).age != persons.get(i-1).age)
--age_rank;
persons.get(i).age_rank = age_rank * age_preference;
}
System.out.println();
System.out.println("List of persons sorted by their age in ascending order: ");
for(Person p : persons)
System.out.println(p.toString());
// Assign combined rank
for(Person p : persons)
p.combined_rank = (p.age_rank + p.income_rank);
// Sort the array list by the value of combined rank (descending order)
Collections.sort(persons, new Comparator<Person>(){
@Override
public int compare(Person p1, Person p2) {
return (((double)p1.combined_rank > (double)p2.combined_rank) ? -1 :
((double)p1.combined_rank < (double)p2.combined_rank) ? 1 : 0);
}
});
System.out.println();
System.out.println("List of persons sorted by their combined ranking preference in descending order: ");
for(Person p : persons)
System.out.println(p.toString());
}
}
class Person {
String name;
int age; // lower is better
double income; // higher is better
double age_rank;
double income_rank;
double combined_rank;
public Person(String name, int age, double income) {
this.name = name;
this.age = age;
this.income = income;
this.age_rank = 0.0;
this.income_rank = 0.0;
this.combined_rank = 0.0;
}
@Override
public String toString() {
return ("Person-"+this.name+", age("+this.age+"|"+this.age_rank+"th), income("+this.income+"|"+this.income_rank+"th), Combined Rank("+this.combined_rank+")");
}
}
控制台输出
按收入降序排列的人员名单:
人-E,年龄(30|0.0),收入(85.0|2.5),综合排名(0.0)
D人,年龄(55|0.0),收入(60.0|2.0),综合排名(0.0)
人-A,年龄(60|0.0),收入(55.0|1.5),综合排名(0.0)
B人,年龄(45|0.0),收入(50.0|1.0),综合排名(0.0)
Person-C,年龄(20|0.0th),收入(50.0|1.0th),综合排名(0.0)
按年龄升序排列的人员列表:
Person-C,年龄(20|2.5th),收入(50.0|1.0th),综合排名(0.0)
人-E,年龄(30|2.0),收入(85.0|2.5),综合排名(0.0)
人-B,年龄(45|1.5th),收入(50.0|1.0th),综合排名(0.0)
D人,年龄(55|1.0),收入(60.0|2.0),综合排名(0.0)
人-A,年龄(60|0.5th),收入(55.0|1.5th),综合排名(0.0)
按组合排名偏好降序排列的人员列表:
人-E,年龄(30|2.0),收入(85.0|2.5),综合排名(4.5)
Person-C,年龄(20|2.5th),收入(50.0|1.0th),综合排名(3.5)
D人,年龄(55|1.0),收入(60.0|2.0),综合排名(3)
B人,年龄(45|1.5th),收入(50.0|1.0th),综合排名(2.5)
人-A,年龄(60|0.5th),收入(55.0|1.5th),综合排名(2.5)
最佳答案
你可以维护两个TreeSet用于分别存储年龄和收入信息,因此您可以在排序时轻松地从这两棵树中查询年龄和收入的排名。
我们可以从 TreeSet 中调用 tailSet(int)
方法来获取大于或等于特定数字的数字列表,在这种情况下,它将是年龄/收入的排名。
TreeSet ageRank = new TreeSet();
TreeSet incomeRank = new TreeSet();
for(Person p : persons){
ageRank.add(p.getAge());
incomeRank.add(p.getIncome());
}
Collections.sort(persons, new Comparator<Person>(){
@Override
public int compare(Person p1, Person p2) {
int ageRank1 = ageRank.tailSet(p1.getAge()).size();
int ageRank2 = ageRank.tailSet(p2.getAge()).size();
int incomeRank1 = incomeRank.tailSet(p1.getIncome()).size();
int incomeRank2 = incomeRank.tailSet(p2.getIncome()).size();
//Calculate the combined_rank and return result here. Code omitted
}
});
采用这种方法,一个排序一个for循环就足以完成所有计算。
如果您需要定期更新人员名单,这种方法会派上用场,因为您不需要对年龄和收入进行排序,也不需要在出现变化时一次又一次地重新计算所有排名更新,你只需要更新这两棵树。
注意:为了在用于排序的内部Comparator
类中使用ageRank
和incomeRank
,他们需要声明为最终变量或实例变量。
关于java - Java多因子加权排序的高效实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30449023/
我有一个包含三个字段的表:ID、值、计数 ID和Value构成PK。 给定一个 ID,我想选择一个按计数加权的值,然后将计数减一。 如果我有 1 A 2 1 B 3 我应该有 2/5 的机会获
我有一个数据集,其中开始日期和日期没有特定的顺序。我想创建一组新的几个月列和数据的加权平均值。 del 代表否。日期范围内的天数 d 是该时间段的平均值 from datetime import da
我正在开发的一个软件应用程序需要能够根据用户当前拥有的任务数量将任务分配给一组用户,其中任务最少的用户最有可能获得下一个任务。然而,当前的任务负载应该被视为一个权重,而不是一个绝对的顺序定义。 IOW
我正在尝试用字典创建一个光学字符识别系统。 事实上,我还没有实现字典=) 我听说有一些基于 Levenstein 距离的简单指标,这些指标考虑了不同符号之间的不同距离。例如。 'N' 和 'H' 彼此
三个AI新手问题: 为什么 A* 可以采用启发式算法来找到最佳路径? 如果障碍物挡住了道路,系带制动技术有什么用? 什么算法适合在有障碍物的网格上找到路径? (像吃 bean 一样) 第一个问题让我们
我有一个 2396x34 double matrix命名 y其中每一行 (2396) 代表一个单独的情况,由 34 个连续的时间段组成。 我也有 numeric[34]命名 x这代表了 34 个连续时
我有一个如下所示的多维数组,我想做一些奇特的排序,但我不知道如何处理它。我想首先按第 5 个元素对数组元素进行排序,然后是第 4 个元素,然后是第 3 个元素,然后是第 2 个元素,然后是第 1 个元
我想对具有三个变量(列)的数据集(即 Sample_Data)进行 Kmeans 聚类,如下所示: A B C 1 12 10 1 2 8 11 2 3 14 10
我环顾四周,发现了一些与我类似的问题,但它们缺乏解释。 我正在尝试搜索包含多列的表格。我希望匹配列数最多的行位于顶部,匹配列数最少的行位于底部。我见过几种方法。我目前的糟糕方式是使用大量 MySQL
我必须大量使用加权概率分布,并且想使用 violinplots 进行一些可视化。但是,我找不到在任何常见嫌疑人(matplotlib、seaborn、bokeh 等)中使用加权数据创建这些数据的方法。
我会尽量做到彻底: 我有 11 个小组。 我有很多人需要在这些组之间进行划分 每个人都有一个加权偏好列表。通常在该列表上有 3 个有序的组,但一些异常值会有更多或更少的组。 IE:人 1 有进入 A
我有 100 个顶点和一个计算顶点 x 和顶点 y 之间边的权重的函数 f(x,y)。 f 不是特别昂贵,因此如果需要,我可以生成带权重的索引邻接列表。 有哪些有效、易处理的方法可以通过最小化或最大化
谁能给我指出一个关于如何构建(乘法和/或加法)加权 voronoi 图的引用实现,该图最好基于 Fortune 的 voronoi 算法? 我的目标:给定一组点(每个点都有一个权重)和一组边界边(通常
有没有一种方法可以使用标准库进行漂亮而优雅的加权洗牌?有 std::discrete_distribution。我想要的是这样的: std::vector data { N elements }; s
其实不是RANDBETWEEN()。我正在尝试创建一个 UDF 来返回数组中数字的索引,其中数字越大,被选择的可能性就越大。 我知道如何将概率分配给工作表中的随机数(即对概率之和使用 MATCH()
canvas 占据了整个屏幕,从绿色可以看出。canvasFrame 有两行,其中第二行是滚动文本小部件。第二行也被加权但是它没有填满屏幕,因为绿色区域应该是黄色的。如何让第二行填充空白并让滚动的文本
我正在研究 HITS 算法实现的加权版本。 这是 Hits 算法的公式(非加权版本): 其中HITS A为权威评分,HITS H为hub评分,维基百科算法伪代码: G := set of pages
我不确定如何实现这个,但这里是描述: 取一个 0-10 之间的数字作为输入(0 总是返回 false,10 总是返回 true) 将接收到的参数作为输入,并传递给一个函数,在运行时确定所需的 bool
所以我在 Pandas DataFrame 中有两个值列和两个权重列,我想生成第三列,它是这两列的分组依据、加权平均值。 因此: df = pd.DataFrame({'category':['a',
我正在尝试使用 ggridges 包(基于 ggplot2)创建一个 joyplot。一般的想法是 joyplot 创建很好缩放的堆叠密度图。但是,我似乎无法使用加权密度生成其中之一。在创建 joyp
我是一名优秀的程序员,十分优秀!