如何使用集合从arrayList调用第二大数字

编程入门 行业动态 更新时间:2024-10-11 05:25:16
本文介绍了如何使用集合从arrayList调用第二大数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要我的阵列"列表数据中的最高,第二高和第三高的数字.我通过使用Collections.max()获得最大价值(最高数字).但是,我需要第二大和第三大值. 我的代码:

I need Highest,second highest and third highest number from My Array list Data. I am getting Max Value (highest number) by using Collections.max(). But, I need Second largest and third largest values. My Code:

maxPartyid = Collections.max(electionresdashlist.get(k).getPartyNamesDTO()); //1st Highest System.out.println( "maxPartyid:::"+maxPartyid); in DTO : public class PartyNamesDTO implements Comparable<PartyNamesDTO>{ private String partyId; public String getPartyId() { return partyId; } public void setPartyId(String partyId) { this.partyId = partyId; } private Integer votes; public Integer getVotes() { return votes; } public void setVotes(Integer votes) { this.votes = votes; } @Override public int compareTo(PartyNamesDTO emp) { return this.votespareTo(emp.getVotes()); } public String toString(){ return partyId; } }

推荐答案

由于您已经在PartyNamesDTO上实现了Comparable接口,因此您可以

Since you implemented the Comparable interface on PartyNamesDTO you can just do

Collections.sort(electionresdashlist)

这将使您的列表根据compareTo逻辑进行排序,如果您的排序是升序,则可以获得最大记录为

which will make your list sorted according to compareTo logic and if your sorting is ascending order then you can get max record as

electionresdashlist.get(electionresdashlist.size()-1); electionresdashlist.get(electionresdashlist.size()-2); electionresdashlist.get(electionresdashlist.size()-3);

,如果是降序排列,则相反.

and if it is descending order the other way around

更多推荐

如何使用集合从arrayList调用第二大数字

本文发布于:2023-11-30 08:20:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   第二大   数字   arrayList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!