# [Java] Collection Framework - List Interface
Study Repository

[Java] Collection Framework - List Interface

by rlaehddnd0422

List Interface

  • ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋ฉด์„œ, ์ €์žฅ์ˆœ์„œ๊ฐ€ ์œ ์ง€๋˜๋Š” ์ปฌ๋ ‰์…˜์„ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ๋ฐฐ์—ด๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ index๋ฅผ ํ†ตํ•ด ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฐฐ์—ด๊ณผ ๋‹ฌ๋ฆฌ ๋ฆฌ์ŠคํŠธ๋Š” ์ž๋ฃŒํ˜• ํฌ๊ธฐ๊ฐ€ ๊ณ ์ •๋˜์ง€ ์•Š๊ณ  ๋™์ ์œผ๋กœ ๋Š˜์–ด๋‚ฌ๋‹ค ์ค„์–ด๋“ค์—ˆ๋‹ค ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋ณ€์ ์ธ ์š”์†Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์š”์†Œ ์‚ฌ์ด์— ๋นˆ ๊ณต๊ฐ„์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์‚ฝ์ž…/์‚ญ์ œ์‹œ ๋ฐฐ์—ด ์ด๋™์ด ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค.

 

Method of List Interface

๋ฉ”์†Œ๋“œ ๊ธฐ๋Šฅ
void add(int index, Object element)
boolean addAll(int index, Collection c)
์ง€์ •๋œ index์— ๊ฐ์ฒด, ์ปฌ๋ ‰์…˜ ์ถ”๊ฐ€
Object remove(int index) ์ง€์ •๋œ index์˜ ๊ฐ์ฒด ์‚ญ์ œ ํ›„ ํ•ด๋‹น ๊ฐ์ฒด ๋ฆฌํ„ด
Object get(int index) ์ง€์ •๋œ index์˜ ๊ฐ์ฒด ๋ฆฌํ„ด
Object set(int index, Object element) ์ง€์ •๋œ index์— ๊ฐ์ฒด ์ €์žฅ 
int indexOf(Object o) ์ง€์ •๋œ ๊ฐ์ฒด์˜ index ๋ฆฌํ„ด (->)
int lastIndexOf(Object o) ์ง€์ •๋œ ๊ฐ์ฒด์˜ index ๋ฆฌํ„ด (<-)
List subList(int fromIndex, int toIndex) fromIndex ~ toIndex์˜ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด
ListIterator listIterator()

ListIterator listIterator(int index)
List์˜ ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ListIterator๋ฅผ ๋ฆฌํ„ด
void sort(Comparator c) ์ง€์ •๋œ ๋น„๊ต์ž๋กœ List๋ฅผ ์ •๋ ฌ

 

List ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด๋กœ๋Š” ArrayList, LinkedList, Stack, Vector ํด๋ž˜์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ˆœ์ฐจ์ ์œผ๋กœ ํ•˜๋‚˜์”ฉ ์•Œ์•„๋ด…์‹œ๋‹ค.


1. ArrayList class Implements List

๊ณ„์ธต ๊ตฌ์กฐ

  • ArrayList ํด๋ž˜์Šค๋Š” ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ๋งŒ๋“  ๋ฆฌ์ŠคํŠธ๋กœ, ์š”์†Œ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๊ณ , ์ €์žฅ ์ˆœ์„œ๊ฐ€ ์œ ์ง€๋œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค.
    • ArrayList๋Š” ๊ธฐ์กด์˜ Vector๋ฅผ ๊ฐœ์„ ํ•œ ๊ฒƒ์œผ๋กœ Vector์™€ ๊ตฌํ˜„ ์›๋ฆฌ์™€ ๊ธฐ๋Šฅ์ ์ธ ์ธก๋ฉด์ด ๋™์ผํ•˜์ง€๋งŒ, Vector๋Š” ๊ธฐ์กด ์†Œ์Šค์™€์˜ ํ˜ธํ™˜์„ ์œ„ํ•ด ๋‚จ๊ฒจ๋‘”๊ฒƒ์ด๋ผ, ๊ฐ€๋Šฅํ•˜๋ฉด Vector๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹ค ArrayList๋ฅผ ์‚ฌ์šฉํ•ฉ์‹œ๋‹ค.
  • ๊ฐ์ฒด๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด size๊ฐ€ ๋Š˜์–ด๋‚˜๊ณ , ์‚ญ์ œ๋˜๋ฉด size๊ฐ€ ์ค„์–ด๋“œ๋Š” ๊ฐ€๋ณ€์„ฑ์„ ์ง€๋‹™๋‹ˆ๋‹ค.
  • ์žฅ์  : ๋‹จ๋ฐฉํ–ฅ ํฌ์ธํ„ฐ ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฃŒ์— ๋Œ€ํ•œ ์กฐํšŒ(get())๋Š” ๋น ๋ฅธ ํŽธ(O(1))์ด๋ผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 
  • ๋‹จ์  : ์‚ฝ์ž…/์‚ญ์ œ ์‹œ ๋ฐฐ์—ด์˜ ์ด๋™์ด ์ผ์–ด๋‚˜๋ฏ€๋กœ ๋Š๋ฆฝ๋‹ˆ๋‹ค. (์ˆœ์ฐจ์ ์ธ ์‚ฝ์ž…/์‚ญ์ œ์˜ ๊ฒฝ์šฐ์—๋Š” O(1))
    • ์‚ฝ์ž…/์‚ญ์ œ ์œ„์น˜๋กœ ์ด๋™ํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ O(N) + ์‚ญ์ œ O(1) + ๋ฐฐ์—ด ์ด๋™์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ถ”๊ฐ€์‹œ๊ฐ„ ๋ฐœ์ƒ = O(N) + a

 

public class ArrayListTest {
    public static void main(String[] args) {
        List<Integer> l = new ArrayList<>(); // ์„ ์–ธ

        l.add(1); // ์ถ”๊ฐ€
        l.add(2); // ์ถ”๊ฐ€
        l.add(3); // ์ถ”๊ฐ€
        l.add(9); // ์ถ”๊ฐ€
        l.add(9); // ์ถ”๊ฐ€
    
        l.set(0, 3); // 0๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ๊ฐ’ 3์œผ๋กœ ๋ณ€๊ฒฝ
        l.remove(4); // ์ธ๋ฑ์Šค๋กœ ์ œ๊ฑฐ
        l.remove(Integer.valueOf(3)); // ์ปฌ๋ ‰์…˜ ๋‚ด๋ถ€๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์กฐํšŒํ•˜์—ฌ ๋™๋“ฑ(๊ฐ’์ด ๋™์ผ)ํ•œ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜๋ฉด ํ•˜๋‚˜ ์ œ๊ฑฐ

        int size = l.size(); // ํฌ๊ธฐ

        Integer getFirst = l.get(0);// 0๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๊ฐ์ฒด ๋ฆฌํ„ด
        
        boolean contains = l.contains(9); // ์š”์†Œ์˜ ์กด์žฌ ์—ฌ๋ถ€
        int i = l.indexOf(9); // ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฆฌํ„ด (์—†์œผ๋ฉด -1๋ฆฌํ„ด)


        Iterator<Integer> iterator = l.iterator(); // ์ดํ„ฐ๋ ˆ์ดํ„ฐ
        while (iterator.hasNext()) { // ์ดํ„ฐ๋ ˆ์ดํ„ฐ ์ˆœํšŒ
            System.out.println(iterator.next()); // ์ดํ„ฐ๋ ˆ์ดํ„ฐ ์ถœ๋ ฅ
        }
    }
}

2. LinkedList class Implements List

  • LinkedList๋Š” ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ๋งŒ๋“ค ์ปฌ๋ ‰์…˜์ž…๋‹ˆ๋‹ค. (๋ฐฐ์—ด์ด ์•„๋‹˜)
  • ๋ฐ์ดํ„ฐ์˜ ์ค‘๊ฐ„ ์‚ฝ์ž…, ์‚ญ์ œ๊ฐ€ ๋นˆ๋ฒˆํ•œ ๊ฒฝ์šฐ ๋น ๋ฅธ ์„ฑ๋Šฅ์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.
    • ์‚ฝ์ž…/์‚ญ์ œ ์œ„์น˜์— ๋”ฐ๋ผ ๊ทธ ์œ„์น˜๊นŒ์ง€ ์ด๋™ํ•˜๋Š” ์‹œ๊ฐ„ O(N) + ํฌ์ธํ„ฐ์˜ ์ด๋™ O(1) = O(N) 
  • ๋‹จ์  : ์ž„์˜์˜ ์š”์†Œ์— ๋Œ€ํ•œ ์ ‘๊ทผ ์„ฑ๋Šฅ์€ ์ข‹์ง€ ์•Š์€ ํŽธ์ž…๋‹ˆ๋‹ค. ( ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•œ get() search๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ์ฐจ์ ์œผ๋กœ ์ฐพ์•„์•ผ ํ•˜๋ฏ€๋กœ O(N) )
  • ์–‘๋ฐฉํ–ฅ ํฌ์ธํ„ฐ ๊ตฌ์กฐ๋กœ ๋ณธ์ธ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด์™€ ๋ณธ์ธ์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํ˜•ํƒœ
  • ์Šคํƒ, ํ, ํŠธ๋ฆฌ ๋“ฑ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ทผ๊ฐ„์ด ๋˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ
public class LinkedListTest {
    public static void main(String[] args) {
        List<String> ll = new LinkedList<>(Arrays.asList("1st", "second", "third")); // ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”
        List<String> ll2 = new LinkedList<>(); // ์„ ์–ธ

        // ์ถ”๊ฐ€
        ll.add("fourth");
        ll.add("fifth");

        // ๋ณ€๊ฒฝ
        ll.set(0, "first");

        // ์‚ญ์ œ
        ((LinkedList<String>) ll).removeFirst(); // ์ฒซ๋ฒˆ์งธ ์š”์†Œ ์‚ญ์ œ (์—…์บ์ŠคํŒ… to LinkedList)
        ((LinkedList<String>) ll).removeLast(); // ๋งˆ์ง€๋ง‰ ์š”์†Œ ์‚ญ์ œ (์—…์บ์ŠคํŒ… to LinkedList)
//        ll.remove(2); // ์ธ๋ฑ์Šค๋กœ ์‚ญ์ œ

        // ํฌ๊ธฐ
        int size = ll.size(); // ์ปฌ๋ ‰์…˜ ํฌ๊ธฐ

        // ๊ฒ€์ƒ‰
        String s = ll.get(0);// ์ธ๋ฑ์Šค๋กœ ๊ฒ€์ƒ‰
        String first = ((LinkedList<String>) ll).getFirst(); // ์ฒซ๋ฒˆ์งธ ์š”์†Œ ๋ฆฌํ„ด (์—…์บ์ŠคํŒ… to LinkedList)
        String last = ((LinkedList<String>) ll).getLast(); // ๋งˆ์ง€๋ง‰ ์š”์†Œ ๋ฆฌํ„ด (์—…์บ์ŠคํŒ… to LinkedList)


        // ์ดํ„ฐ๋ ˆ์ดํ„ฐ
        Iterator<String> iterator = ll.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

    }
}

3. Vector class Implements List

  • ArrayList์˜ ๊ตฌํ˜• ๋ฒ„์ „์œผ๋กœ ๋‚ด๋ถ€ ๊ตฌ์„ฑ์ด ๊ฑฐ์˜ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฉ”์†Œ๋“œ๊ฐ€ ๋™๊ธฐํ™”๋˜์–ด ์žˆ์–ด ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์•ˆ์ „ํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์“ฐ์ผ ์ผ์ด ๊ฑฐ์˜ ์—†๋Š” ํŽธ

4. Stack class Implements List

 

  • LIFO(Last-In First-Out) ๊ตฌ์กฐ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.
  • ๋งˆ์ง€๋ง‰์— ๋“ค์–ด์˜จ ์›์†Œ๊ฐ€ ๋จผ์ € ๋‚˜๊ฐ„๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • push(์‚ฝ์ž…) pop(์ œ๊ฑฐ)๋กœ๋งŒ ์‚ฝ์ž…/์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅ 
  • Vector class๋ฅผ ์ƒ์†ํ•˜๋ฏ€๋กœ ์ž˜ ์“ฐ์ง€ ์•Š๊ณ , Queue interface์˜ ArrayDeque๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํŽธ
public class StackTest {
    public static void main(String[] args) {
        Stack<String> s = new Stack<>();

        // ์‚ฝ์ž…
        s.push("FIRST");
        s.push("SECOND");
        s.push("THIRD");

        System.out.println(s.peek()); // THIRD

        s.pop(); // ์ œ๊ฑฐ

        Iterator<String> iterator = s.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

 


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

 

 

Java List Interface

In Java, the List interface is an ordered collection that allows us to store and access elements sequentially. It extends the Collection interface. Classes that Implement List Since List is an interface, we cannot create objects from it. In order to use fu

www.programiz.com

 

 

[Java] ์ž๋ฐ” LinkedList ์‚ฌ์šฉ๋ฒ• & ์˜ˆ์ œ ์ด์ •๋ฆฌ

LinkedList๋ž€? ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(LinkedList)๋Š” ๊ฐ ๋…ธ๋“œ๊ฐ€ ๋ฐ์ดํ„ฐ์™€ ํฌ์ธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ํ•œ ์ค„๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋ฐฉ์‹์˜ ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋…ธ๋“œ๋“ค์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๊ณ , ๋…ธ๋“œ์˜ ํฌ์ธํ„ฐ๊ฐ€ ์ด์ „ ๋…ธ

coding-factory.tistory.com

 

 

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

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

inpa.tistory.com

 

 

๐Ÿงฑ ArrayList vs LinkedList ํŠน์ง• & ์„ฑ๋Šฅ ๋น„๊ต

LinkedList vs ArrayList ํŠน์ง• ๋น„๊ต LinkedList๊ฐ€ ๊ฐ๊ธฐ ๋…ธ๋“œ๋ฅผ ๋‘๊ณ  ์ฃผ์†Œ ํฌ์ธํ„ฐ๋ฅผ ๋งํฌํ•˜๋Š” ์‹์œผ๋กœ ์ž๋ฃŒ๋ฅผ ๊ตฌ์„ฑํ•œ ์ด์œ ๋Š” ArrayList๊ฐ€ ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ์ €์žฅํ•จ์œผ๋กœ์จ ๋ฐœ์ƒํ•˜๋Š” ๋‹จ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด

inpa.tistory.com

 

[JAVA] LinkedList์˜ ๊ฐœ๋… ๋ฐ ์‚ฌ์šฉ๋ฒ•

์•ˆ๋…•ํ•˜์„ธ์š” ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” LinkedList์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค ๋ชฉ์ฐจ LinkedList๋ž€? LinkedList ์„ ์–ธํ•˜๊ธฐ LinkedList ๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ธฐ LinkedList ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ LinkedList ๊ฐ’ ์‚ญ์ œํ•˜๊ธฐ LinkedList ํฌ๊ธฐ ๊ตฌํ•˜๊ธฐ LinkedList

crazykim2.tistory.com

 

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

Study Repository

rlaehddnd0422

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