Comparator 정렬 구현(java7,java8)

12515 단어 수필.
컬렉션 객체:
//JYJProduct 
JYJProduct jyjProduct = new JYJProduct();
//list 
List<JYJProduct> list= new ArrayList<JYJProduct>();

방법1:
 //  ** ** 
Collections.sort(list, new Comparator<JYJProduct>() {
              @Override
                 public int compare(JYJProduct o1, JYJProduct o2) {
                     if (o1.getComprehensive() > o2.getComprehensive()) {
                         return 1;
                     }
                     if (o1.getComprehensive().equals(o2.getComprehensive())) {
                         return 0;
                     }
                     return -1;
                 }
});
 //  ** ** 
Collections.sort(list, new Comparator<JYJProduct>() {
	   @Override
	   public int compare(JYJProduct o1, JYJProduct o2) {
	       if (o1.getComprehensive() > o2.getComprehensive()) {
	           return -1;
	       }
	       if (o1.getComprehensive().equals(o2.getComprehensive())) {
	           return 0;
	       }
	       return 1;
	   }
});

메서드 2(java 7 정렬):
 //  ** ** 
  Collections.sort(list, new Comparator<JYJProduct>() {
           @Override
             public int compare(JYJProduct s1, JYJProduct s2) {
                 return s1.getComprehensive().compareTo(s2.getComprehensive());
             }
   });
    //  ** ** 
    Collections.sort(list, new Comparator<JYJProduct>() {
           @Override
              public int compare(JYJProduct s1, JYJProduct s2) {
                  return s2.getComprehensive().compareTo(s1.getComprehensive());
              }
	});

메서드 3(java 8 정렬):
 //  ** ** 
 Collections.sort(list, (s1, s2) -> s1.getComprehensive().compareTo(s2.getComprehensive()));
 //  ** ** 
 Collections.sort(list, (s1, s2) -> s2.getComprehensive().compareTo(s1.getComprehensive()));

좋은 웹페이지 즐겨찾기