week8_jubin - GANGNAM-JAVA/JAVA-STUDY GitHub Wiki
Lambda expression
- ํจ์ ์ฒ๋ฆฌ ๋ฅ๋ ฅ
Method references
- ํจ์๋ฅผ ์ง์ ํธ์ถํ์ง ์๊ณ ์ด๋ฆ์ผ๋ก ํจ์๋ฅผ ์ฐธ์กฐ. ํ๋ผ๋ฏธํฐ๋ก ํจ์๋ฅผ ์ฌ์ฉ
Default method
- interface ๋ ๊ธฐ๋ณธ์ ์ธ method ์ ๊ตฌํ์ฒด๋ฅผ ๊ฐ์ง ์ ์๋ค.
New tools
- ์ข ์์ฑ์ ์ฐพ๊ธฐ ์ํด jdeps ์ฒ๋ผ ์๋ก์ด Complier tools ๊ณผ utility ๊ฐ ์ถ๊ฐ๋๋ค.
Stream API
- ํ์ดํ ๋ผ์ธ ์ฒ๋ฆฌ๋ฅผ ์ฉ์ดํ๊ฒํ๋ ์๋ก์ด ์คํธ๋ฆผ API.
Date Time API
- ํฅ์๋ ๋ ์ง, ์๊ฐ API
Optional
- ์ ์ ํ๊ฒ Null ๊ฐ์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ์ต๊ณ ์ ๋ฐฉ๋ฒ์ ์ค์ ์ ๋๋ค.
NaShorn, Javascript Engine
- Javascript code ๋ฅผ ์คํํ๊ธฐ ์ํ Java ๊ธฐ๋ฐ ์์ง
- ๋๋ค
- Method reference
- Stream API
- Optional
์ฐธ๊ณ : https://jamcode.tistory.com/63
-
๋๋ค์ ์ธํฐํ์ด์ค
๋๋ค : java ์์ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ๊ตฌํํ๋ ๋ฐฉ์
ํด๋์ค ์์ฑํ์ง ์๊ณ ํจ์ ํธ์ถ๋ง์ผ๋ก ๊ธฐ๋ฅ ์ํ (๋ด๋ถ ์ต๋ช ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ค๊ณ ํฉ๋๋ค)
ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ด๋ ?
: ์์ ํจ์๋ก ๊ตฌํํ๊ณ ํธ์ถ, ๋งค๊ฐ๋ณ์๋ง ์ด์ฉํ๋ ํจ์๋ก ์ธ๋ถ์ ์ฌ์ด๋ ์ดํํธ๊ฐ ์์
: ๋ณ๋ ฌ ์ฒ๋ฆฌ ๋ฑ ์์ ์ ์ธ ํ์ฅ์ฑ ์๋ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์
str -> { System.out.println(str); } str -> System.out.println(str); x, y -> return x+y; // ์๋จ x, y -> x+y; // ๋จ int max = (x, y) -> (x>=y)? x : y;
ํจ์ํ ์ธํฐํ์ด์ค: ํ ๊ฐ์ ์ถ์๋ฉ์๋๋ฅผ ๊ฐ๋ ์ธํฐํ์ด์ค ( default method ๊ฐ๋ฅ)
package lambda; interface PrintLambda{ void printMethod(String str); } public class labmdaPractice { public static void main(String[] args){ PrintLambda test1 = t -> System.out.println(t); //๊ตฌํ๋ถ test1.printMethod("print1"); print2(test1); //๊ตฌํ ๋ถ ๋๊ฒจ์ค PrintLambda test3 = print3(); test3.printMethod("print"); } public static void print2(PrintLambeda test2){ test2.printMethod("print2"); } public static PrintLambda print3(){ return s -> System.out.println( s +"3!!"); } }
-
Method reference
์ฐธ๊ณ : https://imcts.github.io/java-method-reference/
๋ฉ์๋ ์ด๋ฆ์ผ๋ก ๋๊ฒจ์ฃผ๋๋ฐฉ๋ฒ
ํด๋์ค::์ธ์คํด์ค๋ฉ์๋
์ฒซ ๋ฒ์งธ ํํ์์๋ ์ฒซ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๊ฐ ๋ฉ์๋์ ์์ ์๊ฐ ๋๊ณ , ๋๋จธ์ง ํ๋ผ๋ฏธํฐ๋ ํด๋น ๋ฉ์๋๋ก ์ ๋ฌ ๋ฉ๋๋ค.
ex) String::compareToIgnoreCase๋ (x, y) -> x.compareToIgnoreCase(y) ์ ๊ฐ์ต๋๋ค.
ํด๋์ค::์ ์ ๋ฉ์๋
๋ ๋ฒ์งธ ํํ์์๋ ๋ชจ๋ ํ๋ผ๋ฏธํฐ๊ฐ ์ ์ ๋ฉ์๋๋ก ์ ๋ฌ๋ฉ๋๋ค.
ex) Object::isNull์ x -> Object.isNull(x) ์ ๊ฐ์ต๋๋ค.
๊ฐ์ฒด::์ธ์คํด์ค๋ฉ์๋
์ธ ๋ฒ์งธ ํํ์์๋ ์ฃผ์ด์ง ๊ฐ์ฒด์์ ๋ฉ์๋๊ฐ ํธ์ถ๋๋ฉฐ ํ๋ผ๋ฏธํฐ๋ ์ธ์คํด์ค ๋ฉ์๋๋ก ์ ๋ฌ ๋ฉ๋๋ค.
ex) System.out::println์ x -> System.out.println(x)์ ๋์ผํฉ๋๋ค.
3. ์คํธ๋ฆผ API
-
์๋ฃ ๋์๊ณผ ๊ด๊ฒ์์ด ๋์ผํ ์ฐ์ฐ ์ํ
-
ํ๋ฒ ์์ฑํ๊ณ ๊ฐ์ฉํ ์คํธ๋ฆผ ์ฌ์ฉ ๋ถ๊ฐ
-
์ผ๊ด์ฑ ์๋ ์ฒ๋ฆฌ ๊ฐ๋ฅ, ๋ฐฐ์ด, ์ปฌ๋ ์ ์ ๋์ผํ ์ฐ์ฐ์ด ์ํ๋จ
-
๊ธฐ์กด ์๋ฃ๋ฅผ ๋ณ๊ฒฝํ์ง ์๋๋ค
-
์ค๊ฐ์ฐ์ฐ/ ์ต์ข ์ฐ์ฐ์ผ๋ก ๊ตฌ๋ถ๋จ
-
์ต์ข ์ฐ์ฐ์ด ์ํ๋์ด์ผ ๋ชจ๋ ์ฐ์ฐ ์ ์ฉ
- ์ค๊ฐ์ฐ์ฐ?
- filter(), map()
์คํธ๋ฆผ์์ฑ โ ์ค๊ฐ์ฐ์ฐ โ ์ต์ข ์ฐ์ฐ
sList.stream().filter(s->s.length() >= 5).forEach(s->System.out.println(s)); // customerList.stream().map( c -> c.getName() ).forEach(s->System.out.println(s));
- ์ค๊ฐ์ฐ์ฐ?
-
์๋ฃ๋ฅผ ์๋ชจํ๋ฉด์ ์ฌ์ฉํจ. ์ต์ข ์ฐ์ฐ ํ ์ฌ์ฌ์ฉ ๋ถ๊ฐ
Stream<String> stream = sList.stream();
stream.forEach(s->System.out.print(s+" "));
stream.filter() -> ์ฌ์ฌ์ฉ ๋ถ๊ฐ
- ์ต์ข
์ฐ์ฐ
-
forEach
-
count
-
sum()
-
reduce()์ฐ์ฐ : ์ง์ ์ฐ์ฐ ์ง์ ๊ฐ๋ฅ
Arrays.stream(arr).reduce(0, (a,b) -> a+b)); 0 : ์ด๊ธฐ๊ฐ a,b : ์ ๋ฌ์์ a+b : ์ํ์์
-
4. ์ต์ ๋:
https://www.tutorialspoint.com/java8/java8_optional_class.htm
optional : http://homoefficio.github.io/2019/10/03/Java-Optional-๋ฐ๋ฅด๊ฒ-์ฐ๊ธฐ/
: isPresent()-get() ๋์ orElse() , orElseGet() , orElseThrow() ์ฐ๊ธฐ
Optional<String> test = ...;
return test.orElse(null);
if(test.isPresent()){
// ใดใด
}
: optional๊ฐ์ฒด.OrElse(methodA) -- methodA๋ ์ต์ ๋์ด ๋ญ๋ ๋ฌด์กฐ๊ฑด ์คํ๋๋ค
: optional๊ฐ์ฒด.OrElseGet(Supplier) โ supplier ๋ ๊ฐ์ฒด์ ๊ฐ์ด ์์ ๋๋ง ์คํ๋๋ค.
test.orElse(Collections.emptyList());
test.orElseGet(supplier);
: ๋จ์ ๊ฐ ํ์ธ์ด๋ผ๋ฉด Optional ๋ณด๋ค๋ ๋น๊ต ๋ก์ง์ด ๋ซ๋ค
return status != null ? status: READY;
return status != null ? status : Collections.emptyList();
:JPA ์์๋ ๋น์ด์๋ ์ปฌ๋ ์ ์ผ๋ก ๋ฐํํด์ฃผ๋ฏ๋ก ์ธ ํ์๊ฐ ์๋ค.
: ํ๋๋ก ์ฐ๋ ๊ฒ์ ์ง์ํ๋ค
: ๋ฉ์๋ ์ธ์๋ก ์ฐ๋ ๊ฒ์ ์ง์ํ๋ค
: ofNull() ๊ณผ ofNullable()์ฐจ์ด
: 1. ๋์ธ๊ฑธ ํ์ ํ ๋, 2. ๋ ์ผ์๋ ์์ ๋
:Optional ๋์ OptionalInt ์ฌ์ฉ
- concat ์ด๋ + ์ฐ๋ฉด ๋ค๋ฅธ ์ฃผ์๊ฐ ๊ฐ๋ฆฌํค๊ฒ ๋จ
String test1 = new String("hi");
String test2 = new String("ใ
ใ
");
System.out.println(System.identityCode((test1));
test1 = test1.concat(test2); //or test1 = test1 += test2;
System.out.println(System.identityCode((test1)); //๋ค๋ฆ
๋ฌธ์์ด ์ถ๊ฐ, ์์ , ์ญ์ ์ ํ์ GC๊ฐ ๊ด๋ฆฌํด์ผ๋ ์์ ๊ฐ๋น์ง๊ฐ ์์ฑ ๋ผ ํ๋ฉ๋ชจ๋ฆฌ ๋ถ์กฑ์ ์ผ๊ธฐํจ
- String
final byte[] value;
-
StringBuilder / StringBuffer
byte[] value
-
String Builder:
๋๊ธฐํ ์ง์ ์ํจ
-
String Buffer:
๋๊ธฐํ ์ง์, thread safe
String: ๋ฌธ์์ด ์ฐ์ฐ์ด ์ ๊ณ ๋ฉํฐ์ฐ๋ ๋์ผ ๊ฒฝ์ฐ
StringBuilder : ๋ฌธ์์ด ์ฐ์ฐ์ด ๋ง๊ณ ๋จ์ผ์ฐ๋ ๋์ผ ๊ฒฝ์ฐ
StringBuffer : ๋ฌธ์์ด ์ฐ์ฐ์ด ๋ง๊ณ ๋ฉํฐ์ฐ๋ ๋ ํ๊ฒฝ์ผ ๊ฒฝ์ฐ (synchronized ํค์๋ ์ฌ์ฉ ๊ฐ๋ฅ)
StringBuffer -> String : toString() ์ด์ฉ