Java

자바 연습 (스트림)

이무쿤 2019. 11. 4. 22:48
반응형

 

 

public class Customer {


private int id;
private String name;
private int age;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

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

public int getCost() {
return cost;
}

public void setCost(int cost) {
this.cost = cost;
}

private int cost;

public Customer(int i, String s, int a, int c)
{
id = i;
name = s;
age = a;
cost = c;

}



}

 

 

 

import java.util.*;
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub

Customer lee = new Customer(1,"이순신",40,100);
Customer kim = new Customer(2,"김유신",20,100);
Customer ho = new Customer(3,"홍길동",13,50);


List list = new ArrayList<>();
list.add(lee);
list.add(kim);
list.add(ho);

list.stream().map(s->s.getName()).forEach(s->System.out.println(s));
int total =  list.stream().mapToInt(s->s.getCost()).sum();
System.out.println(total);
list.stream().filter(c->c.getAge()>=20).map(s->s.getName()).forEach(s->System.out.println(s));
}

}

반응형