2012/03/29 13:35
IP주소 문자열을 가지는 목록 List<String> ipList 내부에 있는 IP주소를 정렬하는 방법
사용하는 클래스 java.util.Collections(class), java.util.Comparator(Interface)
정렬하는데 사용식 :
Collections.sort(ipList, new IpListSortByIp());
IpListSortByIp.java
public class IpListSortByIp implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
try {
if (InetAddress.getByName(o1).hashCode() > InetAddress.getByName(o2).hashCode()) {
return 1;
} else if (InetAddress.getByName(o1).hashCode() < InetAddress.getByName(o2).hashCode()) {
return -1;
}
}
catch (UnknownHostException e) {
//Exception 처리
}
return 0;
}
}
간단히 정의를 한다면,
문자열 IP주소를 java.net.InetAddress 객체로 변형하여 그 객체가 가지고 있는 HashCode를 비교하여 정렬하는 방식이다.
'JAVA Language' 카테고리의 다른 글
| 문자열 IP주소 목록 정렬하기 (0) | 2012/03/29 |
|---|---|
| [Java Object] java.lang.Class (0) | 2011/11/11 |
| 자바 음력날짜 얻기 (0) | 2011/10/11 |
| 자바언어의 패키지 코딩 관례 (0) | 2011/08/07 |
| Java Method Chaining : '자바에서 메소드 이어서 쓰기' 랄까? (0) | 2011/07/17 |
| 테스트 케이스에서 Transaction 예외시키는 방법, @Trasactional, @Transactional(propagation=Propagation.NOT_SUPPORTED) (0) | 2011/06/23 |
TAG compare,
ipaddress,
Java,
java.util.Collections,
sort