gpt4 book ai didi

java - 如何对对象集合进行排序

转载 作者:行者123 更新时间:2023-11-30 06:50:12 25 4
gpt4 key购买 nike

比如我有课

class Person{
Integer rank;
Double profit;
Person(Integer rank, Double profit){
this.rank = rank;
this.profit = profit;
}
Person(Double profit){
this(0, profit);
}
}

我想对按排名评估的盈利人员排序的列表进行排名。这样

rank(Arrays.asList(
new Person(30),
new Person(20),
new Person(20),
new Person(10))
)

会生成列表

new Person(1, 30), 
new Person(2, 20),
new Person(2, 20),
new Person(3, 10)

我还想使用 Java 8 中的自定义 Collector(或类似的东西)来完成它,而不是使用简单的循环。

最佳答案

让我们使用一个自定义收集器,我们将所有来自外部的东西传递给它。收集器将从调用者那里获得排名和构造函数。我们想这样调用它:

public List<Person> rank(List<Person> people) {
return people
.stream()
.sorted(Comparator.<Person>comparingDouble(x -> x.profit).reversed())
.collect(new IntegerRankingCollector<>(
Comparator.comparingDouble(p -> p.profit), // how to differentiate rankings
p -> p.rank, // where to get rank for an element which was already ranked
(p, rank) -> new Person(rank, p.profit) // how to create an element from another element values and a rank
));
}

这个收集器可以像Collectors.toList()一样实现但是使用累加器方法:

  1. 获取前一个元素的排名
  2. 如果当前元素等级应该不同于上一个元素等级
  3. 创建一个具有新等级的元素

这是它的外观,它应该适用于有序流:

public class IntegerRankingCollector<T> implements Collector<T, List<T>, List<T>> {
...

public IntegerRankingCollector(Comparator<? super T> comparator, Function<T, Integer> ranker, BiFunction<T, Integer, T> creator) {
this.comparator = comparator;
this.ranker = ranker;
this.creator = creator;
}

@Override
public BiConsumer<List<T>, T> accumulator() {
return (list, current) -> {
ArrayList<T> right = new ArrayList<>();
right.add(creator.apply(current, 1));
combiner().apply(list, right);
};
}

@Override
public BinaryOperator<List<T>> combiner() {
return (left, right) -> {
int rankAdjustment = getRankAdjustment(left, right);
for (T t : right)
left.add(creator.apply(t, rankAdjustment + ranker.apply(t)));
return left;
};
}

private int getRankAdjustment(List<T> left, List<T> right) {
Optional<T> lastElementOnTheLeft = optGet(left, left.size() - 1);
Optional<T> firstElementOnTheRight = optGet(right, 0);

if (!lastElementOnTheLeft.isPresent() || !firstElementOnTheRight.isPresent())
return 0;
else if (comparator.compare(firstElementOnTheRight.get(), lastElementOnTheLeft.get()) == 0)
return ranker.apply(lastElementOnTheLeft.get()) - 1;
else
return ranker.apply(lastElementOnTheLeft.get());
}

private Optional<T> optGet(List<T> list, int index) {
if (list == null || list.isEmpty())
return Optional.empty();
else
return Optional.of(list.get(index));
}

...
}

为完整起见,这是该类的完整代码。我从 Collectors.toList 复制了其余部分:

public class IntegerRankingCollector<T> implements Collector<T, List<T>, List<T>> {

private static final Set<Characteristics> CHARACTERISTICSS = Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
private Comparator<? super T> comparator;
private BiFunction<T, Integer, T> creator;
private Function<T, Integer> ranker;

public IntegerRankingCollector(Comparator<? super T> comparator, Function<T, Integer> ranker, BiFunction<T, Integer, T> creator) {
this.comparator = comparator;
this.ranker = ranker;
this.creator = creator;
}

@Override
public BiConsumer<List<T>, T> accumulator() {
return (list, current) -> {
ArrayList<T> right = new ArrayList<>();
right.add(creator.apply(current, 1));
combiner().apply(list, right);
};
}

@Override
public BinaryOperator<List<T>> combiner() {
return (left, right) -> {
int rankAdjustment = getRankAdjustment(left, right);
for (T t : right)
left.add(creator.apply(t, rankAdjustment + ranker.apply(t)));
return left;
};
}

private int getRankAdjustment(List<T> left, List<T> right) {
Optional<T> lastElementOnTheLeft = optGet(left, left.size() - 1);
Optional<T> firstElementOnTheRight = optGet(right, 0);

if (!lastElementOnTheLeft.isPresent() || !firstElementOnTheRight.isPresent())
return 0;
else if (comparator.compare(firstElementOnTheRight.get(), lastElementOnTheLeft.get()) == 0)
return ranker.apply(lastElementOnTheLeft.get()) - 1;
else
return ranker.apply(lastElementOnTheLeft.get());
}

private Optional<T> optGet(List<T> list, int index) {
if (list == null || list.isEmpty())
return Optional.empty();
else
return Optional.of(list.get(index));
}


@Override
public Supplier<List<T>> supplier() {
return ArrayList::new;
}

@Override
public Function<List<T>, List<T>> finisher() {
return l -> l;
}

@Override
public Set<Characteristics> characteristics() {
return CHARACTERISTICSS;
}
}

关于java - 如何对对象集合进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41600652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com