본문 바로가기
Programming/Java, Spring

[Java 기초문법] collections, list, linked list, looping with iterators

by Renechoi 2022. 10. 20.

[Java 기초문법] by Professional Java Developer Career Starter: Java Foundations @ Udemy

 

 


 

Collections

 

자바에서 Collections은 object를 store하는 형식을 말한다. 

 

List, Set, Queue, Deque로 구분되고 별도로 Map이 존재한다. 

 

Colletions의 특징은 dynamic 변경이 가능하다는 점이다. 

 

And also the entire collecionts API of Java is heavily predicated on the use and knowledge of interfaces. 

 

 

 

 


 

 

Lists 

 

Lists are intended to allow us to store objects in order. 

 

List itself is an interface, and as such, cannot be created specifically a list. You need to create an instance of a class that implements the list interface. 

 

그냥 array와 collections의 리스트는 무엇이 다른가? 

 

그냥 array는 initialize size를 해야하고, size를 바꿀 수 없었다면 리스트는 can grow or shink dinamically. 

또 하나는, array는 primitive numbers를 store하지만, list는 그렇지 않다는 점이다. 

List는 actual object만을 contain 한다.

 


 

 

이 코드에서 집계하는 방식을 list를 사용해서 바꿔보자. 

 

 

 

 

List instance 생성 

while 반복문에서 add로 리스트에 넣어주기 

 

 

 

 

 

diamond symbol을 통해 type을 specify. 

 

 

List<IEmployee> employees = new ArrayList<>();


새로운 반복문을 만들어서 꺼내기 

 

meaning : take the individual employees that are inside of this employee's collecion and loop over them, and for each employee, call it as worker, and iterate over collection. 

 

 

int totalSalaries = 0;
IEmployee employee = null;
List<IEmployee> employees = new ArrayList<>();
while (peopleMat.find()) {
    employee = Employee.createEmployee(peopleMat.group());
    employees.add(employee);
}
for (IEmployee worker : employees){
    System.out.println(worker.toString());
    totalSalaries += worker.getSalary();

}

 

이렇게 바꿀 수 있는 한편, 기능상으로나 로직으로나 사실 크게 달라진 점은 없다. 

 


 

 

Linked Lists 

 

 

 

 

처음 선언된 List가 more generic 하기 때문에 여기서 ArrayList를 Linkedlist로 바꿔도 문제가 발생하지 않는다. 

 

Linkedlist가 특이한 점은 that it does not use an array internally, rather it requests individual pockets of memory for each element that we add to it. 

 

 

 

 

so not only does it store the next location in the previous location, it also stores the previous location. 

 

 

만약 이미 저장된 데이터중에 중간 값들이 삭제되어야 하면 어떨까? 

 

 

the idea is that as we're iterating ove these employees , I want to remove those names. 

 

조건문으로 일치하는 것을 포착하는 방법이 있지만 

 

 

실제 삭제 기능만을 하도록 구현할 때 terators를 사용하는 방법을 알아보자. 

 

though it's little old fashioned 

 


 

Looping with Iterators 

 

 

for (Iterator<IEmployee> it = employees.iterator(); it.hasNext();){
    IEmployee worker = it.next();
    if (worker instanceof Employee){
        Employee tmpWorker = (Employee) worker;
        if (removalNames.contains(tmpWorker.firstName)){
            it.remove();
        }
    }
}

 

이때 ctrl + alt + m 으로 바로 method화할 수도 있다. 

 

 

 

 

 

 


 

Lists의 다양한 메소드 

 

 

 

add() 메소드 사용해보기 

 

 

employees.add(0,new Programmer(("")));

 

기존에 존재하는 리스트의 맨 앞에 추가한다. 

 

 

employees.contains(employee);

 

리스트의 요소 존재여부를 boolean으로 리턴.

 

 

 

 

 

반응형