티스토리 뷰

Java

Strategy Pattern - Comparator #2

techbard 2016. 7. 25. 14:37



# Main.java


import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;


public class Main {


    public static void main(String[] args) {


List<String> al = new ArrayList<>();

al.add("ab");

al.add("c");


Collections.sort(al, new Comparator<String>() {


   // sorted by string length

   @Override

   public int compare(String o1, String o2) {

if (o1.length() == o2.length()) {

   return 0;

}


return o1.length() > o2.length() ? -1 : 1;

   }

});

for (String a : al) {

   System.out.println(a);

}

    }

}



# 결과


ab

c


댓글
댓글쓰기 폼
공지사항
Total
410,766
Today
35
Yesterday
44
«   2019/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
글 보관함