1~4. ๋๋ค์์ด๋? ๋๋ค์ ์์ฑํ๊ธฐ
๋๋ค์(Lambda Expression)
ํจ์๋ฅผ ๊ฐ๋จํ ์์ผ๋ก ํํํ๋ ๋ฐฉ๋ฒ
int max ( int a, int b) {
return a > b ? a : b;
}
(a, b) -> a > b ? a : b
์ต๋ช
ํจ์(์ด๋ฆ์ด ์๋ ํจ์, annonymous function)
๋ฐํ ํ์
, ํจ์ ์ด๋ฆ ์ ๊ฑฐ
ํ์ดํ ์ฝ์
ํจ์์ ๋ฉ์๋์ ์ฐจ์ด
ํจ์๋ ์ผ๋ฐ์ ์ฉ์ด, ๋ฉ์๋๋ ๊ฐ์ฒด์งํฅ๊ฐ๋
์ฉ์ด
ํจ์๋ ํด๋์ค์ ๋
๋ฆฝ์ , ๋ฉ์๋๋ ํด๋์ค์ ์ข
์์
๋๋ค์ ์์ฑํ๊ธฐ
๋ฉ์๋์ ์ด๋ฆ๊ณผ ๋ฐํํ์
์ ์ ๊ฑฐํ๊ณ โโโ ๋ฅผ ๋ธ๋ก ์์ ์ถ๊ฐ
๋ฐํ๊ฐ์ด ์๋ ๊ฒฝ์ฐ, ์์ด๋ ๊ฐ๋ง ์ ๊ณ return๋ฌธ ์๋ต ๊ฐ๋ฅ
๋งค๊ฐ๋ณ์์ ํ์
์ด ์ถ๋ก ๊ฐ๋ฅํ๋ฉด ์๋ต ๊ฐ๋ฅ
์ฃผ์์ฌํญ
๋งค๊ฐ๋ณ์๊ฐ ํ๋์ธ ๊ฒฝ์ฐ, ๊ดํธ() ์๋ต ๊ฐ๋ฅ
๋ธ๋ก ์์ ๋ฌธ์ฅ์ด ํ๋์ผ ๊ฒฝ์ฐ, ๊ดํธ{} ์๋ต ๊ฐ๋ฅ
๋๋ค์์ ์
(a, b) -> a > b ? a : b
(name, i) -> System.out. println (name + "=" + i)
x -> x * x
() -> ( int )(Math. random () * 6 )
๋๋ค์์ ์ต๋ช
๊ฐ์ฒด
๋๋ค์(์ต๋ช
๊ฐ์ฒด)์ ๋ค๋ฃจ๊ธฐ ์ํ ์ฐธ์กฐ๋ณ์ ํ์
5~6. ํจ์ํ ์ธํฐํ์ด์ค
ํจ์ํ ์ธํฐํ์ด์ค
๋จ ํ๋์ ์ถ์ ๋ฉ์๋๋ง ์ ์ธ๋ ์ธํฐํ์ด์ค
interface MyFuction {
public abstract int max ( int a , int b );
}
MyFunction f = new MyFunction () {
public int max ( int a , int b ) {
return a > b ? a : b;
}
};
ํจ์ํ ์ธํฐํ์ด์ค ํ์
์ ์ฐธ์กฐ๋ณ์๋ก ๋๋ค์์ ์ฐธ์กฐํ ์ ์์
๋จ, ํจ์ํ ์ธํฐํ์ด์ค์ ๋ฉ์๋์ ๋๋ค์์ ๋งค๊ฐ๋ณ์ ๊ฐ์์ ๋ฐํํ์
์ด ์ผ์นํด์ผ ํจ
MyFuction f = (a, b) -> a > b ? a : b;
int value = f. max ( 3 , 5 ); // ์ค์ ๋ก๋ ๋๋ค์์ด ํธ์ถ๋จ
ํจ์ํ ์ธํฐํ์ด์ค - ์์
์ต๋ช
๊ฐ์ฒด๋ฅผ ๋๋ค์์ผ๋ก ๋์ฒด
List< String > list = Arrays. asList ( "abc" , "aaa" , "bbb" , "ddd" , "aaa" );
@ FunctionalInterface
interface Comparator < T > {
int compare (T o1 , T o2 );
}
Collections. sort (list, new Comparator< String >() {
public int compare (String s1 , String s2 ) {
return s2. compareTo (s1);
}
});
Collections. sort (list, (s1, s2) -> s2. compareTo (s1));
ํจ์ํ ์ธํฐํ์ด์ค ํ์
์ ๋งค๊ฐ๋ณ์, ๋ฐํํ์
@ FunctionalInterface
interface MyFunction {
void myMethod ();
}
// ๋๋ค์์ ๋งค๊ฐ๋ณ์๋ก ํ์ฉ
void aMethod (MyFunction f) {
f. myMethod (); // MyFunction์ ์ ์๋ ๋ฉ์๋ ํธ์ถ
}
MyFunction f = () -> System.out. println ( "myMethod()" );
aMethod (f);
// ์ค์ฌ์
aMethod (() -> System.out. println ( "myMethod()" ));
ํจ์ํ ์ธํฐํ์ด์ค ํ์
์ ๋งค๊ฐ๋ณ์
// ๋๋ค์์ ๋ฐํ
MyFunction myMethod () {
MyFunction f = () -> {};
return f;
}
// ์ค์ฌ์
MyFunction myMethod () {
return () -> {};
}
ํจ์ํ ์ธํฐํ์ด์ค ํ์
์ ๋ฐํํ์
7~8. java.util.function ํจํค์ง
java.util.function ํจํค์ง
์์ฃผ ์ฌ์ฉ๋๋ ๋ค์ํ ํจ์ํ ์ธํฐํ์ด์ค ์ ๊ณต
ํจ์ํ ์ธํฐํ์ด์ค ๋ฉ์๋ ์ค๋ช
java.lang.Runnable void run() ๋งค๊ฐ๋ณ์ ์๊ณ ๋ฐํ๊ฐ๋ ์์ Supplier T get() ๋งค๊ฐ๋ณ์ ์๊ณ ๋ฐํ๊ฐ์ ์์ Consumer void accept(T t) ๋งค๊ฐ๋ณ์๋ง ์๊ณ ๋ฐํ๊ฐ ์์ Function<T, R> R apply(T t) ํ๋์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์์ ๊ฒฐ๊ณผ ๋ฐํ Predicate boolean test(T t) ํ๋์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์์ boolean์ ๋ฐํ
Supplier< Integer > f = () -> ( int )(Math. random () * 100 ) + 1 ;
Consumer< Integer > f = i -> System.out. print (i + ", " );
Predicate< Integer > f = i -> i % 2 == 0 ;
Function< Integer , Integer > f = i -> i / 10 * 10 ;
๋งค๊ฐ๋ณ์๊ฐ 2๊ฐ์ธ ํจ์ํ ์ธํฐํ์ด์ค
ํจ์ํ ์ธํฐํ์ด์ค ๋ฉ์๋ ์ค๋ช
BiConsumer<T, U> void accept(T t, U u) ๋ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ง ์๊ณ ๋ฐํ๊ฐ ์์ BiPredicate<T, U> boolean test(T t, U u) ๋ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์์ boolean์ ๋ฐํ BiFunction<T, U, R> R apply(T t, U u) ๋ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์์ ํ๋์ ๊ฒฐ๊ณผ ๋ฐํ
// ๋งค๊ฐ๋ณ์๊ฐ 3๊ฐ ์ด์์ธ ํจ์ํ ์ธํฐํ์ด์ค๊ฐ ํ์ํ๋ฉด ์ง์ ์์ฑํ์ฌ ์ฌ์ฉ
@ FunctionalInterface
interface TriFunction < T , U , V , R > {
R apply (T t , U u , V v );
}
๋งค๊ฐ๋ณ์ ํ์
๊ณผ ๋ฐํ ํ์
์ด ์ผ์นํ๋ ํจ์ํ ์ธํฐํ์ด์ค
ํจ์ํ ์ธํฐํ์ด์ค ๋ฉ์๋ ์ค๋ช
UnaryOperator T apply(T t) Function์ ์์. ๋งค๊ฐ๋ณ์์ ๊ฒฐ๊ณผ์ ํ์
์ด ๊ฐ์ BinaryOperator T apply(T t, T t) BiFunction์ ์์. ๋งค๊ฐ๋ณ์์ ๊ฒฐ๊ณผ์ ํ์
์ด ๊ฐ์
9~12. Predicate์ ๊ฒฐํฉ, CF
Predicate์ ๊ฒฐํฉ
Predicate< Integer > p = i -> i < 100 ;
Predicate< Integer > q = i -> i < 200 ;
Predicate< Integer > r = i -> i % 2 == 0 ;
Predicate< Integer > notP = p. negate (); // i >= 100
Predicate< Integer > all = notP. and (q. or (r)); // 100 <= i && i < 200 || i%2 == 0
Predicate< Integer > all2 = notP. and (q. or (r)); // 100 <= i && (i < 200 || i%2 == 0)
System.out. println (all. test ( 2 )); // true
System.out. println (all2. test ( 2 )); // false
and()
or()
negate()
๋ก ๋ Predicate๋ฅผ ํ๋๋ก ๊ฒฐํฉ
Predicate< String > p2 = Predicate. isEqual (str1);
boolean result = p2. test (str2); // str1๊ณผ str2๊ฐ ๊ฐ์์ง ๋น๊ตํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ
๋ฑ๊ฐ๋น๊ต๋ฅผ ์ํ Predicate์ ์์ฑ์๋ isEqual()๋ฅผ ์ฌ์ฉ(static ๋ฉ์๋)
์ปฌ๋ ์
ํ๋ ์์๊ณผ ํจ์ํ ์ธํฐํ์ด์ค
list. forEach (i -> System.out. print (i + ", " ));
list. removeIf (x -> x % 2 == 0 || x % 3 == 0 );
list. replaceAll (i -> i * 10 );
// map์ ๋ชจ๋ ์์๋ฅผ {k,v} ํ์์ผ๋ก ์ถ๋ ฅ
map. forEach ((k,v) -> System.out. print ( "{" + k + "," + v + "}," ));
ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ๋ ์ปฌ๋ ์
ํ๋ ์์์ ๋ฉ์๋
13~14. ๋ฉ์๋, ์์ฑ์์ ๋ฉ์๋ ์ฐธ์กฐ
๋ฉ์๋ ์ฐธ์กฐ
ํ๋์ ๋ฉ์๋๋ง ํธ์ถํ๋ ๋๋ค์์ โ๋ฉ์๋ ์ฐธ์กฐโ๋ก ๋ ๊ฐ๋จํ ํ ์ ์์
์ข
๋ฅ ๋๋ค ๋ฉ์๋ ์ฐธ์กฐ static ๋ฉ์๋ ์ฐธ์กฐ (x) โ ClassName.method(x) ClassName::method ์ธ์คํด์ค ๋ฉ์๋ ์ฐธ์กฐ (obj, x) โ obj.method(x) ClassName::method ํน์ ๊ฐ์ฒด ์ธ์คํด์ค ๋ฉ์๋ ์ฐธ์กฐ (x) โ obj.method(x) obj::method
Integer method (String s) {
return Integer. parseInt (s);
}
Function< String , Integer > f = (String s) -> Integer. parseInt (s);
Function< String , Integer > f = Integer :: parseInt;
์์ฑ์์ ๋ฉ์๋ ์ฐธ์กฐ
Supplier< MyClass > s = () -> new MyClass ();
Supplier< MyCalss > s = MyClass ::new ;
Function< Integer , MyClass > s = (i) -> new MyClass (i);
Function< Integer , MyClass > s = MyClass ::new ;
์์ฑ์์ ๋ฉ์๋ ์ฐธ์กฐ
Function< Integer , int []> f = x -> new int [x];
Function< Integer , int []> f2 = int [] ::new ;
๋ฐฐ์ด๊ณผ ๋ฉ์๋ ์ฐธ์กฐ
15~16. ์คํธ๋ฆผ, ์คํธ๋ฆผ์ ํน์ง
์คํธ๋ฆผ
stream. distinct (). limit ( 5 ). sorted (). forEach (System.out :: println)
// ----------์ค๊ฐ ์ฐ์ฐ---------- ---------์ต์ข
์ฐ์ฐ----------
๋ค์ํ ๋ฐ์ดํฐ ์์ค๋ฅผ ํ์คํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋ค๋ฃจ๊ธฐ ์ํ ๊ฒ
์คํธ๋ฆผ์ด ์ ๊ณตํ๋ ๊ธฐ๋ฅ
์ค๊ฐ ์ฐ์ฐ - ์ฐ์ฐ ๊ฒฐ๊ณผ๊ฐ ์คํธ๋ฆผ์ธ ์ฐ์ฐ. ๋ฐ๋ณต์ ์ผ๋ก ์ ์ฉ ๊ฐ๋ฅ
์ต์ข
์ฐ์ฐ - ์ฐ์ฐ ๊ฒฐ๊ณผ๊ฐ ์คํธ๋ฆผ์ด ์๋ ์ฐ์ฐ. ๋จ ํ ๋ฒ๋ง ์ ์ฉ ๊ฐ๋ฅ (์คํธ๋ฆผ์ ์์๋ฅผ ์๋ชจ)
์คํธ๋ฆผ์ ํน์ง
List< Integer > list = Arrays. asList ( 3 , 1 , 5 , 4 , 2 );
List< Integer > sortedList = list. stream (). sorted (). collect (Collectors. toList ());
System. outprintln (list); // [3, 1, 5, 4, 2]
System. outprintln (sortedList); // [1, 2, 3, 4, 5]
์คํธ๋ฆผ์ ๋ฐ์ดํฐ ์์ค๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ธฐ๋ง ํ ๋ฟ ๋ณ๊ฒฝํ์ง ์์
strStream. forEach (System.out :: println); // ๋ชจ๋ ์์๋ฅผ ํ๋ฉด์ ์ถ๋ ฅ(์ต์ข
์ฐ์ฐ)
int numOfStr = strStream. count (); // ์๋ฌ. ์คํธ๋ฆผ์ด ์ด๋ฏธ ๋ซํ์
์คํธ๋ฆผ์ Iterator ์ฒ๋ผ ์ผํ์ฉ (ํ์ ์ ๋ค์ ์คํธ๋ฆผ์ ์์ฑํด์ผ ํจ)
IntStream intStream = new Random (). int ( 1 , 46 ); // 1~45 ๋ฒ์์ ๋ฌดํ ์คํธ๋ฆผ
intStream. distinct (). limit ( 6 ). sorted () // ์ค๊ฐ ์ฐ์ฐ
. forEach (i -> System.out. print (i + "," )); // ์ต์ข
์ฐ์ฐ
์ต์ข
์ฐ์ฐ ์ ๊น์ง ์ค๊ฐ์ฐ์ฐ์ด ์ํ๋์ง ์์ - ์ง์ฐ๋ ์ฐ์ฐ
stream. forEach (System.out :: println); // forEach ๋ฉ์๋ ์์ for๋ฌธ์ด ์์
์คํธ๋ฆผ์ ์์
์ ๋ด๋ถ ๋ฐ๋ณต์ผ๋ก ์ฒ๋ฆฌํจ
Stream< String > strStream = Stream. of ( "dd" , "aaa" , "CC" , "cc" , "b" );
int sum = strStream. parallel () // ๋ณ๋ ฌ ์คํธ๋ฆผ์ผ๋ก ์ ํ(์์ฑ๋ง ๋ณ๊ฒฝ)
. mapToInt (s -> s. length ()). sum (); // ๋ชจ๋ ๋ฌธ์์ด์ ๊ธธ์ด์ ํฉ
์คํธํ์ ์์
์ ๋ณ๋ ฌ๋ก ์ฒ๋ฆฌ - ๋ณ๋ ฌ์คํธ๋ฆผ
๊ธฐ๋ณธํ ์คํธ๋ฆผ - IntStream, LongStream, DoubleStream
์คํ ๋ฐ์ฑ & ์ธ๋ฐ์ฑ์ ๋นํจ์จ์ด ์ ๊ฑฐ๋จ(Stream<Integer>
๋์ intStream ์ฌ์ฉ)
์ซ์์ ๊ด๋ จ๋ ์ ์ฉํ ๋ฉ์๋๋ฅผ Stream<T>
๋ณด๋ค ๋ ๋ง์ด ์ ๊ณต
17~22. ์คํธ๋ฆผ ๋ง๋ค๊ธฐ
์คํธ๋ฆผ ๋ง๋ค๊ธฐ - ์ปฌ๋ ์
List< Integer > list = Arrays. asList ( 1 , 2 , 3 , 4 , 5 );
Stream< Integer > intStream = list. stream (); // list๋ฅผ ์คํธ๋ฆผ์ผ๋ก ๋ณํ
// ์คํธ๋ฆผ์ ๋ชจ๋ ์์๋ฅผ ์ถ๋ ฅ
intStream. forEach (System.out :: print); // 12345
intStream. forEach (System.out :: print); // ์๋ฌ. ์คํธ๋ฆผ์ด ์ด๋ฏธ ๋ซํ
Collection ์ธํฐํ์ด์ค์ stream()์ผ๋ก ์ปฌ๋ ์
์ ์คํธ๋ฆผ์ผ๋ก ๋ณํ
์คํธ๋ฆผ ๋ง๋ค๊ธฐ - ๋ฐฐ์ด
Stream< String > strStream = Stream. of ( "a" , "b" , "c" ); // ๊ฐ๋ณ ์ธ์
Stream< String > strStream = Stream. of ( new String []{ "a" , "b" , "c" });
Stream< String > strStream = Arrays. stream ( new String []{ "a" , "b" , "c" });
Stream< String > strStream = Arrays. stream ( new String []{ "a" , "b" , "c" }, 0 , 3 );
๊ฐ์ฒด ๋ฐฐ์ด๋ก๋ถํฐ ์คํธ๋ฆผ ์์ฑํ๊ธฐ
IntStream IntStream. of ( int ... values) // Stream์ด ์๋๋ผ IntStream
IntStream IntStream. of ( int [])
IntStream Arrays. stream ( int [])
IntStream Arrays. stream ( int [] array, int startInclusive, int endExclusive)
๊ธฐ๋ณธํ ๋ฐฐ์ด๋ก๋ถํฐ ์คํธ๋ฆผ ์์ฑํ๊ธฐ
์คํธ๋ฆผ ๋ง๋ค๊ธฐ - ์์์ ์
IntStream intStream = new Random (). ints (); // ๋ฌดํ ๋์ ์คํธ๋ฆผ ์์ฑ
intStream. limit ( 5 ). forEach (System.out :: println); // 5๊ฐ์ ์์๋ง ์ถ๋ ฅ
IntStream intStream = new Random (). ints ( 5 ); // 5๊ฐ์ง๋ฆฌ ์ ํ ๋์ ์คํธ๋ฆผ ์์ฑ
IntStream intStream = new Random (). ints ( 5 , 1 , 11 ); // (1, 10)์ฌ์ด 5 ์์ ์ ํ ๋์ ์คํธ๋ฆผ
๋์๋ฅผ ์์๋ก ๊ฐ๋ ์คํธ๋ฆผ ์์ฑํ๊ธฐ
์คํธ๋ฆผ ๋ง๋ค๊ธฐ - ํน์ ๋ฒ์์ ์ ์
IntStream intStream = IntStream. range ( 1 , 5 ); // 1,2,3,4
IntStream intStream = IntStream. rangeClosed ( 1 , 5 ); // 1,2,3,4,5
ํน์ ๋ฒ์์ ์ ์๋ฅผ ์์๋ก ๊ฐ๋ ์คํธ๋ฆผ ์์ฑํ๊ธฐ (IntStream, LongStream)
์คํธ๋ฆผ ๋ง๋ค๊ธฐ - ๋๋ค์ iterate(), generate()
// iterate()๋ ์ด์ ์์๋ฅผ seed๋ก ํด์ ๋ค์ ์์๋ฅผ ๊ณ์ฐ (์ด์ ์์์ ์ข
์์ )
Stream< Integer > evenStream = Stream. iterate ( 0 , n -> n + 2 );
// generate๋ seed๋ฅผ ์ฌ์ฉํ์ง ์์ (์ด์ ์์์ ๋
๋ฆฝ์ )
Stream< Double > randomStream = Stream. generate (Math :: random);
Stream< Integer > oneStream = Stream. generate (() -> 1 );
๋๋ค์์ ์์ค๋ก ํ๋ ์คํธ๋ฆผ ์์ฑํ๊ธฐ
์คํธ๋ฆผ ๋ง๋ค๊ธฐ - ํ์ผ๊ณผ ๋น ์คํธ๋ฆผ
Stream < Path > Files. list (Path dir); // Path๋ ํ์ผ ๋๋ ๋๋ ํ ๋ฆฌ
Stream < String > Files. lines (Path dir);
Stream < String > Files. lines (Path dir, Charset cs);
Stream < String > lines (); // BufferedReader ํด๋์ค์ ๋ฉ์๋
ํ์ผ์ ์์ค๋ก ํ๋ ์คํธ๋ฆผ ์์ฑํ๊ธฐ
Stream emptyStream = Stream. empty (); // empty()๋ ๋น ์คํธ๋ฆผ์ ์์ฑํด์ ๋ฐํ
long count = emptyStream. count (); // count ๊ฐ์ 0
๋น์ด์๋ ์คํธ๋ฆผ ์์ฑํ๊ธฐ
23~25. ์คํธ๋ฆผ์ ์ฐ์ฐ
์คํธ๋ฆผ์ ์ฐ์ฐ - ์ค๊ฐ ์ฐ์ฐ
// ์ค๋ณต ์ ๊ฑฐ
Stream < T > distinct ()
// ์กฐ๊ฑด์ ์ ๋ง๋ ์์ ์ ์ธ
Stream < T > filter (Predicate < T > predicate)
// ์คํธ๋ฆผ์ ์ผ๋ถ๋ฅผ ์๋ผ๋
Stream < T > limit ( long maxSize)
// ์คํธ๋ฆผ์ ์ผ๋ถ๋ฅผ ๊ฑด๋๋
Stream < T > skip ( long n)
// ์คํธ๋ฆผ์ ์์์ ์์
์ํ
Stream < T > peek (Consumer < T > action)
// ์คํธ๋ฆผ์ ์์๋ฅผ ์ ๋ ฌ
Stream < T > sorted ()
Stream < T > sorted (Comparator < T > comparator)
// ์คํธ๋ฆผ์ ์์ ๋ณํ
Stream < R > map (Function < T,R > mapper)
DoubleStream mapToDouble (ToDoubleFunction < T > mapper)
IntStream mapToInt (ToIntFunction < T > mapper)
LongStream mapToLong (ToLongFunction < T > mapper)
Stream < R > flatMap (Function < T,Stream < R >> mapper)
DoubleStream flatMapToDouble (Function < T, DoubleStream > m)
IntStream flatMapToInt (Function < T, IntStream > m)
LongStream flatMapToLong (Function < T, LongStream > m)
์คํธ๋ฆผ์ ์ฐ์ฐ - ์ต์ข
์ฐ์ฐ
// ๊ฐ ์์์ ์ง์ ๋ ์์
์ํ
void forEach (Consumer <? super T > action)
void forEachOrdered (Consumer <? super T > action)
// ์คํธ๋ฆผ์ ์์์ ๊ฐ์ ๋ฐํ
long count ()
// ์คํธ๋ฆผ์ ์ต๋/์ต์๊ฐ ๋ฐํ
Optional < T > max (Comparator <? super T > comparator)
Optional < T > min (Comparator <? super T > comparator)
// ์คํธ๋ฆผ์ ์์ ํ๋ ๋ฐํ
Optional < T > findAny () // ์๋ฌด๊ฑฐ๋ ํ๋
Optional < T > findFirst () // ์ฒซ๋ฒ์งธ ์์
// ์ฃผ์ด์ง ์กฐ๊ฑด์ ๋ชจ๋ ์์๊ฐ ๋ง์กฑ์ํค๋์ง ํ์ธ
boolean allMatch (Predicate < T > p) // ๋ชจ๋ ๋ง์กฑํ๋์ง
boolean anyMatch (Predicate < T > p) // ํ๋๋ผ๋ ๋ง์กฑํ๋์ง
boolean noneMatch (Predicate < T > p) // ๋ชจ๋ ๋ง์กฑํ์ง ์๋์ง
// ์คํธ๋ฆผ์ ๋ชจ๋ ์์๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํ
Object [] toArray ()
A [] toArray (IntFunction <A [] > generator)
// ์คํธ๋ฆผ์ ์์๋ฅผ ํ๋์ฉ ์ค์ฌ๊ฐ๋ฉด์ ๊ณ์ฐ
Optional < T > reduce (BinaryOperator < T > accumulator)
T reduce (T identity, BinaryOperator < T > accumulator)
U reduce (U identity, BiFunction < U,T,U > accumulator BinaryOperator < T > combiner)
// ์คํธ๋ฆผ์ ์์๋ฅผ ์์ง. ์ฃผ๋ก ์์๋ฅผ ๊ทธ๋ฃนํํ๊ฑฐ๋ ๋ถํ ํ ๊ฒฐ๊ณผ๋ฅผ ์ปฌ๋ ์
์ ๋ด์ ๋ฐํ
R collect (Collector < T,A,R > collector)
R collect (Supplier < R > supplier, BiConsumer < R,T > accumulator, BiConsumer < R,R > combiner)
26~34. ์คํธ๋ฆผ์ ์ค๊ฐ ์ฐ์ฐ
์คํธ๋ฆผ์ ์ค๊ฐ ์ฐ์ฐ
IntStream intStream = IntStream. rangeClosed ( 1 , 10 ); // 12345678910
intStream. skip ( 3 ). limit ( 5 ). forEach (System.out :: print); // 45678
์คํธ๋ฆผ ์๋ฅด๊ธฐ - skip(), limit()
Int intStream = IntStream. of ( 1 , 2 , 2 , 3 , 3 , 3 , 4 , 5 , 5 , 6 );
intStream. distinct (). forEach (System.out :: print); // 123456
Int intStream = IntStream. rangeClosed ( 1 , 10 ); // 12345678910
intStream. filter (i -> i % 2 == 0 ). forEach (System.out :: print); // 246810
intStream. filter (i -> i % 2 != 0 && i % 3 != 0 ). forEach (System.out :: print);
intStream. filter (i -> i % 2 != 0 ). filter (i -> i % 3 != 0 ). forEach (System.out :: print);
์คํธ๋ฆผ ์์ ๊ฑธ๋ฌ๋ด๊ธฐ - filter(), distinct()
Stream < T > sorted () // ์คํธ๋ฆผ ์์์ ๊ธฐ๋ณธ ์ ๋ ฌ(Comparable)๋ก ์ ๋ ฌ
Stream < T > sorted (Comparator <? super T > comparator) // ์ง์ ๋ Comparator๋ก ์ ๋ ฌ
์คํธ๋ฆผ ์ ๋ ฌํ๊ธฐ - sorted()
// e.g. ํ์ผ ์คํธ๋ฆผ์์ ํ์ผ ํ์ฅ์(๋๋ฌธ์)๋ฅผ ์ค๋ณต์์ด ๋ฝ์๋ด๊ธฐ
fileStream. map (File :: getName) // Stream<File> -> Stream<String>
. filter (s -> s. indexOf ( '.' ) != - 1 ) // ํ์ฅ์๊ฐ ์๋ ๊ฒ์ ์ ์ธ
. map (s -> s. substring (s. indexOf ( '.' ) + 1 )) // Stream<String> -> Stream<String>
. map (String :: toUpperCase) // Stream<String> -> Stream<String>
. distinct () // ์ค๋ณต ์ ๊ฑฐ
. forEach (System.out :: print); // JAVABAKTXT
์คํธ๋ฆผ์ ์์ ๋ฐํํ๊ธฐ - map()
fileStream. map (File :: getName) // Stream<File> -> Stream<String>
. filter (s -> s. indexOf ( '.' ) != - 1 ) // ํ์ฅ์๊ฐ ์๋ ๊ฒ์ ์ ์ธ
. peek (s -> System.out. printf ( "filename=%s%n" , s)) // ํ์ผ๋ช
์ถ๋ ฅ
. map (s -> s. substring (s. indexOf ( '.' ) + 1 )) // Stream<String> -> Stream<String>
. peek (s -> System.out. printf ( "extension=%s%n" , s)) // ํ์ฅ์ ์ถ๋ ฅ
. forEach (System.out :: print); // JAVABAKTXT
์คํธ๋ฆผ์ ์์๋ฅผ ์๋นํ์ง ์๊ณ ์ฟ๋ณด๊ธฐ - peek()
Stream< String []> strArrStream = Stream. of (
new String []{ "abc" , "def" , "jkl" },
new String []{ "ABC" , "GHI" , "JKL" }
);
// ์คํธ๋ฆผ์ ์คํธ๋ฆผ
Stream<Stream< String >> strStreamStream = strArrStream. map (Arrays :: stream);
// ์คํธ๋ฆผ์ ์คํธ๋ฆผ -> ์คํธ๋ง ์คํธ๋ฆผ
Stream< String > strStream = strArrStream. flatMap (Arrays :: stream);
์คํธ๋ฆผ์ ์คํธ๋ฆผ์ ์คํธ๋ฆผ์ผ๋ก ๋ณํ - flatMap()
35~39. Optional<T>
Optional<T>
T ํ์
๊ฐ์ฒด์ ๋ํผํด๋์ค (๊ฐ์ ์ ์ผ๋ก null ๋ค๋ฃจ๊ธฐ)
null์ ์ง์ ๋ค๋ฃจ๋ ๊ฒ์ ์ํ
null ์ฒดํฌ ์ if๋ฌธ ํ์. ์ฝ๋๊ฐ ์ง์ ๋ถ
public final class Optional < T > {
private final T value; // T ํ์
์ ์ฐธ์กฐ๋ณ์ (๋ชจ๋ ์ข
๋ฅ์ ๊ฐ์ฒด ์ ์ฅ ๊ฐ๋ฅ)
...
}
Optional<T>
๊ฐ์ฒด ์์ฑํ๊ธฐ
String str = "abc" ;
Optional< String > optVal = Optional. of (str);
Optional< String > optVal = Optional. of ( "abc" );
Optional< String > optVal = Optional. of ( null ); // NullPointerException ๋ฐ์
Optional< String > optVal = Optional. ofNullable ( null ); // OK.
null ๋์ ๋น Optional<T>
๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์
Optional< String > optVal = null ; // ๋๋ก ์ด๊ธฐํ. ๋ฐ๋์ง X
Optional< String > optVal = Optional. < String > empty (); // ๋น ๊ฐ์ฒด๋ก ์ด๊ธฐํ
Optional<T>
๊ฐ์ฒด ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
Optional ๊ฐ์ฒด์ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ - get(), orElse(), orElseGet(), orElseThrow()
Optional< String > optVal = Optional. of ( "abc" );
String str1 = optVal. get (); // optVal์ ์ ์ฅ๋ ๊ฐ์ ๋ฐํ. null์ด๋ฉด ์์ธ
String str2 = optVal. orElse ( "" ); // optVal์ ์ ์ฅ๋ ๊ฐ์ด null์ด๋ฉด "" ๋ฐํ
String str3 = optVal. orElseGet (String ::new ); // ๋๋ค์ ์ฌ์ฉ๊ฐ๋ฅ
String str4 = optVal. orElseThrow (NullPointerException ::new ); // null์ด๋ฉด ์์ธ ๋ฐ์
isPresent() - Optional ๊ฐ์ฑ์ ๊ฐ์ด null์ด๋ฉด false, ์๋๋ฉด true ๋ฐํ
OptionalInt, OptionalLong, OptionalDouble
๊ธฐ๋ณธํ ๊ฐ์ ๊ฐ์ธ๋ ๋ํผํด๋์ค
๋น Optional ๊ฐ์ฒด์์ ๋น๊ต
OptionalInt opt = OptionalInt. of ( 0 );
OptionalInt opt2 = OptionalInt. empty ();
System.out. println (opt. isPresent ()); // true
System.out. println (opt2. isPresent ()); // false
System.out. println (opt. equals (opt2)); // false