lambda, ArrayList<Post>posts = new ArrayList<>(); 등등 외움_20260605

JAEWOONG LEE·2026년 6월 5일
post-thumbnail

아래 소스 통째로 외우려고 노력중 현재 70%

목적은 예민한 정보를 모두 갖고 있는 클래스와
그것을 도중에 필터링하려는 목적의 클래스
그리고 메인에서 람다를 이용하여 재각공한다.
일단 외운 뒤 이해를 하려함

package collection.c04practice.eg04dto2;

import java.util.ArrayList;
import java.util.List;

import java.util.stream.Collectors;

class Post
{
private int nID;
private String sTitle, sThumnailUrl, sContent;

public Post(int nID, String sTitle,String sThumnailUrl, String sContent)
{
    this.nID = nID;
    this.sTitle = sTitle;
    this.sThumnailUrl = sThumnailUrl;
    this.sContent = sContent;
}

public int getId()
{
    return nID;
}

public String getTitle()
{
    return sTitle;
}

public String getThumnailUrl()
{
    return sThumnailUrl;
}

public String getContent()
{
    return sContent;
}

}

class PostListDto
{
private String sThumbnailUrl, sTitle;

public PostListDto(String sThumbnailUrl, String sTitle)
{
    this.sThumbnailUrl = sThumbnailUrl;
    this.sTitle = sTitle;
}

public String toString()
{
    return String.format("Post{ThumbnailUrl = %s, Title = %s     }",sThumbnailUrl, sTitle);
}

}

public class Main
{
public static void main(String[] args)
{
ArrayListposts = new ArrayList<>();
posts.add(new Post(1, "a1", "//www1", "b1"));
posts.add(new Post(2, "a2", "//www1", "b1"));
posts.add(new Post(3, "a3", "//www1", "b1"));
posts.add(new Post(4, "a4", "//www1", "b1"));

    List<PostListDto> postListDtos = posts.stream()
    .map(post->new PostListDto(post.getThumnailUrl(), post.getTitle()))
    .collect(Collectors.toList());

    for(PostListDto postListDto : postListDtos)
    {
        System.out.println(postListDto);
    }
}

}

profile
just for programming test

0개의 댓글