我需要从我的
ArrayList
中获得最高、第二和第三高的数字。我通过调用 Collections.max()
获得最大值(即最高数字),但我还需要第二和第三最高值。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.votes.compareTo(emp.getVotes());
}
public String toString(){
return partyId;
}
}
您可以使用java 8流来排序和跳过不必要的元素
list.stream().sorted(Comparator.reverseOrder()).limit(2).skip(1).findFirst()
findFirst()
返回对象的 Optional<T>
。
list.stream().sorted(Collections.reverseOrder()).distinct().limit(2).skip(1).findFirst().get()
既然你在
Comparable
上实现了 PartyNamesDTO
接口,你就可以做
Collections.sort(electionresdashlist)
这将使您的列表根据
compareTo
逻辑排序,如果您的排序是升序,那么您可以获得最大记录为
electionresdashlist.get(electionresdashlist.size()-1);
electionresdashlist.get(electionresdashlist.size()-2);
electionresdashlist.get(electionresdashlist.size()-3);
如果是降序则相反
Integer integer2 = numbers.stream().max((x,y)->x.compareTo(y)).get();
Integer integer3 = numbers.stream().filter(x->x<integer2).max((x,y)->x.compareTo(y)).get();
System.out.println("Second max is: "+integer3);
get last -1
PartyNamesDTO tmpObj = partyList.stream().sorted(Comparator.reverseOrder()).skip(1).findFirst().get();
get first-1
PartyNamesDTO tmpObj = partyList.stream().sorted(Comparator.naturalOrder()).skip(1).findFirst().get();
这可能对你有帮助。
我有一个简单的解决方案,可以从数组列表中找到第 n 最高元素:
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(12,44,76,1,8,9,9));
int highest = arr.stream().sorted(Collections.reverseOrder()).collect(Collectors.toList()).get(n-1);
试试这个代码:-
Collections.sort(electionresdashlist);
System.out.println( "maxPartyid:::"+electionresdashlist.get(0).getPartyNamesDTO());
System.out.println( "2nd maxPartyid:::"+electionresdashlist.get(1).getPartyNamesDTO());
System.out.println( "3rd maxPartyid:::"+electionresdashlist.get(2).getPartyNamesDTO());
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.votes.compareTo(emp.getVotes());
}
public String toString(){
return partyId;
}
}
long largest=0, secondLargest=-1, thirdLargest=-2;
listName.forEach(item -> {
if(item > largest) {
thirdLargest = secondLargest
secondLargest = largest;
largest = list.get(i);
}
if(item > secondLargest && item != largest) {
thirdLargest = secondLargest;
secondLargest = item;
}
if(item>thirdLargest && item != secondLargest) {
thirdLargest = item;
}
});
其他方法是使用比较器根据投票数排列列表元素,前 3 名将为您提供结果
static void getKthHighest(List<Integer> list, int k) {
Optional<Integer> element = list.stream().sorted(Collections.reverseOrder()).limit(k).skip(k - 1).findFirst();
if(element.isPresent()) {
System.out.println(element.get());
}
}
Integer[] arr= {80,19,32,150,2,78};
IntStream arrstream=Arrays.stream(arr);
OptionalInt firstmaxNum=stream.max();
IntStream secondmaxstream=Arrays.stream(arr);
OptionalInt secondMaximum=stream1.filter(s->s!=maxNum.getAsInt()).max();
System.out.println(" first maximum number :" +firstmaxNum.getAsInt());
System.out.println(" second maximum number :" +secondMaximum.getAsInt());}
如果列表如下
列表数字 = Arrays.asList(5, 9, 11, 2, 8, 21, 1, 21);
getKthHighest(数字, 2);
static void getKthHighest(List list, int k) { 可选元素 = list.stream().sorted(Collections.reverseOrder()).distinct().limit(k).skip(k - 1).findFirst(); if (element.isPresent()) { System.out.println(element.get()); } }
@Saheb 提供了一个很好的答案,但需要添加 unique() 以获得更好的结果。如果 2 个不重复,那么会有帮助。
再次搜索没有第一个最大值的最大值,这样你就得到了第二个最大值。