public class Test{
public static void main(String[] args) {
{
// 기존방식
int sum = 1;
List<Integer> list = Arrays.asList(1,2,3);
for (Integer value : list) {
sum += value;
}
System.out.println("기존방식:"+sum);
}
// 스트림 방식
List<Integer> list = Arrays.asList(1,2,3);
int result = list.stream().reduce(1, (a, b) -> a+b );
System.out.println("스트림방식:"+result);
}
}
public class Test{
public static void main(String[] args) {
// Array
int[] arr = new int[]{1,2,3};
Arrays.stream(arr);
// Collection List, Set..
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Stream stream = list.stream();
}
}
public class Test{
public static void main(String[] args) {
// Array
Stream<Integer> arr = Stream.<Integer>builder().add(1).add(2).add(3).build();
arr.forEach((x)->System.out.println("value="+x));
}
}
public class Test{
public static void main(String[] args) {
// Array
List<String> list = Arrays.asList("ab", "bc", "cd", "de");
Stream<String> str = list.stream().filter(x->x.contains("b"));
str.forEach((x)->System.out.println("value="+x));
}
}
public class Test{
public static void main(String[] args) {
// Array
Stream<String> stream = Stream.generate(() -> "test").limit(10);
stream.forEach((x)->System.out.println("value="+x));
}
}
public class Test {
public static void main(String[] args) {
List<String> fruit = Arrays.asList("사과", "오렌지", "배", "배", "오렌지");
fruit.stream().distinct().forEach(System.out::println);
}
}
public class Test {
public static void main(String[] args) {
List<Integer> fruit = Arrays.asList(40, 100, -20, 4, 10);
fruit.stream().sorted().forEach(System.out::println);
fruit.stream().sorted(Comparator.reverseOrder() ).forEach(System.out::println);
}
}