관리 메뉴

LIFE & IT

기존 for문과 람다식 forEach 문의 차이점 비교 본문

웹 프로그래밍

기존 for문과 람다식 forEach 문의 차이점 비교

프린이! 2019. 9. 30. 01:43

기존 웹 서핑의 설명이 비전공자인 나에게 너무 와 닿지도 않고 하나 파고 또 파봐야 되는 양파처럼 머리가 아팠다.

그래서 웹 서핑 결과(내가 이해하는 수준) + 나의 이해도를 결합해 나만의 차이점 비교를 이해 정리한다.

물론 기초부터 차근차근 한 자 한 자 배우면 모든 말이 이해되겠지만.. 난 지금 빨리 어느 정도의 spot에 도달해야 하기 때문에 어쩔 수 없다.

 

 

테스트 결과 모두 같은 결과를 도출했고, 지금 나의 이해 방식이 맞지 않을 수 있다. 그건 나중에 수정 보안하겠음!

 


테스트 환경 : Spring STS

Java : 1.8 이상

 

기타 참고용

BoardVO.class

public class BoardVO {
		
		private Long bno;
		private String title;
		private String content;
		private String writer;
		private Date regdate;
		private Date updateDate;
		
}

BoardMapper.interface

package org.zerock.mapper;

import java.util.List;

import org.zerock.domain.BoardVO;

public interface BoardMapper {
	

	public List<BoardVO> getList();


}

BoardMapper.xml

  <mapper namespace="org.zerock.mapper.BoardMapper">

  	<select id="getList" resultType="org.zerock.domain.BoardVO">
  	  
  	<![CDATA[
  		select * from tbl_board where bno > 0
  	]]>
  	
  	</select>

▷ 기존 for 을 활용한 방식

실행문 : 
		ArrayList<BoardVO> list = (ArrayList<BoardVO>) mapper.getList();
		
		for(int i = 0 ; i < list.size() ; i++) {
			log.info(list.get(i));
		}



결과 값:

INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=14, title=new Title 1, content=new Content 1, writer=newbie, regdate=Sat Sep 28 17:45:05 KST 2019, updateDate=Sat Sep 28 17:45:05 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=15, title=new Title 1, content=new Content 1, writer=newbie, regdate=Sat Sep 28 17:55:51 KST 2019, updateDate=Sat Sep 28 17:55:51 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=19, title=BoardServieTest title 1, content=BoardServieTest content 1, writer=testNewbie, regdate=Mon Sep 30 00:05:43 KST 2019, updateDate=Mon Sep 30 00:05:43 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=2, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:28:43 KST 2019, updateDate=Wed Sep 25 17:28:43 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=4, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:43 KST 2019, updateDate=Wed Sep 25 17:29:43 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=5, title=Modify Title #1, content=Modify Content #1, writer=user00, regdate=Wed Sep 25 17:29:44 KST 2019, updateDate=Sun Sep 29 03:42:47 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=6, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:45 KST 2019, updateDate=Wed Sep 25 17:29:45 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=7, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:45 KST 2019, updateDate=Wed Sep 25 17:29:45 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=8, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:46 KST 2019, updateDate=Wed Sep 25 17:29:46 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=9, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:47 KST 2019, updateDate=Wed Sep 25 17:29:47 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=10, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:48 KST 2019, updateDate=Wed Sep 25 17:29:48 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=11, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:49 KST 2019, updateDate=Wed Sep 25 17:29:49 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=12, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:49 KST 2019, updateDate=Wed Sep 25 17:29:49 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=16, title=new Title 2 select key, content=new Content 2 select key, writer=newbie, regdate=Sat Sep 28 18:00:22 KST 2019, updateDate=Sat Sep 28 18:00:22 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=17, title=new Title 2 select key, content=new Content 2 select key, writer=newbie, regdate=Sat Sep 28 22:16:13 KST 2019, updateDate=Sat Sep 28 22:16:13 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=18, title=new Title 1, content=new Content 1, writer=newbie, regdate=Sat Sep 28 22:16:13 KST 2019, updateDate=Sat Sep 28 22:16:13 KST 2019)
INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@6536e911: startup date [Mon Sep 30 01:07:48 KST 2019]; root of context hierarchy

 

  • ArrayList<BoardVO> list = BoardVO 클래스가 가진 속성 값들로 저장하는 리스트 객체.
  • mapper.getList() = BoardVO 라는 객체 타입을 결과 값으로 가지는 메서드.
  • list.size() = list 객체 안의 저장된 수만큼의 개수. length() 함수와 비슷한 함수.

 

기존 for 문에서 for-each 문 활용한 방식

실행문:
		ArrayList<BoardVO> list = (ArrayList<BoardVO>) mapper.getList();
		
		for(BoardVO board2 : list) {
			log.info(board2);
		}


결과 값:

INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=14, title=new Title 1, content=new Content 1, writer=newbie, regdate=Sat Sep 28 17:45:05 KST 2019, updateDate=Sat Sep 28 17:45:05 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=15, title=new Title 1, content=new Content 1, writer=newbie, regdate=Sat Sep 28 17:55:51 KST 2019, updateDate=Sat Sep 28 17:55:51 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=19, title=BoardServieTest title 1, content=BoardServieTest content 1, writer=testNewbie, regdate=Mon Sep 30 00:05:43 KST 2019, updateDate=Mon Sep 30 00:05:43 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=2, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:28:43 KST 2019, updateDate=Wed Sep 25 17:28:43 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=4, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:43 KST 2019, updateDate=Wed Sep 25 17:29:43 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=5, title=Modify Title #1, content=Modify Content #1, writer=user00, regdate=Wed Sep 25 17:29:44 KST 2019, updateDate=Sun Sep 29 03:42:47 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=6, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:45 KST 2019, updateDate=Wed Sep 25 17:29:45 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=7, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:45 KST 2019, updateDate=Wed Sep 25 17:29:45 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=8, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:46 KST 2019, updateDate=Wed Sep 25 17:29:46 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=9, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:47 KST 2019, updateDate=Wed Sep 25 17:29:47 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=10, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:48 KST 2019, updateDate=Wed Sep 25 17:29:48 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=11, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:49 KST 2019, updateDate=Wed Sep 25 17:29:49 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=12, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:49 KST 2019, updateDate=Wed Sep 25 17:29:49 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=16, title=new Title 2 select key, content=new Content 2 select key, writer=newbie, regdate=Sat Sep 28 18:00:22 KST 2019, updateDate=Sat Sep 28 18:00:22 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=17, title=new Title 2 select key, content=new Content 2 select key, writer=newbie, regdate=Sat Sep 28 22:16:13 KST 2019, updateDate=Sat Sep 28 22:16:13 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=18, title=new Title 1, content=new Content 1, writer=newbie, regdate=Sat Sep 28 22:16:13 KST 2019, updateDate=Sat Sep 28 22:16:13 KST 2019)

 

board2 = list.get(i) 라고 생각하면 쉽다. BoardVO 타입의 board2 for문 지역변수

for(BoardVO board2 : list) = for(int i = 0 ; i < list.size() ; i++)

    list 의 객체에 들어있는 값을 하나씩 BoardVO 타입의  board2 에 옮긴다!+ log.info(board2)를 실행시킨다.

    + list 객체의 값이 하나도 없을 때까지!

 

 

 

▷ 람다식 forEach 문 활용 방식

실행문 :  
		mapper.getList().forEach( board -> log.info(board));
		
		
결과값:

INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=14, title=new Title 1, content=new Content 1, writer=newbie, regdate=Sat Sep 28 17:45:05 KST 2019, updateDate=Sat Sep 28 17:45:05 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=15, title=new Title 1, content=new Content 1, writer=newbie, regdate=Sat Sep 28 17:55:51 KST 2019, updateDate=Sat Sep 28 17:55:51 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=19, title=BoardServieTest title 1, content=BoardServieTest content 1, writer=testNewbie, regdate=Mon Sep 30 00:05:43 KST 2019, updateDate=Mon Sep 30 00:05:43 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=2, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:28:43 KST 2019, updateDate=Wed Sep 25 17:28:43 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=4, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:43 KST 2019, updateDate=Wed Sep 25 17:29:43 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=5, title=Modify Title #1, content=Modify Content #1, writer=user00, regdate=Wed Sep 25 17:29:44 KST 2019, updateDate=Sun Sep 29 03:42:47 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=6, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:45 KST 2019, updateDate=Wed Sep 25 17:29:45 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=7, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:45 KST 2019, updateDate=Wed Sep 25 17:29:45 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=8, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:46 KST 2019, updateDate=Wed Sep 25 17:29:46 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=9, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:47 KST 2019, updateDate=Wed Sep 25 17:29:47 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=10, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:48 KST 2019, updateDate=Wed Sep 25 17:29:48 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=11, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:49 KST 2019, updateDate=Wed Sep 25 17:29:49 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=12, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Sep 25 17:29:49 KST 2019, updateDate=Wed Sep 25 17:29:49 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=16, title=new Title 2 select key, content=new Content 2 select key, writer=newbie, regdate=Sat Sep 28 18:00:22 KST 2019, updateDate=Sat Sep 28 18:00:22 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=17, title=new Title 2 select key, content=new Content 2 select key, writer=newbie, regdate=Sat Sep 28 22:16:13 KST 2019, updateDate=Sat Sep 28 22:16:13 KST 2019)
INFO : org.zerock.mapper.BoardMapperTests - BoardVO(bno=18, title=new Title 1, content=new Content 1, writer=newbie, regdate=Sat Sep 28 22:16:13 KST 2019, updateDate=Sat Sep 28 22:16:13 KST 2019)

mapper.getList() = BoardVO 타입을 결과 값으로 가지는 객체

mapper.getList().forEach() = getList() 객체의 결과 값을 가지고 forEach 함수의 내장 for문 메서드를 실행한다.

forEach( board -> log.info(board) ) 

     getList() 함수의 결과 값을 하나씩 BoardVO 타입의 board 에 넘긴다! + log.info(board)를 실행한다.

    + getList() 결과 값이 하나도 없을 때까지!

 

 

결론적으로 앞에 있는 기존 for 문과 동일한 결과 값을 가지고 훨씬 간편하게 문장 처리를 할 수 있다는 것이 장점!

반복해서 사용하지 않으면 결코 실전에 활용하기 어렵다..

forEach 문에 대한 이해를 정확하게 해야 결과 값을 가질 수 있기 때문!

'웹 프로그래밍' 카테고리의 다른 글

Maven Local repository 개별 관리  (0) 2020.10.22
WAR ? JAR ?  (0) 2019.07.19