java2019. 9. 16. 22:57

Arrary to HashSet : 중복 제거

HashSet to ArrayList : 일일이 삽입할 필요없이 아래의 생성자를 이용하면 쉽게 다른 콜렉션 생성 가능.

 

public ArrayList(Collection<? extends E> c

 

Iterator 인스턴스에서 next() 메소드 사용 시, 값을 하나 뱉고 다음 멤버를 가리킨다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
String [] str = {"a", "b", "c", "a"};
    HashSet hs = new HashSet<>();
    for ( String s  :str ) {
        hs.add(s);
    }
     
    System.out.println(hs);
     
    ArrayList ar = new ArrayList<>(hs);
     
    Iterator i = ar.iterator();
     
    while(i.hasNext() ) {
        System.out.println( i.next());
    }

 

결과 : 

[a, b, c]
a
b
c
[a, b, c]

 

출처 : 자바의 신

'java' 카테고리의 다른 글

Thread Priority 와 Daemon Thread  (0) 2019.09.17
Runnable interface와 Thread class  (0) 2019.09.17
Generic  (0) 2019.09.16
System class 의 기본 기능 활용  (0) 2019.09.16
Deprecated, SuppressWarnings 예시  (0) 2019.09.15
Posted by easy16