'list'에 해당되는 글 3건

  1. Java Collection 2008/07/03
  2. Iterator보다 빠르게 Collection의 오브젝트 처리 2008/02/21
  3. Initializing Collection 2008/02/14

Java Collection

from java 2008/07/03 16:47
Collection은 레퍼런스 타입들을 유지하기 위한 메카니즘이다.
Java는 대충 아래의 클래스들로 구성이 되어 있습니다. 추가적으로 java.util.concurrent 패키지에 확장 Collection 클래스들도 있습니다.

Collection : 가장 상위 인터페이스
 + Set : 중복요소가 없는 컬렉션으로, 하위 인터페이스로 SortedSet이 있습니다.
 + List : 순서가 있는 컬렉션으로, 인덱스로 위치를 지정하여 값을 찾을 수 있습니다.
 + Queue : FIFO의 구조로 객체를 유지하는 컬렉션으로, 하위 인터페이스는 BlockingQueue가 있습니다.
 + Map : 키를 값에 매핑 하는 컬렉션으로, 중복키를 허용하지 않는다. 하위 인터페이스들은 ConcurrentMap, SortedMap이 있습니다.

Set
- AbstractSet, CopyOnWriteArraySet, EnumSet, HashSet, JobStateReasons, LinkedHashSet, TreeSet

List
- AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector

Queue
- AbstractQueue, ArrayBlockingQueue, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingQueue, LinkedList, PriorityBlockingQueue, PriorityQueue, SynchronousQueue

Map
- AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, EnumMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, PrinterStateReasons, Properties, Provider, RenderingHints, TabularDataSupport, TreeMap, UIDefaults, WeakHashMap



Collection에 들어있는 오브젝트를 가져오기 위해서 일반적으로 Iterator 패턴을 구현한 Iterator 클래스를 많이 사용합니다. 하지만, Iterator클래스의 next()를 통해서 오브젝트를 가져오고, hasNext()를 통해서 오브젝트의 마지막을 체크하기 때문에, List 타입의 Collection에서는 get()를 사용하는 것이 더 빠르게 동작을 합니다.

ex) Iterator 클래스 사용예

Iterator iterator = array.iterator();
while (iterator.hasNext()) {
 Object object = iterator.next();
}

ex) List 타입의 get() 사용 예

int size = array.size();
for (int i=0; i<size ; i++) {
 Object object = array.get(i);
}

Initializing Collection

from java 2008/02/14 22:08

출처는 http://satukubik.com/2007/12/19/java-tips-initializing-collection/ 입니다.

Especially in unit test, we have to initialize an array or a collection. Well, for array, it’s OK… A simple code that we know can solve the problem:

String[] s = new String [] {"1", "2"};

But how about Collection? Normal way to initialize collection is something like this (which pretty ugly):

List<String> s = new ArrayList<String>();
s.add("1");
s.add("2");

I hardly find an elegant solution until I see this post. There are at least three better solution for the case.

First solution:

List<String> s = new ArrayList<String>() {{ add("1"); add("2"); }};

Which unfortunately, doesn’t pass Java Code Convention (that is, if you format the code, it will become uglier than the original).

List<String> s = new ArrayList<String>() {
{
add("1");
add("2");
}
};

Second solution:

List<String> s = Arrays.asList(new String[]{"1", "2"});

This solution is the best if you use Java 1.4 or before. But if you use Java 5, the third is more elegant:

List<String> s = Arrays.asList("1", "2");

Great!

EDIT: Rob Juurlink commented that this solution will create a fixed read only collection, so you may want to wrap it with ArrayList (or other Collection class) to make it writable


위 내용에 좋은 답글이 달려서 그 내용도 복사를 하였습니다.
* Christian Ullenboom

I like this trick:

List list = Arrays.asList( “1,2,3,4,5,6,7″.split(”,”) );


* CK

check out this code

JButton button = new JButton();
button.setText(”hello”);
button.setRolloverEnabled(true);
button.setOpaque(true);

JButton anotherButton = new JButton() {{
setText(”hello”);
setRolloverEnabled(true);
setOpaque(true);
}};

I think this way of coding improve code readibility. Please comment.


'java' 카테고리의 다른 글

String, StringBuffer, StringBuilder 선택 기준  (0) 2008/02/14
Generic Method 및 익셉션 처리 팁  (0) 2008/02/14
Initializing Collection  (0) 2008/02/14
Java char data type  (0) 2008/02/13
Java I/O 성능 향상을 위한 버전(JVM)별 내용  (0) 2008/02/12
Tag // ArrayList, java, list, Tip