2015.06.19 / 10:18
HashMap, TreeMap, Hashtable »ç¿ë¹ý Á¤¸®
¸ÞÀÏ·¯
Ãßõ ¼ö 281
TreeMap´Â value¿¡ ÀÇÇØ °ª Á¤·Ä °¡´ÉÇϵµ·Ï ÇÔ!
(÷ºÎÆÄÀÏ ´Ù¿î ¹Þ¾Æ¼ È®ÀÎ)
<%
/*
java HashMap »ç¿ë¹ý
1. Map interface¸¦ implements ÇÑ Å¬·¡½º·Î Áߺ¹ Çã¿ë ¾ÈÇÔ
2. key, value ½ÖÀ¸·Î ÀÌ·ç¾îÁö¸ç null Çã¿ë
3. °¡Àå óÀ½ ³ÖÀº µ¥ÀÌÅÍ°¡ index 0 (FIFO)
*/
java.util.HashMap hashmap = new java.util.HashMap();
hashmap.put("a","2");
hashmap.put("c","3");
hashmap.put("b","1");
out.println ("hash map size : " + hashmap.size() + "
");
// hash map °ª °¡Á®¿À±â (¹æ¹ý1)
java.util.Set keyset = hashmap.keySet();
Object[] hashkeys = keyset.toArray();
for(int i=0; i
String key = (String)hashkeys[i];
out.println(key + " - " + hashmap.get(key));
out.println("
");
}
out.println("
");
// hash map °ª °¡Á®¿À±â (¹æ¹ý1)
java.util.Iterator itr = hashmap.keySet().iterator();
while (itr.hasNext()) {
String key = (String)itr.next();
out.println(key + " - " + hashmap.get(key));
out.println("
");
}
%>
½ÇÇà °á°ú
a - 2
c - 3
b - 1
<%
/*
java TreeMap »ç¿ë¹ý
1. Map interface¸¦ implements ÇÑ Å¬·¡½º·Î Áߺ¹ Çã¿ë ¾ÈÇÔ
2. SortedMapÀ» implements ÇÏ¿© key °ª¿¡ ´ëÇÑ Á¤·Ä
3. key°ª¿¡ ÀÇÇؼ ÀÚµ¿ Á¤·Ä µÊ
*/
java.util.TreeMap treemap = new java.util.TreeMap();
treemap.put("a","2");
treemap.put("c","3");
treemap.put("b","1");
// treemap size
out.println ("tree map size : " + treemap.size() + "
");
// tree map °ª °¡Á®¿À±â (¹æ¹ý1)
Object[] hashkeys = (treemap.keySet()).toArray();
for(int i=0; i
String key = (String)hashkeys[i];
out.println(key + " - " + treemap.get(key));
out.println("
");
}
out.println("
");
// tree map °ª °¡Á®¿À±â (¹æ¹ý2)
java.util.Iterator itr = treemap.keySet().iterator();
while (itr.hasNext()) {
String key = (String)itr.next();
out.println(key + " - " + treemap.get(key));
out.println("
");
}
out.println("
");
// tree map °ª °¡Á®¿À±â - value °ª Á¤·Ä·Î Çؼ°¡Á®¿À±â
java.util.TreeSet set = new java.util.TreeSet(new java.util.Comparator() {
public int compare(Object obj, Object obj1) {
return ((Comparable) ((java.util.Map.Entry) obj).getValue()).compareTo(((java.util.Map.Entry) obj1).getValue());
}
});
set.addAll(treemap.entrySet());
for (java.util.Iterator i = set.iterator(); i.hasNext();) {
java.util.Map.Entry entry = (java.util.Map.Entry) i.next();
out.println(entry.getKey() + " - " + entry.getValue());
out.println("
");
}
%>
½ÇÇà °á°ú
a - 2
b - 1
c - 3
--------------------------------------------------------------------------------
b - 1
a - 2
c - 3
<%
/*
java HashTable »ç¿ë¹ý
1. Map interface¸¦ implements ÇÑ Å¬·¡½º·Î Áߺ¹ Çã¿ë ¾ÈÇÔ
2. key, value ½ÖÀ¸·Î ÀÌ·ç¾îÁö¸ç null Çã¿ë ¾ÈÇÔ
3. °¡Àå ¸¶Áö¸·¿¡ ³ÖÀº µ¥ÀÌÅÍ°¡ index 0 (FILO)
*/
java.util.Hashtable hashtable = new java.util.Hashtable();
hashtable.put("a","2");
hashtable.put("c","3");
hashtable.put("b","1");
out.println ("hash table size : " + hashtable.size() + "
");
// hash table °ª °¡Á®¿À±â (¹æ¹ý1)
java.util.Set keyset = hashtable.keySet();
Object[] hashkeys = keyset.toArray();
for(int i=0; i
String key = (String)hashkeys[i];
out.println(key + " - " + hashtable.get(key));
out.println("
");
}
out.println("
");
// hash table °ª °¡Á®¿À±â (¹æ¹ý1)
java.util.Iterator itr = hashtable.keySet().iterator();
while (itr.hasNext()) {
String key = (String)itr.next();
out.println(key + " - " + hashtable.get(key));
out.println("
");
}
%>
½ÇÇà °á°ú
b - 1
a - 2
c - 3
÷ºÎÆÄÀÏ
hashfamily.zip