java自定义容器排序实现接口

package com.breaver;

//import java.awt.List;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Scanner;

//import java.io.IOException;

//实现对容器元素排序自定义

public class Main implements Comparator{

/* (non-Javadoc)

* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)

*/

/*从大到小排序---虽然这个对题目没用,因为

我看错了是第三贵-----但是这个自定义容器排序接口还是可以的

*/

@Override

public int compare(Object o1, Object o2) {

// TODO Auto-generated method stub

int a = ((Integer)o1).intValue();

int b = ((Integer)o2).intValue();

if(a>b)

return -1;

else if(a<b)

return 1;

return 0;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc =new Scanner(System.in);

int n=0;

ArrayList<Integer> al = new ArrayList<>();//lambda表达式---让编译器自己猜

n = sc.nextInt();

for(int i = 0;i<n;i++)

al.add(sc.nextInt());

sc.close();

Collections.sort(al);

int j=1,temp=al.get(0);

for(int i=1;i<al.size();i++)

if(temp!=al.get(i)){

temp = al.get(i);

j++;

if(j == 3)

break;

}

if(j == 3)

System.out.print(temp);

else

System.out.print("-1");

}

}