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

时间:2018-07-31 06:22:07

标签: java arraylist collections

我需要“我的阵列”列表数据中的最高,第二高和第三高的数字。我通过使用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;
        }
}        
             
             

9 个答案:

答案 0 :(得分:2)

自从在Comparable上实现了PartyNamesDTO接口以来,您就可以做到

Collections.sort(electionresdashlist)

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

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

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

答案 1 :(得分:1)

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);
        

答案 2 :(得分:0)

尝试此代码:-

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;
        }
}  

答案 3 :(得分:0)

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名将为您提供结果

答案 4 :(得分:0)

您可以使用Java 8流来排序和跳过不必要的元素

list.stream().sorted(Comparator.reverseOrder()).limit(2).skip(1).findFirst()

findFirst()返回对象的Optional<T>

答案 5 :(得分:0)

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());
    }
}

答案 6 :(得分:0)

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();

这可能会对您有所帮助。

答案 7 :(得分:0)

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());}

答案 8 :(得分:-1)

再次搜索没有第一个最大值的最大值,因此得到第二个最大值。