如何找到Pricepoint的最低排名
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何找到Pricepoint的最低排名相关的知识,希望对你有一定的参考价值。
任何人都可以指导我如何映射内部地图的结果与外部地图。我创建了一个带有List持有Id,Pricepoint,Rank的List的对象,我已经通过获得每个价格点的最低等级来映射到内部地图,现在我想用他们的特定Id来映射结果。谢谢
public class Pricepoint {
String id;
String pricepoint;
int rank;
public Pricepoint(String id,String pricepoint,int rank)
{
this.id = id;
this.pricepoint = pricepoint;
this.rank= rank;
}
public String getId() {
return id;
}
public String getPricepoint() {
return pricepoint;
}
public int getRank() {
return rank;
}
public static void main(String[] args)
{
final Comparator<Pricepoint> comp = (p1, p2) -> Integer.compare( p1.getRank(), p2.getRank());
List<Pricepoint> p = new LinkedList<>();
p.add(new Pricepoint("1","PP1",1));
p.add(new Pricepoint("2", "PP1", 2));
p.add(new Pricepoint("3","PP2",3));
p.add(new Pricepoint("4", "PP2", 4));
Map<String,Map<String,Integer>> map1 = p.stream()
.collect(Collectors.toMap(Pricepoint::getPricepoint,Pricepoint::getRank,Math::min))// I'm struck here
答案
如果我没有弄错的话,你似乎想要这样的东西:
Map<String, Map<String, Integer>> map1 = p.stream()
.collect(Collectors.collectingAndThen(
Collectors.groupingBy(
Pricepoint::getPricepoint,
Collectors.collectingAndThen(
Collectors.minBy(Comparator.comparing(Pricepoint::getRank)),
Optional::get)),
(Map<String, Pricepoint> x) -> {
return x.entrySet()
.stream()
.collect(Collectors.groupingBy(
entry -> entry.getValue().getId(),
Collectors.toMap(Entry::getKey, e -> e.getValue().getRank())));
}));
System.out.println(map1);
但这简直太复杂了......老实说,我真的希望你有一个用例。
以上是关于如何找到Pricepoint的最低排名的主要内容,如果未能解决你的问题,请参考以下文章
PHP 搜索引擎排名跟踪器 - RankTrackr访问片段