import java.util.*;
public class HashtableTest {
static String[] array1 = {"C","B","A"};
static String[] array2 = {"1","2","3"};
public static void main(String args[]) {
Hashtable h = new Hashtable();
h.put(array1[0], array2[0]);
h.put(array1[1], array2[1]);
h.put(array1[2], array2[2]);
// unsorted keys output
Iterator it = h.keySet().iterator();
while (it.hasNext()) {
String element = (String)it.next();
System.out.println(element+" "+(String)h.get(element));
}
System.out.println("============");
// sorted keys output
Vector v = new Vector(h.keySet());
Collections.sort(v);
it = v.iterator();
while (it.hasNext()) {
String element = (String)it.next();
System.out.println( element+" "+(String)h.get(element));
}
/*
output :
A 3
C 1
B 2
============
A 3
B 2
C 1
*/
}
}
Collection의 sort()를 이용해서 Iterator를 정렬합니다.
Collection으로 Iterator를 정렬합니다.
1 comment
Hashtable이 아닌 ArrayList를 정렬하는 것도 혹시 있을까요?