在TreeMap中根据值列表中最旧的值来排序键? [英] Ordering the Keys in a TreeMap dependant on the oldest value in its list of values?

查看:247
本文介绍了在TreeMap中根据值列表中最旧的值来排序键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TreeMap 如下:

  TreeMap< Parent ,List< Child>> mapOfParentsAndChilds = new TreeMap< Parent,List< Child>>(); 

我要通过以下条件订购地图:



地图中的第一个键(父对象)将是其值列表中具有最老的子的键。



如果父项A的值列表中的最早的子项 20 岁,而父项B的值列表中最早的子项为 19 岁,则父项A应<



如何实现此解决方案? p>子实体:

  @Entity 
@Table(name =CHILD)
public class子
{
//其他vars

@Column(name =AGE)
private int age;

}


解决方案

具有自定义排序的地图,您可以使用构造函数,该构造函数需要 Comparator 。比较器可让您决定如何订购键。



在Java 8 Comparator.comparing 已添加使用新的流式传输功能简单。如果你不熟悉Java 8流,请阅读它们。

 地图< Parent,List< Children> mapOfParentsAndChildren = new TreeMap<>(
Comparator.comparing(parent - >
parent.getChildren()。stream()
.mapToInt(Child :: getAge)
。 min()。orElse(0)

.thenComparing(System :: identityHashCode)
);

请注意,子级列表需要从父级对象访问。您不能通过键的相应值的属性来订购地图的键,因为地图值可以更改。


@assylias:如果两个父母有同龄的最老的孩子,只有一个将被保留在地图中,因为它们将被视为平等。


,我们可以使用 thenComparing() 。如果第一个说的最老的孩子是平等的,使用这个额外的比较器。您可以添加任何其他条件,以决定在这种情况下哪个父胜利。我把 System :: identityHashCode 作为结束者。 identityHashCode 返回任意但一致的整数。实际上,我使用它来确保不同的父母比较不平等,但首先是任意的。



如果你有更好的标准使用,我鼓励你改变这个。例如,如果每个父级都有一个唯一的名称, .thenComparing(Parent :: getName)会更好。


I have a TreeMap as follows:

  TreeMap<Parent, List<Child>> mapOfParentsAndChilds = new TreeMap<Parent, List<Child>>();

I want to order the Map by the following condition:

The First key (Parent object) in the Map will be the one that has the oldest child in their list of values.

i.e. If the oldest child in Parent A's list of values was 20 years old, and the oldest child in Parent B's list of values was 19 years old then Parent A should be before Parent B in the map and so on.

How can I implement this solution?

Child entity:

@Entity
@Table(name = "CHILD")
public class Child
{
    //other vars 

    @Column(name = "AGE")
    private int age;

}

解决方案

To create a map with custom sorting you can use the constructor which takes a Comparator. A comparator lets you decide how to order keys.

In Java 8 Comparator.comparing was added, which makes creating a comparator using the new streaming feature easy. If you're not familiar with Java 8 streams, read up on them. They're incredibly powerful and convenient.

Map<Parent, List<Children>> mapOfParentsAndChildren = new TreeMap<>(
    Comparator.comparing(parent ->
        parent.getChildren().stream()
            .mapToInt(Child::getAge)
            .min().orElse(0)
    )
    .thenComparing(System::identityHashCode)
);

Notice that the list of children needs to be accessible from the parent object. You can't order a map's keys by a property of the keys' corresponding values because map values can change.

@assylias: If two parents have oldest children of the same age, only one will be kept in the map because they will be deemed equal.

To fix that, we can chain an additional comparator using thenComparing(). This extra comparator is used if the first one says the oldest children are equal. You can add in whatever additional criteria you want to decide which parent wins in that case. I threw in System::identityHashCode as a tie breaker. identityHashCode returns an arbitrary, yet consistent integer. In essence I'm using it to ensure different parents compare unequal, but which one goes first is arbitrary.

If you have some better criterion to use, I encourage you to change this. For instance, if each parent has a unique name, .thenComparing(Parent::getName) would be better.

这篇关于在TreeMap中根据值列表中最旧的值来排序键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆