* ȸ¿ø°¡ÀÔ * ¾ÆÀ̵ð/ºñ¹Ð¹øÈ£ ã±â   ID PW
Last Modified : 2008.09.18

Collection

Collection À̶õ ·¹ÆÛ·±½º¸¦ ´ãÀ» ¼ö ÀÖ´Â ÀڷᱸÁ¶¿Í °ü·ÃµÈ Ŭ·¡½ºµéÀ» ¸»ÇÕ´Ï´Ù.
½±°Ô ÀÌÇØÇϱâ À§Çؼ­´Â ·¹ÆÛ·±½º¸¦ ´ã±â À§ÇÑ ±×¸©À̶ó°í »ý°¢Çϼ¼¿ä.
ÀÌ·± ÀڷᱸÁ¶¸¦ Áö¿øÇϱâ À§ÇØ ÀÚ¹Ù¿¡¼­´Â Collection, Set, List, Map, SortedSet, SortedMap À̶ó´Â interface °¡ ÀÖ°í À̵é interface ¸¦ implements ÇÑ ´Ù¼öÀÇ class °¡ ÀÖ½À´Ï´Ù.
ÀÌµé ¸ðµÎ¸¦ °¡¸£ÄÑ Collection Framework ¶ó°í ºÎ¸¨´Ï´Ù.
´ÙÀ½Àº Collection Framework ÀÇ Å¬·¡½º ´ÙÀ̾î±×·¥À» º¸¿©ÁÖ°í ÀÖ½À´Ï´Ù.
Collection Framework

Collection Framework ÀÇ ÀÎÅÍÆäÀ̽º

java.util.Collection

  • ÀúÀåµÈ reference µéÀÌ ¼ø¼­¸¦ °¡ÁöÁö ¾Ê´Â´Ù
  • Áߺ¹ÀÌ Çã¿ëµÈ´Ù

Set

  • ÀúÀåµÈ reference µéÀÌ Æ¯Á¤ÇÑ ¼ø¼­¸¦ °¡Áö°í ÀÖÁö ¾Ê´Ù
  • Áߺ¹ÀÌ Çã¿ëµÇÁö ¾Ê´Â´Ù

List

  • ÀúÀåµÈ reference µéÀÌ Æ¯Á¤ÇÑ ¼ø¼­¸¦ °¡Áö°í ÀÖ´Ù
  • Áߺ¹ÀÌ Çã¿ëµÈ´Ù

Map

  • ÀúÀåµÈ reference µéÀÌ Key¿Í Key¿¡ ´ëÇÑ ValueÀÇ ÇüÅ·ΠÀúÀåµÈ´Ù

SortedSet

  • °ªµéÀÌ Á¤·ÄµÈ Set

SortedMap

  • Key°¡ Á¤¿­µÈ Map

collection classes °èº¸


Interface Implementation Historical
Set HashSet  
TreeSet  
List ArrayList Vector
LinkedList Stack
Map HashMap Hashtable
TreeMap Properties

Set »ç¿ë ¿¹Á¦

SetExample.java

package net.java_school.example;

import java.util.*;

public class SetExample {
  public static void main(String args[]) {
  
    Set set = new HashSet();
    set.add("¾çÈ¿¼±");
    set.add("È«¿ëÇ¥");
    set.add("ȲÁøÈ£");
    set.add("±èµ¿Áø");
    set.add("Àü°æ¼ö");
    
    System.out.println(set);
    
    Set sortedSet = new TreeSet(set);
    System.out.println(sortedSet);
    
  }
}

JDK ¹öÀü 1.5 À̻󿡼­ Generic ¸¦ »ç¿ëÇÏ´Â Äڵ带 º¯°æÇÏ¸é ¾Æ·¡¿Í °°½À´Ï´Ù.

SetExample.java

package net.java_school.example;

import java.util.*;

public class SetExample {
  public static void main(String args[]) {
  
    Set<String> set = new HashSet<String>();
    
    set.add("¾çÈ¿¼±");
    set.add("È«¿ëÇ¥");
    set.add("ȲÁøÈ£");
    set.add("±èµ¿Áø");
    set.add("Àü°æ¼ö");
    
    System.out.println(set);
    
    Set<String> sortedSet = new TreeSet<String>(set);
    System.out.println(sortedSet);
    
  }
}

µÎ ¿¹Á¦ÀÇ Â÷ÀÌ´Â ¾Æ·¡¸¦ Âü°íÇÕ´Ï´Ù.
Collection API¸¦ JDK 1.5 ÀÌ»óÀÇ ¹®¼­¿¡¼­ º¸¸é ÀÎÅÍÆäÀ̽º³ª Ŭ·¡½º ¸í ¿·¿¡ <E>, <T>, <K, V> °¡ ºÙ¾îÀÖ´Â °ÍÀ» º¼ ¼ö ÀÖ½À´Ï´Ù.
À̰ÍÀº Java 5.0ºÎÅÍ Generics ¶ó´Â °ÍÀÌ »õ·Î »ý±â¸é¼­ ³ª¿Â Generics ¿¡ ´ëÇÑ Ç¥±â¹ýÀÔ´Ï´Ù.
ÀÚ¹Ù doc ¹®¼­¿¡ ³ªÅ¸³­ Generics Ç¥±â¹ýÀÇ Àǹ̴Â
<E>´Â Element, <T>´Â Type, <K, V> ´Â Key, Value ÀÔ´Ï´Ù.
ÀÌ·¸µí Generics ´Â ÁÖ·Î Collection API ¿¡ »ç¿ëµÇ¸ç ÇØ´ç collection ¿¡ ¾î¶² µ¥ÀÌÅÍ Å¸ÀÔÀ» º¸°üÇÒ °ÍÀÎÁö ¸í½ÃÀûÀ¸·Î Ç¥½Ã¸¦ ÇÒ ¶§ »ç¿ëµË´Ï´Ù.

List »ç¿ë ¿¹Á¦

ListExample.java

package net.java_school.example;

import java.util.*;

public class ListExample {
  public static void main(String args[]) {
  
    List list = new ArrayList();
    
    list.add("¾çÈ¿¼±");
    list.add("È«¿ëÇ¥");
    list.add("ȲÁøÈ£");
    list.add("±èµ¿Áø");
    list.add("Àü°æ¼ö");
    
    System.out.println(list);
    System.out.println("2: " + list.get(2));
    System.out.println("0: " + list.get(0));
    
    LinkedList queue = new LinkedList();
    
    queue.addFirst("¾çÈ¿¼±");
    queue.addFirst("È«¿ëÇ¥");
    queue.addFirst("ȲÁøÈ£");
    queue.addFirst("±èµ¿Áø");
    queue.addFirst("Àü°æ¼ö");
    
    System.out.println(queue);
    queue.removeLast();
    queue.removeLast();
    
    System.out.println(queue);
    
  }
}

À§ ¿¹Á¦¸¦ Generic ¸¦ ÀÌ¿ëÇÏ´Â ¿¹Á¦·Î º¯È¯ÇÏ¸é ¾Æ·¡¿Í °°½À´Ï´Ù.

ListExample.java

package net.java_school.example;

import java.util.*;

public class ListExample {
  public static void main(String args[]) {
  
    List<String> list = new ArrayList<String>();
    
    list.add("¾çÈ¿¼±");
    list.add("È«¿ëÇ¥");
    list.add("ȲÁøÈ£");
    list.add("±èµ¿Áø");
    list.add("Àü°æ¼ö");
    
    System.out.println(list);
    System.out.println("2: " + list.get(2));
    System.out.println("0: " + list.get(0));
    
    LinkedList<String> queue = new LinkedList<String>();
    
    queue.addFirst("¾çÈ¿¼±");
    queue.addFirst("È«¿ëÇ¥");
    queue.addFirst("ȲÁøÈ£");
    queue.addFirst("±èµ¿Áø");
    queue.addFirst("Àü°æ¼ö");
    
    System.out.println(queue);
    
    queue.removeLast();
    queue.removeLast();
    System.out.println(queue);
    
  }
}

Vector »ç¿ë ¿¹Á¦

VectorExample.java

package net.java_school.example;

import java.util.*;

public class VectorExample {

  public Vector vect;

  public VectorExample () {
    vect = new Vector();
  }

  public static void main ( String[] args ) {
  
    VectorExample ve = new VectorExample();
    
    for ( int i = 0; i < 10; i++ ) {
      ve.vect.addElement( String.valueOf( Math.random() * 100 ) );
    }
    
    for ( int i = 0; i < 10; i++ ) {
      System.out.println( ve.vect.elementAt(i) );
    }
  }
  
}

¿¹Á¦¸¦ Generic ¸¦ »ç¿ëÇØ¼­ º¯È¯ÇÏ¸é ¾Æ·¡¿Í °°½À´Ï´Ù.

VectorExample.java

package net.java_school.example;

import java.util.*;

public class VectorExample {

  public Vector<String> vect;

  public VectorExample () {
    vect = new Vector<String>();
  }

  public static void main ( String[] args ) {
  
    VectorExample ve = new VectorExample();
    
    for ( int i = 0; i < 10; i++ ) {
      ve.vect.addElement( String.valueOf( Math.random() * 100 ) );
    }
    
    for ( int i = 0; i < 10; i++ ) {
      System.out.println( ve.vect.elementAt(i) );
    }
  }
  
}

Map »ç¿ë ¿¹Á¦

MapExample.java

package net.java_school.example;

import java.util.*;

public class MapExample {
  public static void main(String args[]) {

    Map map = new HashMap();

    map.put("1", "¾çÈ¿¼Ç");
    map.put("2", "È«¿ëÇ¥");
    map.put("3", "ȲÁøÈ£");
    map.put("4", "±èµ¿Áø");
    map.put("5", "Àü°æ¼ö");

    System.out.println(map);
    System.out.println((String)map.get("4"));

    Map sortedMap = new TreeMap(map);
    System.out.println(sortedMap);

  }
}

À§ ¿¹Á¦¸¦ Generic ¸¦ »ç¿ëÇØ¼­ º¯È¯ÇÏ¸é ¾Æ·¡¿Í °°½À´Ï´Ù.

MapExample.java

package net.java_school.example;

import java.util.*;

public class MapExample {
  public static void main(String args[]) {

    Map<String,String> map = new HashMap<String,String>();

    map.put("1", "¾çÈ¿¼Ç");
    map.put("2", "È«¿ëÇ¥");
    map.put("3", "ȲÁøÈ£");
    map.put("4", "±èµ¿Áø");
    map.put("5", "Àü°æ¼ö");

    System.out.println(map);
    System.out.println((String)map.get("4"));

    Map<String,String> sortedMap = new TreeMap<String,String>(map);
    System.out.println(sortedMap);

  }
}

Wrapper Ŭ·¡½º

Collection API ¿¡¼­´Â ·¹ÆÛ·±½º¸¸À» ´ãÀ» ¼ö ÀÖ½À´Ï´Ù.
¿ø½Ã µ¥ÀÌÅÍÇüÀÎ °æ¿ì´Â Collection ¿¡ ´ãÀ» ¼ö ¾ø½À´Ï´Ù.
¿ø½Ã µ¥ÀÌÅÍÇüÀ» Collection ¿¡ ´ãÀ» ¼ö´Â ¾øÀ» ±î¿ä?
Wrapper Ŭ·¡½º ÀÌ¿ëÇÏ´Â °ÍÀÌ ´äÀÏ µÉ ¼ö ÀÖÀ» °Ì´Ï´Ù.
¸ðµç ±âº» µ¥ÀÌÅÍ Å¸ÀÔ¿¡ ´ëÇØ¼­ ±×¿¡ ´ëÀÀÇÏ´Â wrapper Ŭ·¡½º°¡ Á¸ÀçÇÕ´Ï´Ù.

±âº» µ¥ÀÌÅÍ Å¸ÀÔ Wrapper Ŭ·¡½º
boolean Boolean
byte Byte
char Character
short Short
int Integer
long Long
float Float
double Double

Enumeration ÀÎÅÍÆäÀ̽º

¿­°ÅÇü ÇüÅ·ΠÀúÀåµÈ °´Ã¼¸¦ óÀ½ºÎÅÍ ³¡±îÁö Â÷·Ê·Î Á¶È¸Çϴµ¥ À¯¿ëÇÑ ÀÎÅÍÆäÀ̽ºÀÔ´Ï´Ù.
¸Þ¼Òµå´Â ´ÙÀ½ 2°³°¡ ÀüºÎÀÔ´Ï´Ù.

hasMoreElements()
nextElement()

¾Æ·¡ ÄÚµå´Â Vector<E> v ÀÇ ¸ðµç ¿ä¼Ò¸¦ Ãâ·ÂÇÕ´Ï´Ù.

for (Enumeration<E> e = v.elements(); e.hasMoreElements();) {
  System.out.println(e.nextElement());
}

Iterator ÀÎÅÍÆäÀ̽º

»ç¿ë¹ýÀº Enumeration °ú ºñ½ÁÇÕ´Ï´Ù.
Enumeration º¸´Ù´Â ¸Þ¼Òµå¸íÀÌ ´õ °£´ÜÇϸç Enumeration °ú´Â ´Þ¸® ¿ä¼Ò¸¦ »èÁ¦ÇÏ´Â ¸Þ¼Òµå°¡ ÀÖ½À´Ï´Ù.

¸Þ¼Òµå

hasNext()
next()
remove()

Properties Ŭ·¡½º

ÀÚ¹Ù¿¡¼­ ¼³Á¤ ÆÄÀϷκÎÅÍ °ªÀ» ÀÐÀ» ¶§ ¸¹ÀÌ »ç¿ëÇϴ Ŭ·¡½ºÀÔ´Ï´Ù.

PropertiesTest1.java

package net.java_school.example;

import java.util.*;
import java.io.*;
public class PropertiesTest1 {
  public static void main(String[] args) {

    Properties prop = new Properties();
    prop.put("name", "±èÅÂÈñ");
    prop.put("address", "¼­¿ï ¼­Ãʱ¸");
    try {
      prop.store(new FileOutputStream("test.properties"),"My Favorite Actress");
    } catch ( IOException e ) {}
  }
}

PropertiesTest2.java

package net.java_school.example;

import java.util.*;
import java.io.*;
public class PropertiesTest2 {
  public static void main(String[] args) {

    Properties prop2 = new Properties();
    try {
      prop2.load(new FileInputStream("test.properties"));
    } catch ( IOException e ) {}
    System.out.println(prop2.getProperty("name"));
    System.out.println(prop2.getProperty("address"));
  }
}

PropertiesTest1 ¿¹Á¦¸¦ ½ÇÇàÇÏ¸é ÆÄÀÏ½ÃÆ®ÅÛ¿¡ test.properties ÆÄÀÏÀÌ ¸¸µé¾îÁý´Ï´Ù. ÆÄÀÏÀ» ¿­¾îº¸¸é ´ÙÀ½°ú °°½À´Ï´Ù.

test.properties

#My Favorite Actress
#Thu Sep 18 21:03:24 KST 2008
address=\uC11C\uC6B8 \uC11C\uCD08\uAD6C
name=\uAE40\uD0DC\uD76C

¿¹»ó°ú ´Þ¸® ÇÑ±Û ºÎºÐÀÌ ÀÌ»óÇÑ ¹®ÀÚ·Î µÇ¾î ÀÖ½À´Ï´Ù. ÀÚ¹Ù ÇÁ·¯ÆÛƼ´Â ÀÚ¹Ù ÇÁ·Î±×·¥¿¡¼­ ¼³Á¤¿¡ °ü·ÃµÈ ºÎºÐ¿¡ ÀÌ¿ëÇϱâ À§ÇØ ¸¸µé¾î Á³Áö¸¸ ºñ¿µ¾î±ÇÀ» À§ÇÑ ¹è·Á´Â ÇÏÁö ¾ÊÀº °Í °°½À´Ï´Ù.
ÇѱÛÀº °æ¿ì ÇÁ·ÎÆÛƼ ÆÄÀÏ¿¡¼­ ¾Æ½ºÅ° Äڵ尪À¸·Î ÀúÀåµÇ¾î ÀÖ¾î¾ß ÇÕ´Ï´Ù.
À̰ÍÀÌ ¿ì¸®·Î¼­´Â ÀÚ¹Ù ÇÁ·ÎÆÛƼÀÇ ´ÜÁ¡ÀÔ´Ï´Ù.