Collections-List, Queue

장석빈·2022년 6월 6일
0
post-custom-banner


-Set : 중복 엘리먼트를 포함하지 않는다.
-List : 순서가 있다.
-Queue : First In First Out
-Deque : 양방향 큐, LIFO, FIFO
-Map : 키-값 매핑, 중복 키를 허용하지않는다.

ex1)

public class ListEx1 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(5); //5삽입
        list.add(4);//4삽입
        list.add(-1);//-1삽입
        System.out.println(list); //[5,4,-1]

        list.add(2, 100);//index 2에 100 삽입
        System.out.println(list);//[5,4,100,-1]
        System.out.println("리스트의 엘리먼트 갯수 : " + list.size());

        //특정 위치의 element 찾기
        System.out.println(list.get(3));//-1

        //모든 엘리먼트 하나씩 꺼내기
        for(Integer m : list){
            System.out.printf("%d ", m);
        }
        System.out.println();
    }
}

ex2)

public class Point {
    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override
    public String toString() {
        return "Point{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}

public class ListEx2 {
    public static void main(String[] args) {

        List<Point> list = new ArrayList<>();

        //3개의 Point 객체 삽입
        list.add(new Point(2,3));
        list.add(new Point(-5,20));
        list.add(new Point(30,-8));
        System.out.println(list);//[Point{x=2, y=3}, Point{x=-5, y=20}, Point{x=30, y=-8}]

        //인덱스 1에 객체 삭제
        list.remove(1);
        System.out.println(list);//[Point{x=2, y=3}, Point{x=30, y=-8}]

        //특정 위치의 element 찾기
        Point p1 = list.get(1);
        System.out.println(p1);

        //모든 element 꺼내기
        for(Point p : list){
            System.out.println(p);
        }
    }
}

ex3)

public class DequeEx {
    public static void main(String[] args) {

        //1. Queue 테스트
        Queue<String> queue = new ArrayDeque<>();//First In First Out
        queue.offer("Seoul");
        queue.offer("Busan");
        queue.offer("LA");
        System.out.println(queue);//[Seoul, Busan, LA]

        System.out.println(queue.poll());//Seoul
        System.out.println(queue.poll());//Busan
        System.out.println(queue);//[LA]

        //2,Deque 테스트 1
        Deque<String> stack = new ArrayDeque<>();//양방향으로 넣고 뺄 수 있음.
        stack.offer("Seoul");
        stack.offer("Busan");
        stack.offer("LA");
        System.out.println(stack);//[Seoul, Busan, LA]

        System.out.println(stack.pollLast());//LA
        System.out.println(stack.pollLast());//Busan
        System.out.println(stack);//[Seoul]

        //3. Deque 테스트 2
        Deque<String> deque = new ArrayDeque<>();
        deque.offer("Seoul");
        deque.offer("Busan");
        deque.offerFirst("LA");
        System.out.println(deque);//[LA, Seoul, Busan]

        System.out.println(deque.poll());//LA
        System.out.println(deque.pollLast());//Busan
        System.out.println(deque);//[Seoul]

    }
}
profile
회피형 인간의 개과천선기
post-custom-banner

0개의 댓글