PriorityQueue를 적재적소에 사용하자

Posted by epicdev Archive : 2012. 3. 27. 21:07

데이터가 정렬된 상태로 컨테이너에 들어있어야 할 경우에는 PriorityQueue를 자주 사용한다.

PriorityQueue는 Comparable 인터페이스를 구현하는 클래스(boolean compareTo를 구현하여 priority를 매김)를 데이터로 갖거나

생성자에서 따로 Comparator를 받아서 어떻게 priority를 매길지 정할 수 있다.

나같은 경우에는 평소에 데이터들의 순차적 access가 필요 할 때 PrioirtyQueue를 자주 사용한다.

하지만 얼마전에 PriorityQueue를 남용한 잘못을 저질렀다.


PriorityQueue가 사용되기에 적절한 곳은 (내 생각)

1. 컨테이너 안의 데이터가 priority에 따라 사용 순서가 결정

2. 컨테이너의 내용이 자주 바뀜 (poll과 add가 교차적으로 빈번하게 발생함)

3. 데이터를 재사용 할 일이 별로 없음


그런데 나같은 경우는 1번의 경우만 생각하고 2, 3번의 경우는 생각하지 않아서 문제가 발생했다.

어떤 raw 데이터를 통째로 읽어와서 PriorityQueue에 저장을 해놓고, PriorityQueue에서 데이터를 순차적으로 뽑아서, 데이터들 마다 "어떤 처리"를 한 다음 다시 파일로 쓰는 작업이었다.

여기서 나는 2번째 내용을 위반하였다.

나는 그냥 raw 데이터를 읽어오는 족족 PriorityQueue에다가 넣었는데, 이는 "오로지" 나중에 순차적으로 데이터를 뽑아 쓰려고 이렇게 하였다 (1번 이유).

그런데 이 경우 2번의 경우처럼 poll과 add가 교차적으로 빈번하게 발생하지 않는다.

즉, add가 한꺼번에 연속적으로 전부 발생하고나서, poll을 계속 하면서 데이터들을 처리한다.

따라서 이 경우에는 그냥 ArrayList에다가 데이터를 전부 add한 다음 그냥 Collections.sort로 ArrayList를 정렬해서 사용하면 그만이다.

(add와 poll의 교차수행이 빈번할 경우에 ArrayList보다 PriorityQueue가 좋은 점: ArrayList를 사용할 경우 데이터를 add하게되면 ArrayList의 "정렬됨"이라는 상태가 깨지기 때문에, valid한 ordered ArrayList를 유지하려면, add를 할때마다 매번 정렬을 해주어야 한다 (혹은, poll을 요청하기 전까지 add만 하다가 poll 요청이 들어오면 정렬을 해도 된다. 하지만 교차수행이 빈번한 경우에는 매번 정렬을 해야 할 수도 있다). 물론 ordered ArrayList는 정렬된 상태이므로, ArrayList를 traverse한 다음 적절한 위치에다가 add를 해주는 것도 가능하나, 이 또한 ArrayList의 특성상 비효율적일 수가 있다.)


사실 위의 경우에서는 PriorityQueue를 사용하거나 ArrayList와 Collections.sort를 사용하거나 별다른 차이가 없다.

왜냐하면 "재사용"이 없기 때문이다. 하지만 나는 재사용이 필요하였다 (3번 조건 위반).

만약 PriorityQueue의 데이터를 하나씩 뽑아와서 while (!pq.isEmpty()) 처리한다음 파일로 써버리면

PriorityQueue에 남아있는 내용이 없기 때문에, 만약 raw 데이터의 사용이 다시 필요하다면, 파일에서 또 읽어들여야 했다.

(PriorityQueue는 정렬된 list 상태가 아니라 heap 상태라서, iterator로 PriorityQueue를 traverse하게 되면 priority순으로 데이터가 traverse되지 않는다.

priority순으로 PriorityQueue의 데이터를 access하는 방법은 오로지 poll밖에 없다!)

문제는 이 파일이 용량이 꽤나 커서 읽어들이는데 10초정도의 시간이 소요된다는 것이었다.


따라서 내가 겪은 케이스에는 PriorityQueue를 사용하는것 보다 그냥 ArrayList와 Collections.sort를 사용하는 것이 낫다.


결론

1. 도구는 적재적소에 사용해야 한다.

2. 떄론 없어보이는 도구일지라도, 있어보이는 도구보다 나을때도 있다 (특히나 있어보이는 도구가 오로지 특정 상황에서만 최고의 성능을 발휘할 때).

  

Java에서 File IO할 때의 try-catch-finally 스타일

Posted by epicdev Archive : 2012. 3. 27. 19:54

Java에서 File IO를 할 때에 필연적으로 사용해야하는 것이 try와 catch와 finally이다.

그런데 이런 try, catch, finally 들로 코드를 도배하다보면 정말 UGLY한 코드가 나오기가 쉽다.

아래의 코드가 일반적으로 가장 널리 사용되는 스타일이다.


이 코드는 소위 말하면 정말 UGLY하다고 할 수가 있다.

가독성도 떨어지고 뭔가 불필요하게 try와 catch가 들어있는것 처럼 보인다. (실상은 그렇지 않다. 다 필요하다.)

이처럼 불필요하게 "보이는" try와 catch를 없애려고 아래의 코드처럼 할 수도 있다.


이렇게 하고나면 맨 처음 코드에서 catch가 반복되는 것을 해결 할 수 있어 보인다.

물론 이렇게 하면 해결은 되지만, Java에서의 Exception을 처리할 때의 원칙(Exception들을 catch문 하나에서 한꺼번에 처리하지 않는다)에 위배된다.

즉, out.close에서 발생하는 Exception과 new FileOutputStream에서 발생하는 Exception 모두 하나의 catch문에서 처리가 되어버린다.

이를 해결하기 위해서 또한 아래처럼 코드를 짤 수도 있다.


이렇게 코드를 짜게되면 함수가 IOException을 throw하게 된다. 또한, finally 블록에서 out의 null 체크도 없어졌다.

(new FileOutPutStream에서 Exception이 발생하면 곧바로 IOException을 throw하면서 그 다음 line을 실행하지 않으므로 finally 블록에서의 out은 무조건 null이 아니다)

하지만 나같은 경우는 Exception처리를 외부로 유보하는 것을 좋아하지 않으므로 (이런 사람들이 많을것이라 본다), 개인적으로는 비추천이다.


그래서 이제 최종적으로 내가 "알고 있는 한" 가장 BEAUTIFUL한 코드를 살펴보도록 하겠다.


이 코드를 보면 closeQuietly라는 함수를 finally 블록에서 호출하고 있다.

closeQuietly라는 함수는 Closeable의 varargs 타입을 파라미터로 받아서, 받은 closeable들을 모두 닫아버린다.

이렇게 stream들을 닫는 함수를 따로 만듦으로써 코드가 훨씬 깔끔해졌다.


물론 위의 4가지 스타일 모두 사용가능한 스타일이다.

필요한 상황마다 적재적소에 사용 할 수 있다. 네 번째 스타일의 경우에는 때로는 "닭 잡는데 소 잡는 칼을 쓰는 격"이 될 수도 있다.

이 내용에 대한 프로그래머들의 의견은 http://stackoverflow.com/questions/2699209/java-io-ugly-try-finally-block에서 확인 할 수 있다.

위의 링크를 참조하자면, 4번째 스타일이 가장 낫다는 것이 보편적인 생각인 것 같다.



  
아래의 링크를 따라가시면 프로그래머 점수표(Programmer Competency Matrix)를 보실 수 있습니다.
http://www.indiangeek.net/wp-content/uploads/Programmer%20competency%20matrix.htm 

다양한 분야마다 다양한 항목이 있는데요, 저같은 경우는 대학원생이라 그런지 특정 분야만 2-3점이 나오고 나머지 분야는 0-1점이 엄청 많네요...
스스로 냉정하게 자신을 평가해 볼 수 있는 좋은 기회인 것 같습니다. 또한, 3점의 기준에 맞춰서 앞으로의 학습계획을 짜도 좋을 것 같습니다.

[업데이트 2013.03.27]
Programmer Competency Matrix의 한글 번역본은 아래의 주소에서 찾으실 수 있습니다.

 


  

Javascript 형변환 테크닉

Posted by epicdev Archive : 2012. 2. 23. 22:34
Javascript에서 형변환을 할 때 primitive type의 생성자를 사용하지 않고, 간단하게 연산자를 통해서 아래처럼 형변환이 가능하다.

  

Java에서 중첩 루프 한번에 탈출 하는법

Posted by epicdev Archive : 2012. 1. 26. 22:22
중첩 루프를 돌면서 어떠한 조건이 만족하면 탈출하는식의 코드를 자주 코딩을 하게 된다.
이런 유형은 대개 아래와 같다.

일반적으로 이런 경우에는 아래와 같은 방법으로 코딩을 하게 된다.
혹은 중첩 루프를 함수로 만들어서 if 절 안의 break를 return으로 바꿔서 한번에 루프를 탈출하는 방법을 쓰기도 한다.
C++의 경우 goto를 사용하면 더 간단하게 이 문제를 해결할 수 있다.
물론 goto는 무조건 사용하지말라고 배웠다면 이러한 방법이 꺼려지겠지만,
거의 유일하게 goto를 써도 욕을 먹지 않는 경우가 바로 아래의 경우이다.
flag에 대한 설명을 주저리 주저리 할 필요도 없고, 코드의 가독성 또한 훨씬 높아진다.
하지만 Java에서는 goto문이 없다. 즉, C++에서처럼 goto를 사용해서 중첩 루프를 탈출 할 수 없다는 것이다.
하지만 Java에서는 이와 비슷한 다른 문법적 장치가 있다.
Java에서는 위와 같이 코딩을 하면 중첩 루프를 한번에 탈출할 수 있다.
왜 이런지 이해가 되지 않는다면 아래처럼 named block을 사용했다고 생각하면 된다.
혹은
  

Eclipse에서 Shift + Enter 기능

Posted by epicdev Archive : 2012. 1. 15. 21:53
Eclipse에서 Shift + Enter를 누르면 커서가 어디에 있건 바로 아랫줄에 빈라인을 만들어주고 그 위치로 커서가 이동한다.
별것 아닌것 같은 기능이지만서도, 정말 편리하다.
이 기능이 없으면 end를 누르고 enter를 쳐야되지만 이 기능을 사용하면 오른손을 end까지 움직이지 않고 위의 동작이 가능하다. 
  

Reflection을 사용한 String Destroyer

Posted by epicdev Archive : 2011. 11. 16. 21:09
출처: http://snippets.dzone.com/posts/show/7920


출력 결과
Java World!

Note:
The original string ("Hello World") should be smaller or equal in size when compared to the new string ("Java World!") or else you will end up in ArrayIndexOufofBoundsException.

If the original string is smaller than the new string, then the new string will be truncated. In the above code if i use 'value.set("Hello World", "Java World$Rocks!".toCharArray());' then the output is 'Java World$'

If the original string is larger than the new string then you will end up in ArrayIndexOutOfBoundsException
Stack trace below:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at java.lang.String.getChars(Unknown Source)
at java.io.BufferedWriter.write(Unknown Source)
at java.io.Writer.write(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at com.test.reflection.StringDestroyer.main(StringDestroyer.java:16)

I digged into String.java source code and found that there is a variable int count declared final. Once assigned, the value for count variable cannot be changed. The System.arraycopy(in the above exception) statement uses this count variable for copying the char array to another one. 
  
출력 결과
  

Java에서 현재 스택 상황을 보는 방법

Posted by epicdev Archive : 2011. 11. 16. 20:17
출력 결과
  
읽어보고 article 새로 쓰기: http://www.ibm.com/developerworks/java/library/j-jtp01255/index.html

출처: http://stackoverflow.com/questions/2927391/whats-the-reason-i-cant-create-generic-array-types-in-java

Arrays of generic types are not allowed because they're not sound. The problem is due to the interaction of Java arrays, which are not statically sound but are dynamically checked, with generics, which are statically sound and not dynamically checked. Here is how you could exploit the loophole:


출처: http://download.oracle.com/javase/tutorial/java/generics/erasure.html

Type Erasure

When a generic type is instantiated, the compiler translates those types by a technique called type erasure — a process where the compiler removes all information related to type parameters and type arguments within a class or method. Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics.

For instance, Box<String> is translated to type Box, which is called the raw type — a raw type is a generic class or interface name without any type arguments. This means that you can't find out what type of Object a generic class is using at runtime. The following operations are not possible:

public class MyClass<E> {
    public static void myMethod(Object item) {
        if (item instanceof E) {  //Compiler error
            ...
        }
        E item2 = new E();       //Compiler error
        E[] iArray = new E[10];  //Compiler error
        E obj = (E)new Object(); //Unchecked cast warning
    }
}

The operations shown in bold are meaningless at runtime because the compiler removes all information about the actual type argument (represented by the type parameter E) at compile time.

Type erasure exists so that new code may continue to interface with legacy code. Using a raw type for any other reason is considered bad programming practice and should be avoided whenever possible.

When mixing legacy code with generic code, you may encounter warning messages similar to the following:

Note: WarningDemo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

This can happen when using an older API that operates on raw types, as shown in the following WarningDemo program:

public class WarningDemo {
    public static void main(String[] args){
        Box<Integer> bi;
        bi = createBox();
    }

    static Box createBox(){
        return new Box();
    }
}

Recompiling with -Xlint:unchecked reveals the following additional information:

WarningDemo.java:4: warning: [unchecked] unchecked conversion
found   : Box
required: Box<java.lang.Integer>
        bi = createBox();
                      ^ 
1 warning 

'Archive' 카테고리의 다른 글

Java에서 현재 스택 상황을 보는 방법  (0) 2011.11.16
Java에서 메소드명(String)으로 메소드 호출하기  (0) 2011.11.16
In Praise Of Small Code  (0) 2011.11.15
Hollywood Principle  (0) 2011.11.15
A Taxonomy for "Bad Code Smells"  (0) 2011.11.15
  
 «이전 1 2  다음»