My Blog

자바 스트림에 대해서 공부해보자 본문

자바

자바 스트림에 대해서 공부해보자

JAESG 2023. 5. 27. 01:14

 

foreach도 존재

예제

package Baekjoon;

import java.util.*;
import java.util.stream.Collectors;

class Crew{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private String address;
    private int age;

    public Crew(String name, String address, int age){
        this.name = name;
        this.address = address;
        this.age = age;
    }
}
public class test {
    public static void main(String[] args) {
        List<Crew> numbers = new ArrayList<>();
        numbers.add(new Crew("김건호","강남",20));
        numbers.add(new Crew("김성현","판교",21));
        numbers.add(new Crew("김재성","서울",24));
        numbers.add(new Crew("강철중","구리",24));
        numbers.add(new Crew("홍길동","딸기원",25));

        List<String> collect = numbers.stream()
                .filter(u -> u.getAge() > 21)
                .map(u -> u.getName() +","+ u.getAddress())
                .collect(Collectors.toList());
        System.out.println(collect);
    }
}
[김재성,서울, 강철중,구리, 홍길동,딸기원]
728x90
Comments