스트림 관련 코딩
package chapter12;
import java.util.ArrayList;
import java.util.List;
public class TravelTest {
public static void main(String[] args) {
TravelCustomer customerLee = new TravelCustomer("이순신",40,100);
TravelCustomer customerKim = new TravelCustomer("김유신",20,100);
TravelCustomer customerHong = new TravelCustomer("홍길동",13,50);
List<TravelCustomer> customerList = new ArrayList<TravelCustomer>();
customerList.add(customerLee);
customerList.add(customerHong);
customerList.add(customerKim);
System.out.println(customerList);
customerList.stream().map(c->c.getName()).forEach(s->System.out.println(s));
int total = customerList.stream().mapToInt(c->c.getPrice()).sum();
System.out.println(total);
customerList.stream().filter(c->c.getAge()>=20).map(c->c.getName()).sorted().forEach(s->System.out.println(s));
}
}
package chapter12;
public class TravelCustomer {
private String name;
private int age;
private int price;
public TravelCustomer(String name, int age, int price) {
this.name=name;
this.age=age;
this.price=price;
}
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 getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String toString() {
return name+","+age+","+price;
}
}
반응형
'JAVA' 카테고리의 다른 글
Multi-Thread 프로그래밍 (0) | 2019.10.17 |
---|---|
예외처리 (0) | 2019.10.17 |
스트림(Stream) (0) | 2019.10.15 |
람다식 (0) | 2019.10.14 |
내부클래스 (0) | 2019.10.13 |