转换列表< myObject>进入地图< integer,string>基于Java 8中的Max myobject属性
我有一个列表< myObject>
。它包含一个 ObjectId
的多个记录。
myObject
类看起来
class MyObject{
int id;
int objectId;
String status;
LocalDate updatedTimestamp;
}
我需要一个 map< integer,string>
带有 objectiD
as key 和相应的状态
作为 value 。
我只需要使用一个 ObjectID
的记录,其中最新的更新了Timestamp
。
不同的时间戳将具有不同的状态
。只需要最新的。
我没有找到实现这一目标的方法。我知道我可以通过应用获得
映射,通过应用
maxby()。 groupingby()
and code> and
请指教。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以使用collector
groupingby()
与collecting> collectingandthen()
和maxby()
一起完成。collector
maxby()
期望A 比较器作为参数,并产生可选结果。要创建一个比较器,我们可以使用Java 8方法It can be done using collector
groupingBy()
in conjunction withcollectingAndThen()
andmaxBy()
.Collector
maxBy()
expect a comparator as an argument and produces an optional result. To create a comparator, we can make use of the Java 8 methodComparator.comparing()
.