# [Java] ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ์˜ Collection Interface์— ๋Œ€ํ•ด
Study Repository

[Java] ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ์˜ Collection Interface์— ๋Œ€ํ•ด

by rlaehddnd0422

Java Collection Framework

์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ๋ž€ '๋ฐ์ดํ„ฐ ๊ตฐ(๊ฐ์ฒด)์„ ์ €์žฅํ•˜๋Š” ํด๋ž˜์Šค๋“ค์„ ํ‘œ์ค€ํ™”ํ•œ ์„ค๊ณ„'๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.

์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๋‹ค์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ ํ•„์š”ํ•œ ๋‹ค์–‘ํ•˜๊ณ  ํ’๋ถ€ํ•œ ํด๋ž˜์Šค๋“ค์„ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ผ์ผํžˆ ๊ฐœ๋ฐœ์ž๊ฐ€ LinkedList์™€ ๊ฐ™์€ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ C์–ธ์–ด์ฒ˜๋Ÿผ ๊ตฌํ˜„ํ•  ํ•„์š”์—†์ด ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ์—๋Š” ์˜ค์ง ๊ฐ์ฒด๋งŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, int, double ๊ณผ ๊ฐ™์€ primitive ํƒ€์ž…์€ ์ €์žฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ primitive ํƒ€์ž…์„ ์ €์žฅํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, wrapper ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ Integer, Double ๊ฐ์ฒด๋กœ ๋ฐ•์‹ฑํ•˜์—ฌ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

 

+ '๊ฐ์ฒด๋ฅผ ๋‹ด๋Š”๋‹ค'๋Š” ์˜๋ฏธ๋Š” ์ฃผ์†Œ๊ฐ’์„ ๋‹ด๋Š”๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ, null ๋˜ํ•œ ์ €์žฅ์ด ๊ฐ€๋Šฅ

 

์žฅ์ 

  1. ์ธํ„ฐํŽ˜์ด์Šค์™€ ๋‹คํ˜•์„ฑ์„ ์ด์šฉํ•œ ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ์„ค๊ณ„๋ฅผ ํ†ตํ•ด ํ‘œ์ค€ํ™” ๋˜์–ด, ์‚ฌ์šฉ๋ฒ•์„ ์ตํžˆ๊ธฐ ํŽธ๋ฆฌํ•˜๊ณ  ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋†’์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  2. ์ด๋ฏธ ๊ตฌํ˜„๋œ API๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ, ์ƒˆ๋กœ์šด API๋ฅผ ์ตํžˆ๊ณ  ์„ค๊ณ„ํ•˜๋Š” ์‹œ๊ฐ„์ด ์ค„์–ด๋“ ๋‹ค.

Collection Framework ์ข…๋ฅ˜

์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ ๊ณ„์ธต๊ตฌ์กฐ

์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ์—๋Š” ํฌ๊ฒŒ ๋ณด๋ฉด Collection๊ณผ Map์ด ์žˆ์Šต๋‹ˆ๋‹ค. Collection ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์œ„์—๋Š” Iterable ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์กด์žฌ.


Iterable Interface

public interface Iterable<T> {

    Iterator<T> iterator(); // ์ปฌ๋ ‰์…˜์—์„œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๊ตฌํ˜„
     
    default void forEach(Consumer<? super T> action) { // ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ „์šฉ ๋ฃจํ”„ ๋ฉ”์†Œ๋“œ
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }

    default Spliterator<T> spliterator() { // ํŒŒ์ดํ”„๋ผ์ด๋‹ ๊ด€๋ จ ๋ฉ”์†Œ๋“œ
        return Spliterators.spliteratorUnknownSize(iterator(), 0);
    }
}

* Collection ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ Iterable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋Š”๋ฐ, ์ด๋Š” ๋‹จ์ง€ ๊ณตํ†ต ๋ฉ”์†Œ๋“œ(* ์ปฌ๋ ‰์…˜์„ ์ˆœํšŒํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” Iterator ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š”)๋ฅผ ๋ฝ‘์•„ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด Iterable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋„์ž…ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ, ์ƒ์† ๊ณ„์ธต๋„์—์„œ ๋ณ„ ์˜๋ฏธ๋Š” ์—†์Šต๋‹ˆ๋‹ค.


Collection Interface

Collection์€ ๋‹ค์‹œ List, Queue, Set์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ List, Queue, Set์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ณตํ†ต ๋ฉ”์†Œ๋“œ๋ฅผ ํ•˜๋‚˜๋กœ ๋ฝ‘์•„ ๋งŒ๋“  ์ธํ„ฐํŽ˜์ด์Šค๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

  • List, Queue, Set์„ ์ƒ์†ํ•˜๋Š” ์‹ค์งˆ์  ์ตœ์ƒ์œ„ ์ปฌ๋ ‰์…˜ ํƒ€์ž…์œผ๋กœ, ์ด ์„ธ ๊ฐ€์ง€ ์ธํ„ฐํŽ˜์ด์Šค๋“ค์€ ์—…์บ์ŠคํŒ…(๋‹คํ˜•์„ฑ)์œผ๋กœ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์ปฌ๋ ‰์…˜ ์ž๋ฃŒํ˜•์„ ๋ฐ›์•„ ์ž๋ฃŒ๋ฅผ ์‚ฝ์ž…,์‚ญ์ œ,ํƒ์ƒ‰์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 

 

Collection Interface method

Collection ์ธํ„ฐํŽ˜์ด์Šค๋Š” List, Set, Queue์—์„œ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋ชจ์•„ ์„ ์–ธํ•ด๋†“์€ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. 

IDE์—์„œ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
public interface Collection<E> extends Iterable<E> {

    int size() // Collection์— ์ €์žฅ๋œ ๊ฐ์ฒด์˜ ๊ฐœ์ˆ˜ ๋ฆฌํ„ด

    boolean isEmpty(); // Collection์ด ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ

    boolean contains(Object o);  
    boolean containsAll(Collection<?> c); // ์ง€์ •๋œ ๊ฐ์ฒด๋‚˜ Collection์˜ ๊ฐ์ฒด๋“ค์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ

    Object[] toArray(); // Collection์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด ๋ฐฐ์—ด(Object[])๋กœ ๋ฆฌํ„ด
    <T> T[] toArray(T[] a); // ์ง€์ •๋œ ๋ฐฐ์—ด์— Collection์˜ ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•ด์„œ ๋ฆฌํ„ด
    default <T> T[] toArray(IntFunction<T[]> generator) {
        return toArray(generator.apply(0));
    }

    boolean add(E e); 
    boolean addAll(Collection<? extends E> c); // ์ง€์ •๋œ ๊ฐ์ฒด๋‚˜ Collection์˜ ๊ฐ์ฒด๋“ค์„ ์ถ”๊ฐ€

    boolean remove(Object o); 
    boolean removeAll(Collection<?> c); // ์ง€์ •๋œ ๊ฐ์ฒด๋‚˜ Collection์˜ ๊ฐ์ฒด๋“ค์„ ์‚ญ์ œ

    boolean retainAll(Collection<?> c); // ์ง€์ •๋œ Collection์— ํฌํ•จ๋œ ๊ฐ์ฒด๋งŒ์„ ๋‚จ๊ธฐ๊ณ  ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค์€ ์‚ญ์ œํ•˜๋Š”๋ฐ, ์ด ์ž‘์—…์œผ๋กœ ์ธํ•ด Collection์˜ ๋ณ€ํ™”๊ฐ€ ์ƒ๊ธฐ๋ฉด true, else false return

    void clear(); // ์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐํ™”

    boolean equals(Object o); // ๋™์ผํ•œ Collection์ธ์ง€ ๋น„๊ต

    int hashCode(); // Collection์˜ hash code ๋ฆฌํ„ด

    Iterator<E> iterator(); // Collection์˜ Iterator๋ฅผ ์–ป์–ด์„œ ๋ฆฌํ„ด (์ƒ์œ„ Iterable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†)

    @Override
    default Spliterator<E> spliterator() { // ํŒŒ์ดํ”„๋ผ์ด๋‹ ๊ด€๋ จ ๋ฉ”์†Œ๋“œ๋กœ (์ƒ์œ„ Iterable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ๊ตฌํ˜„) 
        return Spliterators.spliterator(this, 0);
    }

    default Stream<E> stream() { // JDK 1.8 ver.
        return StreamSupport.stream(spliterator(), false);
    }

    default Stream<E> parallelStream() { // JDK 1.8 ver. 
        return StreamSupport.stream(spliterator(), true);
    }

    default boolean removeIf(Predicate<? super E> filter) { // JDK 1.8 ver. 
        Objects.requireNonNull(filter);
        boolean removed = false;
        final Iterator<E> each = iterator();
        while (each.hasNext()) {
            if (filter.test(each.next())) {
                each.remove();
                removed = true;
            }
        }
        return removed;
    }
    
    

}

์œ„ ๋ฉ”์†Œ๋“œ๋ฅผ ์™ธ์›Œ์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹ค, ํ•˜์œ„ ์ปฌ๋ ‰์…˜์—์„œ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋“ค์— ์ด๋Ÿฐ ๊ฒƒ๋“ค์ด ์žˆ๊ตฌ๋‚˜ ์ •๋„๋กœ ์•Œ์•„๋‘์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด Iterable ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์„ ์–ธํ•œ Iterator<E> iterator() ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตณ์ด Collection ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ๋™์ผํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ํ•œ ๋ฒˆ ๋” ์„ ์–ธํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”์ธก์ปจ๋ฐ, ์ปฌ๋ ‰์…˜์„ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋กœ ๊ฐ•์กฐํ•˜๊ธฐ ์œ„ํ•ด ํ•œ ๋ฒˆ๋” ๋ช…์‹œํ•ด์ค€ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค.

+ JDK 1.8๋ถ€ํ„ฐ๋Š” ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์œ„ํ•ด parallelStream, removeIf, stream, forEach ๋””ํดํŠธ ๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€๋จ.

 

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ์ด์–ด Collection ํ”„๋ ˆ์ž„์›Œํฌ์˜ List, Queue, Set์— ๋Œ€ํ•ด ์ˆœ์ฐจ์ ์œผ๋กœ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

<์ฐธ๊ณ  ์ž๋ฃŒ>

 

๐Ÿงฑ Java Collections Framework ์ข…๋ฅ˜ ๐Ÿ’ฏ ์ด์ •๋ฆฌ

Java Collection Framework ์ž๋ฐ” ์ƒˆ๋‚ด๊ธฐ๋ถ„๋“ค์€ ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ๋ผ๋Š” ๋‹จ์–ด์— ๋ญ”๊ฐ€ ๊ฑฐ์ฐฝํ•˜๊ณ  ์–ด๋ ค์šด ๋Š๋‚Œ์ด ๋“ค์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ๊ทธ๋ƒฅ ์ž๋ฃŒ ๊ตฌ์กฐ(Data Structure) ์ข…๋ฅ˜์˜ ํ˜•ํƒœ๋“ค์„ ์ž๋ฐ” ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•œ ๋ชจ์Œ์ง‘

inpa.tistory.com

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ