c로 시작하는 문자열들을 필터로 처리하여 sorted메소드로 정렬한뒤, 결과를 모아서 출력
package ex14.example2;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
List<String> list = Arrays.asList("kim", "park", "lee", "choi", "chee");
List<String> sublist = list.stream(). // 스트림 생성
filter(s -> s.startsWith("c")) // 스트림 처리
.sorted() // 스트림 처리
.collect(Collectors.toList()); // 결과 생성
System.out.println(sublist); //결과 출력
}
}
// [chee, choi]
1~8중에서 짝수만을 골라내어 제곱하는 스트림 API코드
package ex14.example2;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamExample1 {
public static void main(String[] args) {
List<Integer> num = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
System.out.println("입력데이터 = " + num);
List<Integer> result = num.stream().filter(n -> {
return n % 2 == 0;
}).map(n -> {
return n * n;
}).collect(Collectors.toList());
System.out.println(result);
}
}
// 입력데이터 = [1, 2, 3, 4, 5, 6, 7, 8]
// [4, 16, 36, 64]
단어들의 리스트를 받아서 각 단어의 길이 리스트를 반환하는 코드
package ex14.example2;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamExample2 {
public static void main(String[] args) {
List<String> words = Arrays.asList("Java", "Stream", "Library");
System.out.println("입력데이터 : " + words);
List<Integer> result = words.stream()
.map(String::length).collect(Collectors.toList());
System.out.println(result);
}
}
// [4, 6, 7]
가전제품의 가격이 300만원 이상인 가전 제품의 이름을 출력하는 프로그램
package ex14.example2;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
class Product {
int id;
String name;
int price;
public Product(int id, String name, int price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
}
public class StreamTest {
public static void main(String[] args) {
List<Product> list = new ArrayList<>();
list.add(new Product(1, "NoteBook", 100));
list.add(new Product(2, "TV", 320));
list.add(new Product(3, "Washing Machine", 250));
list.add(new Product(4, "Air Conditioner", 500));
List<String> result = list.stream().
filter(product -> product.price > 300).
map(product -> product.name).
collect(Collectors.toList());
System.out.println(result);
}
}
// [TV, Air Conditioner]
package ex14;
import java.util.*;
public class StreamEx01 {
public static void main(String[] args) {
Map<String, Object> data = new HashMap<>();
data.put("name", "홍길동");
data.put("age", 20);
Map<String, Object> data2 = new HashMap<>();
data2.put("name", "장보고");
data2.put("age", 15);
Map<String, Object> data3 = new HashMap<>();
data3.put("name", "이순신");
data3.put("age", 30);
List<Map<String, Object>> arr = Arrays.asList(data, data2, data3);
List<Map<String, Object>> newArr = arr.stream().map(d -> {
int newAge = (Integer) d.get("age") - 1;
d.put("age", newAge);
return d;
}).toList();
newArr.stream().forEach(d -> {
System.out.println(d.get("age"));
});
}
}
// 19
// 14
// 29
상품 검색하기
package ex14.example2;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
class Shop {
private String s;
private int p;
private Scanner sc = new Scanner(System.in);
public void s1(List<Product2> list) {
System.out.println("상품을 검색하세요");
System.out.print("상품의 이름(*은 모든상품을 의미): ");
s = sc.next();
System.out.println();
System.out.print("상품의 가격 상한: ");
p = sc.nextInt();
System.out.println();
List<String> result;
if (s.equals("*")) {
result = list.stream()
.filter(product2 -> product2.getPrice() <= p)
.map(Product2::getName2)
.collect(Collectors.toList());
System.out.println("검색된 상품은" + result + "입니다.");
} else {
result = list.stream()
.filter(product2 -> product2.getName().equals(s))
.filter(product2 -> product2.getPrice() <= p)
.map(Product2::getName2)
.collect(Collectors.toList());
System.out.println("검색된 상품은" + result + "입니다.");
}
}
}
class Product2 {
private int id;
private String name;
private int price;
private String name2;
public Product2(int id, String name, int price, String name2) {
this.id = id;
this.name = name;
this.price = price;
this.name2 = name2;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public String getName2() {
return name2;
}
}
public class StreamTest2 {
public static void main(String[] args) {
List<Product2> list = new ArrayList<>();
list.add(new Product2(1, "컴퓨터", 150, "삼성 컴퓨터 모델 1번"));
list.add(new Product2(2, "텔레비전", 100, "삼성 텔레비전 모델 1번"));
list.add(new Product2(3, "냉장고", 200, "삼성 냉장고 모델 1번"));
list.add(new Product2(4, "컴퓨터", 120, "삼성 컴퓨터 모델 2번"));
list.add(new Product2(5, "텔레비전", 130, "삼성 텔레비전 모델 2번"));
list.add(new Product2(6, "냉장고", 200, "삼성 냉장고 모델 2번"));
list.add(new Product2(7, "컴퓨터", 210, "삼성 컴퓨터 모델 3번"));
list.add(new Product2(8, "텔레비전", 180, "삼성 텔레비전 모델 3번"));
list.add(new Product2(8, "냉장고", 250, "삼성 냉장고 모델 3번"));
List<Product2> list2 = new ArrayList<>();
list2.add(new Product2(1, "컴퓨터", 150, "삼성 컴퓨터 모델 1번"));
list2.add(new Product2(2, "텔레비전", 100, "삼성 텔레비전 모델 1번"));
list2.add(new Product2(3, "냉장고", 200, "삼성 냉장고 모델 1번"));
list2.add(new Product2(4, "컴퓨터", 120, "삼성 컴퓨터 모델 2번"));
list2.add(new Product2(5, "텔레비전", 130, "삼성 텔레비전 모델 2번"));
list2.add(new Product2(6, "냉장고", 200, "삼성 냉장고 모델 2번"));
list2.add(new Product2(7, "컴퓨터", 210, "삼성 컴퓨터 모델 3번"));
list2.add(new Product2(8, "텔레비전", 180, "삼성 텔레비전 모델 3번"));
list2.add(new Product2(8, "냉장고", 250, "삼성 냉장고 모델 3번"));
Shop sh = new Shop();
sh.s1(list2);
}
}
