rxjava : stream

This commit is contained in:
haerong22
2022-03-16 16:16:47 +09:00
parent 37ba93c045
commit fb89148a66
6 changed files with 162 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
package _03_stream;
import java.util.Arrays;
import java.util.List;
public class Dish {
private final String name;
private final boolean vegetarian;
private final int calories;
private final Type type;
public Dish(String name, boolean vegetarian, int calories, Type type) {
this.name = name;
this.vegetarian = vegetarian;
this.calories = calories;
this.type = type;
}
public String getName() {
return name;
}
public boolean isVegetarian() {
return vegetarian;
}
public int getCalories() {
return calories;
}
public Type getType() {
return type;
}
public enum Type {
MEAT, FISH, OTHER
}
@Override
public String toString() {
return name;
}
public static final List<Dish> menu = Arrays.asList(
new Dish("돼지고기", false, 800, Type.MEAT),
new Dish("소고기", false, 700, Type.MEAT),
new Dish("닭고기", false, 400, Type.MEAT),
new Dish("감자 튀김", true, 530, Type.OTHER),
new Dish("", true, 350, Type.OTHER),
new Dish("계절 과일", true, 120, Type.OTHER),
new Dish("피자", true, 550, Type.OTHER),
new Dish("연어", false, 450, Type.FISH)
);
}

View File

@@ -0,0 +1,28 @@
package _03_stream;
import java.util.Arrays;
public class StreamEx_01 {
public static void main(String[] args) {
String[] words = {"a", "bc", "def"};
// stream 사용 X
int count = 0;
for (String word : words) {
if (word.length() > 2) {
count++;
}
}
System.out.println("count = " + count);
// stream 사용
long streamCount = Arrays.stream(words)
.filter(word -> word.length() > 2)
.count();
System.out.println("streamCount = " + streamCount);
}
}

View File

@@ -0,0 +1,21 @@
package _03_stream;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamEx_02 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
// 스트림 생성
Stream<Integer> stream = list.stream(); // Collection
Stream<String> stringStream = Stream.of("a", "b", "c");// 배열
Stream<Double> ranStream = Stream.generate(Math::random); // 메소드 레퍼런스
IntStream intStream = new Random().ints(5); // 난수 스트림
}
}

View File

@@ -0,0 +1,26 @@
package _03_stream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class StreamEx_03 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(3, 1, 5, 4, 2);
List<Integer> sortedList = list.stream()
.sorted() // 정렬
.toList(); // 새로운 List 에 저장
System.out.println("list = " + list);
System.out.println("sortedList = " + sortedList);
String[] words = {"a", "bc", "def"};
Stream<String> stream = Arrays.stream(words);
stream.forEach(System.out::println);
// long count = stream.count(); // 재사용 불가
// System.out.println("count = " + count);
}
}

View File

@@ -0,0 +1,15 @@
package _03_stream;
import java.util.stream.Stream;
public class StreamEx_04 {
public static void main(String[] args) {
Stream<String> stringStream = Stream.of("aa", "bb", "cc", "dd", "ee");
int sum = stringStream.parallel() // 병렬 스트림으로 전환
.mapToInt(String::length).sum();
System.out.println("sum = " + sum);
}
}

View File

@@ -0,0 +1,17 @@
package _03_stream;
import java.util.List;
public class StreamEx_05 {
public static void main(String[] args) {
List<String> names = Dish.menu.stream()
.filter(dish -> dish.getCalories() > 300)
.map(Dish::getName)
.limit(3)
.toList();
System.out.println("names = " + names);
}
}