μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- λ°μΌλ¦¬μ½λ©
- fibonacci
- CSS
- νκ³
- Publishing
- Spring Security
- Spring Data JDBC
- κΉνλΈ
- μΈν 리μ μ΄
- λ¬Έμμ΄λ€μ§κΈ°
- κ³μ°κΈ°λ§λ€κΈ°
- spring data jpa
- CLIλͺ λ Ήμ΄
- 그리λ
- λ°±μ€μκ³ λ¦¬μ¦
- κ±°λμ κ³±
- λΆνΈμΊ ν
- μ λ€λ¦μ€
- FilterChain
- HTML
- μλ£κ΅¬μ‘°
- λ°±μλ
- μ€νλ§
- testing
- 컬λ μ νλ μμν¬
- java
- μκ³ λ¦¬μ¦
- 첫κΈμλλ¬Έμ
- νμ΄νλ‘κ·Έλλ°
- μλ°
- Today
- Total
λμ λͺ¨μ
018 | Java - Enum, Annotation, Lambda, Stream λ³Έλ¬Έ
π Enum
π€ History of Enum
- μλ‘ κ΄λ ¨λ μμ(λ³νμ§ μλ κ°, final)λ€μ μ§ν©
- νμ λμ΄ λ³νμ§ μλ λ°μ΄ν°λ₯Ό λ€λ£¨λ λ° μ¬μ©
// κ³μ
public static final int SPRING = 1;
public static final int SUMMER = 2;
public static final int AUTUMN = 3;
public static final int WINTER = 4;
// νλ μμν¬
public static final int DJANGO = 1;
public static final int SPRING = 2;
// => μ»΄νμΌμλ¬. κ³μ μ SPRINGκ³Ό μ€λ³΅ λ°μ!
interface Seasons {
int SPRING = 1, SUMMER = 2, AUTUMN = 3, WINTER = 4;
}
interface Frameworks {
int DJANGO = 1, SPRING = 2, NEST = 3, EXPRESS = 4;
}
⇒ μ€λ³΅ μμλͺ λ¬Έμ λ νΌν μ μμ§λ§ νμ μμ μ± λ¬Έμ κ° μκΉ
if(Seasons.SPRING == Frameworks.SPRING) { }
⇒ μλ―Έμ μΌλ‘ λ€λ₯Έ κ°λ μ΄μ§λ§, μ λμ λΉκ΅νλ©΄ μλ¬κ° λ°μνμ§ μμ.
μ΄κ±Έ ν΄κ²°ν΄μ£Όλ €λ©΄ μλ‘ λ€λ₯Έ κ°μ²΄λ‘ λ§λ€μ΄μ€μΌλ¨
class Seasons {
public static final Seasons SPRING = new Seasons();
public static final Seasons SUMMER = new Seasons();
public static final Seasons FALL = new Seasons();
public static final Seasons WINTER = new Seasons();
}
class Frameworks {
public static final Frameworks DJANGO = new Frameworks();
public static final Frameworks SPRING = new Frameworks();
public static final Frameworks NEST = new Frameworks();
public static final Frameworks EXPRESS = new Frameworks();
}
⇒ μμλͺ μ€λ³΅, νμ μμ μ± λ¬Έμ ν΄κ²° κ°λ₯. But, μ½λ κΈΈμ΄μ§. μ¬μ©μ μ μνμ μ΄λΌ switchλ¬Έμ μ¬μ© λΆκ°
μ΄λμ λμ¨κ² enum
enum Seasons { SPRING, SUMMER, FALL, WINTER }
enum Frameworks { DJANGO, SPRING, NEST, EXPRESS }
π€ Use Enum
μ΄κ±°νμ μ¬μ©
enum μ΄κ±°νμ΄λ¦ {μμλͺ
1, μμλͺ
2, μμλͺ
3, ...}
μμ
eums Seasons {
SPRING, //μ μκ° 0 ν λΉ
SUMMER, //μ μκ° 1 ν λΉ
AUTUMN, //μ μκ° 2 ν λΉ
WINTER //μ μκ° 3 ν λΉ
}
- μ΄κ±°νμ μ μΈλ μμμ μ κ·Όνλ λ°©λ²: μ΄κ±°νμ΄λ¦.μμλͺ
enum Seasons { SPRING, SUMMER, AUTUMN, WINTER }
public class EnumExample {
public static void main(String[] args) {
System.out.println(Seasons.SPRING); // SPRING
}
}
- μ°Έμ‘°λ³μ favouriteSeasonμ Seasons.SPRING λ΄κΈ°
enum Seasons { SPRING, SUMMER, AUTUMN, WINTER }
public class EnumExample {
public static void main(String[] args) {
Seasons favoriteSeason = Seasons.SPRING;
System.out.println(favoriteSeason); // SPRING
}
}
β Ref.
https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html
Enum (Java Platform SE 7 )
Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.) Note that for a particular enum typ
docs.oracle.com
π Annotation
π€ About Annotation
- “μ 보λ₯Ό μ λ¬ν΄μ€ λμ”μ λͺ©μ μ λ
- νλ‘κ·Έλλ° μΈμ΄μ μν₯μ λ―ΈμΉμ§ μμΌλ©° κ°λ°μ/νλ‘κ·Έλ¨μκ² μ 보 μ 곡
μ λν μ΄μ μ μν
- μ»΄νμΌλ¬μκ² μ 보 μ 곡 ⇒ λ¬Έλ² μλ¬ μ²΄ν¬
- νλ‘κ·Έλ¨ λΉλ μ μ 보 μ 곡 ⇒ μ½λ μλ μμ±
- λ°νμμκ² μ 보 μ 곡 ⇒ νΉμ κΈ°λ₯ μ€ν
μ λν μ΄μ μ μ’ λ₯
- νμ€ μ λν μ΄μ : μλ°μμ κΈ°λ³Έ μ 곡
- λ©ν μ λν μ΄μ : μ λν μ΄μ μ λΆμ΄λ μ λν μ΄μ
- μ¬μ©μ μ μ μ λν μ΄μ
π€ νμ€ μ λν
μ΄μ
(*)
@Override
- λ©μλ μμλ§ λΆμΌ μ μμ
- μ μΈν λ©μλκ° μμ ν΄λμ€μ λ©μλλ₯Ό μ€λ²λΌμ΄λ©νλ λ©μλλΌλ κ²μ μ»΄νμΌλ¬μκ² μλ €μ€
class Super {
void run() {}
}
class Sub extends Super {
@Override
void rnu() {} // μ»΄νμΌ μλ¬ - μ€ν
}
@Deprecated
- μμΌλ‘ μ¬μ©νμ§ μμ κ²μ κΆμ₯νλ νλλ λ©μλμ λΆμ
class OldClass {
@Deprecataed
int oldField;
@Deprecated
int getOldField() { return oldField; }
}
βμ λ©μλλ₯Ό μμ λ κ² λμ @deprecatedλ₯Ό μ¬μ©ν κΉ?
βμλ°λ νμ νΈνμ±μ μ€μμ μ¬κΉ. νμ λ²μ κ³Ό νΈνμ± λλ¬Έμ @deprecatedλ₯Ό μ¬μ©νλ―λ‘ μμΌλ‘λ μ¬μ©νμ§ λ§λΌκ³ μλ €μ£Όλ κ²!
@SuppressWarnings
- μ»΄νμΌ κ²½κ³ λ©μμ§λ₯Ό μ μΈμν¬ λ μ¬μ©
- @SuppressWarnings("all") // λͺ¨λ κ²½κ³ μ μΈ
- λ μ΄μμ κ²½κ³ λ₯Ό νλ²μ μ μΈ
- @SuppressWarnings({"deprecation", "unused", "null"})
@FunctionalInterface
- ν¨μν μΈν°νμ΄μ€λ₯Ό μ μΈν λ, μ»΄νμΌλ¬κ° ν¨μν μΈν°νμ΄μ€μ μ μΈμ΄ λ°λ₯΄κ² μ μΈλμλ μ§ νμΈ
- ν¨μν μΈν°νμ΄μ€: λ¨ νλμ μΆμ λ©μλλ§ κ°μ ΈμΌ ν¨
@FunctionalInterface public interface Runnable { public abstract void run (); // νλμ μΆμ λ©μλ }
- λͺ©μ : λλ€μ μ¬μ© (1:1 λ§€μΉ)
π€ λ©ν μ λν
μ΄μ
- μ λν μ΄μ μ μν μ λν μ΄μ
- μ λν μ΄μ μ μ μ©λμ λλ μ μ§ κΈ°κ°μ μ νλ λ± μ λν μ΄μ μ μ μνλ λ° μ¬μ©
@Target
- μ λν μ΄μ μ μ μ μ μ© λμμ μ§μ νλλ° μ¬μ©
- java.lang.annotation.ElementType
- μμ
import static java.lang.annotation.ElementType.*;
//importλ¬Έμ μ΄μ©νμ¬ ElementType.TYPE λμ TYPEκ³Ό κ°μ΄ μμ± κ°λ₯
@Target({FIELD, TYPE, TYPE_USE}) // μ μ©λμ: FIELD, TYPE
public @interface CustomAnnotation { } // μ μ: CustomAnnotation
@CustomAnnotation // μ μ©λμμ΄ TYPEμΈ κ²½μ°
class Main {
@CustomAnnotation // μ μ©λμμ΄ FIELDμΈ κ²½μ°
int i;
}
@Documented
- μ λν μ΄μ μ λν μ λ³΄κ° javadocμΌλ‘ μμ±ν λ¬Έμμ ν¬ν¨λλλ‘ νλ μ λν μ΄μ μ€μ
- μλ°μμ μ 곡νλ νμ€ μ λν
μ΄μ
κ³Ό λ©ν μ λν
μ΄μ
λͺ¨λ @Documentedκ° μ μ©
(@Override @SuppressWarningsλ₯Ό μ μΈ)
@Documented
@Target(ElementType.Type)
public @interface CustomAnnotation { }
@Inherited
- νμ ν΄λμ€κ° μ λν μ΄μ μ μμ ⇒ μμ ν΄λμ€μ λΆμ μ λν μ΄μ λ€μ΄ λμΌνκ² μ μ©
@Inherited // @SuperAnnotationμ΄ νμ ν΄λμ€κΉμ§ μ μ©
@interface SuperAnnotation{ }
@SuperAnnotation
class Super { }
class Sub extends Super{ } // Subμ μ λν
μ΄μ
μ΄ λΆμ κ²μΌλ‘ μΈμ
@Retention
- μ λν μ΄μ μ μ§μ μκ° κ²°μ
- μ λν
μ΄μ
μ μ§μ μ±
(μ μ§λλ κΈ°κ° μ§μ μμ±)
- SOURCE: μμ€ νμΌμ μ‘΄μ¬. ν΄λμ€ νμΌμλ μ‘΄μ¬νμ§ μμ
- CLASS: ν΄λμ€ νμΌμ μ‘΄μ¬. μ€ν μ μ¬μ©λΆκ°, κΈ°λ³Έκ°
- RUNTIME: ν΄λμ€ νμΌμ μ‘΄μ¬. μ€ν μ μ¬μ© κ°λ₯
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
//μ€λ²λΌμ΄λ©μ΄ μ λλ‘ λμλμ§ μ»΄νμΌλ¬κ° νμΈνλ μ©λ
//ν΄λμ€ νμΌμ λ¨κΈΈ νμ μμ΄ μ»΄νμΌμμλ§ νμΈνκ³ μ¬λΌμ§
public @interface Override(){ }
⇒ μ μμ μμ @Overrideλ μ»΄νμΌλ¬ μ¬μ© ν λ ⇒ μ€ν μ μ¬μ© x
@Repeatable
- μ λν μ΄μ λ°λ³΅ μ¬μ© κ°λ₯
- μ¬μ©μ νμ μ λν μ΄μ μ μ
@Repeatable(Works.class) // ToDo μ λν
μ΄μ
μ μ¬λ¬ λ² λ°λ³΅ν΄μ μΈ μ μκ² νλ€.
@interface Work{
String value();
}
- λ°λ³΅ μ¬μ©
@Work("μ½λ μ
λ°μ΄νΈ")
@Work("λ©μλ μ€λ²λΌμ΄λ©")
class Main{ }
- μ¬λ¬λ² μ μ© κ°λ₯ ⇒ νλλ‘ λ¬Άμ΄μ£Όλ λ³λ μ λν μ΄μ μμ± νμ
@interface Works { // μ¬λ¬κ°μ ToDoμ λν
μ΄μ
μ λ΄μ 컨ν
μ΄λ μ λν
μ΄μ
ToDos
Work[] value();
}
@Repeatable(Works.class) // 컨ν
μ΄λ μ λν
μ΄μ
μ§μ
@interface Work {
String value();
}
π€ μ¬μ©μ μ μ μ λν
μ΄μ
- μ¬μ©μ μ§μ μ μν΄μ μ¬μ©
@interface μ λν
μ΄μ
λͺ
{ // μΈν°νμ΄μ€ μμ @κΈ°νΈλ₯Ό λΆμ¬ μ μ
νμ
μμλͺ
(); // μ λν
μ΄μ
μμ μ μΈ
}
- java.lang.annotation μΈν°νμ΄μ€λ₯Ό μμλ°κΈ° λλ¬Έμ λ€λ₯Έ ν΄λμ€λ μΈν°νμ΄μ€λ₯Ό μμ λ°μ μ μμ
β Ref.
https://docs.oracle.com/javase/tutorial/java/annotations/index.html
Lesson: Annotations (The Java™ Tutorials > Learning the Java Language)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated
docs.oracle.com
π Lambda
π€ λλ€μ
- λ©μλλ₯Ό νλμ μμΌλ‘ ννν κ² ⇒ μ½λ κ°κ²°, λͺ ν
- ν¨μν νλ‘κ·Έλλ° κΈ°λ² μ§μ
//κΈ°μ‘΄ λ©μλ νν λ°©μ
void sayhello() {
System.out.println("HELLO!")
}
//μμ μ½λλ₯Ό λλ€μμΌλ‘ ννν μ
() -> System.out.println("HELLO!")
- νΉμ§
- λ°ννμ κ³Ό μ΄λ¦ μλ΅ κ°λ₯ ⇒ μ΅λͺ ν¨μ(anonymous function)
- λ©μλ λ°λμ μ€νλ¬Έμ΄ νλλ§ μ‘΄μ¬ν κ²½μ° μ€κ΄νΈ μλ΅ κ°λ₯
- λ§€κ°λ³μ νμ μ μΆ κ°λ₯ν κ²½μ° λ§€κ°λ³μ νμ μλ΅ κ°λ₯
- μ΅λͺ κ°μ²΄ → μ μΈκ³Ό λμμ μμ±
π€ ν¨μν μΈν°νμ΄μ€
- μλ°μμ ν¨μλ λ°λμ ν΄λμ€ μμμ μ μ ⇒ λ©μλ λ 립μ x
- λλ€μ → κ°μ²΄ ⇒ μ΄λ¦μ΄ μκΈ° λλ¬Έμ μ΅λͺ
ν΄λμ€
- μ΅λͺ
ν΄λμ€: κ°μ²΄μ μ μΈκ³Ό μμ±μ λμμ ν¨
⇒ λ¨ νλμ κ°μ²΄λ§ μμ±νκ³ λ¨ νλ²λ§ μ¬μ©λλ μΌνμ© ν΄λμ€
- μ΅λͺ
ν΄λμ€: κ°μ²΄μ μ μΈκ³Ό μμ±μ λμμ ν¨
public class LamdaEx {
public static void main(String[] args) {
/* Object obj = new Object() {
int sum(int num1, int num2) {
return num1 + num1;
}
};
*/
FunctionEx exFunction = (num1, num2) -> num1 + num2
System.out.println(exFunction.sum(10,15))
}
@FunctionalInterface // μΈν°νμ΄μ€κ° λ°λ₯΄κ² μ μλμλμ§ μ»΄νμΌλ¬κ° νμΈν μ μκ² ..
interface FunctionEx {
public abstract int sum(int num1, int num2);
}
- κΈ°μ‘΄ μΈν°νμ΄μ€ λ¬Έλ²μ νμ©νμ¬ λλ€μμ λ€λ£¨λ κ²
- κ°λ₯ν μ΄μ ? λλ€μλ κ²°κ΅ νλμ κ°μ²΄μ΄κΈ° λλ¬Έ → μΈν°νμ΄μ€μ μ μλ μΆμλ©μλ ꡬν κ°λ₯
- λλ€μ : μΈν°νμ΄μ€ λ©μλ = 1 : 1
@FunctionalInterface
public interface FunctionalInterface {
public int accept(int x, int y);
}
import static java.lang.Integer.sum;
public class FunctionalInterfaceEx {
public static void main(String[] args) throws Exception {
FunctionalInterface ex;
ex = (x, y) -> {
int result = x + y;
return result;
};
ex = (x, y) -> { return x + y; };
ex = (x, y) -> x + y;
ex = (x, y) -> sum(x, y);
}
public static int sum(int x, int y){
return x + y;
}
}
/* μΆλ ₯: λ€ 7*/
π€ λ©μλ λ νΌλ°μ€
- λλ€μμμ λΆνμν λ§€κ°λ³μλ₯Ό μ κ±°ν λ μ£Όλ‘ μ¬μ©
⇒ λλ€μμΌλ‘ κ°λ¨ν΄μ§ μ΅λͺ κ°μ²΄λ₯Ό λ! κ°λ¨νκ²! μ¬μ©
(left, right) -> Math.max(left, right);
// μμ λ©μλλ₯Ό μ°Έμ‘° λ©μλλ‘ λ³κ²½
// ν΄λμ€μ΄λ¦ :: λ©μλμ΄λ¦
Math :: max
μ μ λ©μλμ μΈμ€ν΄μ€ λ©μλ μ°Έμ‘°
- μ μ λ©μλ μ°Έμ‘°
ν΄λμ€ :: μ μ λ©μλλͺ
- μΈμ€ν΄μ€ λ©μλ μ°Έμ‘°
μ°Έμ‘°λ³μ :: μΈμ€ν΄μ€λ©μλλͺ
μμ
// Calculator.java
public class Calculator {
public static int staticMethod(int x, int y) {
return x + y;
}
public int instanceMethod(int x, int y) {
return x * y;
}
}
import java.util.function.IntBinaryOperator;
public class MethodReferences {
public static void main(String[] args) {
IntBinaryOperator operator;
/* μ μ λ©μλ
* ν΄λμ€μ΄λ¦ :: λ©μλμ΄λ¦
*/
operator = Calculator::staticMethod;
System.out.println("μ μ λ©μλ: " + operator.applyAsInt(3, 5));
/* μΈμ€ν΄μ€λ©μλ
* μΈμ€ν΄μ€μ΄λ¦ :: λ©μλμ΄λ¦
*/
Calculator calculator = new Calculator();
operator = calculator::instanceMethod;
System.out.println("μΈμ€ν΄μ€λ©μλ: " + operator.applyAsInt(3, 5));
}
}
μμ±μ μ°Έμ‘°
- μμ±μ μ°Έμ‘° == κ°μ²΄μμ±
- κ°μ²΄ μμ± & λ¦¬ν΄ λλ€μ ⇒ μμ±μ μ°Έμ‘°λ‘ λμΉ κ°λ₯
(a, b) -> { return new ν΄λμ€(a, b); };
- μμ±μ μ°Έμ‘°λ‘ νν
ν΄λμ€ :: new
β Ref.
- ν¨μν μΈν°νμ΄μ€ μ€ν νμΈ
https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
java.util.function (Java Platform SE 8 )
Interface Summary Interface Description BiConsumer Represents an operation that accepts two input arguments and returns no result. BiFunction Represents a function that accepts two arguments and produces a result. BinaryOperator Represents an operation u
docs.oracle.com
https://codechacha.com/ko/java8-functional-interface/
Java8 - ν¨μν μΈν°νμ΄μ€(Functional Interface) μ΄ν΄νκΈ°
ν¨μν μΈν°νμ΄μ€λ 1κ°μ μΆμ λ©μλλ₯Ό κ°κ³ μλ μΈν°νμ΄μ€λ₯Ό λ§ν©λλ€. Single Abstract Method(SAM)λΌκ³ λΆλ¦¬κΈ°λ ν©λλ€. ν¨μν μΈν°νμ΄μ€λ₯Ό μ¬μ©νλ μ΄μ λ μλ°μ λλ€μμ ν¨μν μΈν°ν
codechacha.com
π Stream
π€ Stream νΉμ§
- μ€νΈλ¦Ό: λ°μ΄ν°λ₯Ό μ°μμ μΌλ‘ μ λ¬νλ ν΅λ‘
- λ°°μ΄, 컬λ μ μ μ μ₯μμλ₯Ό νλμ© μ°Έμ‘°ν΄μ λλ€μμΌλ‘ μ²λ¦¬ν μ μλλ‘ ν΄μ£Όλ λ°λ³΅μ
- λ€μν λ°μ΄ν° μμ€(List, Set, Map, λ°°μ΄ λ±)λ‘λΆν° μ€νΈλ¦Όμ λ§λ€κ³ , νμ€νλ λ°©λ²μΌλ‘ λ€λ£° μ μμ
μ μΈνμΌλ‘ λ°μ΄ν° μμ€ μ²λ¦¬
- “무μ"μ μννλμ§κ° μ€μ
- λ΄λΆ λμ μ리λ₯Ό λͺ°λΌλ μ½λκ° λ¬΄μ¨ μΌμ νλμ§ μ΄ν΄ κ°λ₯
μμ
- Listμ μλ μ«μλ€ μ€ 4λ³΄λ€ ν° μ§μ ν©κ³ ꡬνκΈ°
import java.util.List;
public class ImperativeProgrammingEx{
public static void main(String[] args){
List<Integer> numbers = List.of(1, 3, 6, 7, 8, 11);
// λͺ
λ Ήν νλ‘κ·Έλλ°
int sum = 0;
for(int number : numbers) {
if(number > 4 && (number % 2 == 0)) {
sum += number;
}
}
// μ μΈν νλ‘κ·Έλλ° - μ€νΈλ¦Ό
int sum = numbers.stream()
.filter(number -> number > 4 && (number % 2 == 0))
.mapToInt(number -> number)
.sum();
}
}
λλ€μμΌλ‘ μμ μ²λ¦¬ μ½λ μ 곡
- μ€νΈλ¦Όμ΄ μ 곡νλ λλΆλΆμ μμ μ²λ¦¬ λ©μλ ⇒ ν¨μν μΈν°νμ΄μ€ λ§€κ°νμ μ κ°μ§
- ⇒ λλ€μ λλ λ©μλ μ°Έμ‘°λ₯Ό μ΄μ©ν΄μ μμ μ²λ¦¬ λ΄μ©μ λ§€κ°κ°μΌλ‘ μ λ¬ν μ μμ
stream.forEach(s -> {
String name = s.getName();
int score = s.getScore();
System.out.println(name + ": " + score);
});
λ΄λΆ λ°λ³΅μμ μ¬μ©μΌλ‘ λ³λ ¬ μ²λ¦¬μ μ©μ΄
- μΈλΆλ°λ³΅μ(external iterator)
- κ°λ°μκ° μ½λλ‘ μ§μ 컬λ μ μ μμλ₯Ό λ°λ³΅ν΄μ κ°μ Έμ€λ μ½λ ν¨ν΄
- indexλ₯Ό μ¬μ©νλ forλ¬Έ, iteratorλ₯Ό μ΄μ©νλ whileλ¬Έ
- λ΄λΆλ°λ³΅μ(internal iterator)
- 컬λ μ λ΄λΆ: μμ λ°λ³΅ / κ°λ°μ: μμ λΉ μ²λ¦¬ν΄μΌ ν μ½λλ§ μ 곡νλ μ½λ ν¨ν΄
- μ΄μ
- μμ λ°λ³΅μ 컬λ μ μκ², κ°λ°μλ μμ μ²λ¦¬ μ½λμ μ§μ€
- μμ λ°λ³΅ μμ λ³κ²½, λ©ν°μ½μ΄ CPU μ΅λν νμ©
⇒ μμλ€μ λΆλ°°μμΌ λ³λ ¬ μμ μ λμμ£Όλ―λ‘ ν¨μ¨μ μΈ μμ λ°λ³΅ κ°λ₯- λ³λ ¬ μ€νΈλ¦Ό μ¬μ©: μ€νΈλ¦Ό parallel() λ©μλ μ¬μ©

μ€κ° μ°μ°κ³Ό μ΅μ’ μ°μ°
- μ€νΈλ¦Όμ 컬λ μ
μ μμμ λν΄ μ€κ° μ°μ°κ³Ό μ΅μ’
μ°μ°μ μνν μ μμ
- μ€κ° μ°μ°: λ§€ν, νν°λ§, μ λ ¬ λ±μ μν
- μ΅μ’ μ°μ°: λ°λ³΅, μΉ΄μ΄ν , νκ· , μ΄ν© λ±μ μ§κ³ μν
- μλ₯Ό λ€μ΄ νμ κ°μ²΄λ₯Ό μμλ‘ κ°μ§λ 컬λ μ
μ΄ μλ€κ³ κ°μ ν λ,
- μ€κ° μ°μ°: νμμ μ μλ₯Ό μΆλ ₯
- μ΅μ’ μ°μ°: μ μμ νκ· κ°μ μ°μΆ
π€ νμ΄νλΌμΈ
- 리λμ
(Reduction): λλμ λ°μ΄ν°λ₯Ό κ°κ³΅ν΄ μΆμνλ κ²
- κ²°κ³Όλ¬Ό: λ°μ΄ν°μ ν©κ³, νκ· κ°, μΉ΄μ΄ν , μ΅λκ°, μ΅μκ° λ±
νμ΄νλΌμΈ
- ꡬ쑰: μ¬λ¬κ°μ μ€νΈλ¦Όμ΄ μ°κ²°
- λͺ¨λ μ€κ° μ°μ° μ€νΈλ¦Ό(μ΅μ’ μ°μ° μ μΈ)

π€ μ€νΈλ¦Ό μμ±, μ€κ° μ°μ°, μ΅μ’
μ°μ°
μ€νΈλ¦Ό μμ±
- stream(): Collection μΈν°νμ΄μ€μ μ μ
⇒ Collection μΈν°νμ΄μ€ ꡬν κ°μ²΄(List, Set λ±)λ€μ λͺ¨λ μ΄ λ©μλλ‘ μ€νΈλ¦Ό μμ± κ°λ₯ - stream() μ¬μ© ⇒ ν΄λΉ Collectionμ κ°μ²΄ μμ€λ‘ νλ Stream λ°ν
- μ€νΈλ¦Ό μ¬μ© μ μ£Όμμ
- Read-only
- Disposable
μ€κ° μ°μ°
- μ€κ° μ°μ°μ μ°μ° κ²°κ³Όλ₯Ό μ€νΈλ¦ΌμΌλ‘ λ°ννκΈ° λλ¬Έμ μ°μν΄μ μ¬λ¬ λ² μν κ°λ₯
νν°λ§ - filter(), distinct()
- filter(): μ€νΈλ¦Ό 쑰건μ λ§λ λ°μ΄ν°λ§ νν°λ§
- distinct(): μ€νΈλ¦Ό μμ μ€λ³΅ μ κ±°
λ§€ν - map()
- map(): κΈ°μ‘΄ Stream μμ → μλ‘μ΄ Stream λ체
⇒ μ€νΈλ¦Όμ μ€νΈλ¦Ό λ°ν - flatMap(): μμλ₯Ό λ체νλ 볡μ κ°μ μμλ€λ‘ ꡬμ±λ μλ‘μ΄ μ€νΈλ¦Ό 리ν΄
⇒ μ€νΈλ¦Ό λ°ν
μ λ ¬ - sorted()
- Stream μμ μ λ ¬
- νλΌλ―Έν°λ‘ Comparator λκΈ°κΈ° κ°λ₯
- μ€λ¦μ°¨μ: Comparator μΈμ μμ΄ νΈμΆ
- λ΄λ¦Όμ°¨μ: reverseOrder μ¬μ©
list.stream()
.sorted() // μ€λ¦μ°¨μ
.forEach(n -> System.out.println(n));
System.out.println();
list.stream()
.sorted(Comparator.reverseOrder()) // λ΄λ¦Όμ°¨μ
.forEach(n -> System.out.println(n));
μ°μ° κ²°κ³Ό νμΈ - peek()
- μ°μ° μ€κ°μ κ²°κ³Όλ₯Ό νμΈνμ¬ λλ²κΉ νλ €κ³ ν λ μ¬μ©
intStream
.filter(a -> a%2 ==0)
.peek(n-> System.out.println(n))
.sum();
μ΅μ’ μ°μ°
μ°μ° κ²°κ³Ό νμΈ - forEach()
- νμ΄νλΌμΈ λ§μ§λ§μμ μμλ₯Ό νλμ© μ°μ°
intStream
.filter(a -> a%2 ==0)
.forEach(n -> System.out.println(n));
λ§€μΉ - match()
- Stream μμλ€μ΄ νΉμ ν 쑰건μ μΆ©μ‘±νλμ§ κ²μ¬
- ν¨μν μΈν°νμ΄μ€ Predicateλ‘ ν΄λΉ 쑰건μ λ§μ‘±νλμ§ κ²μ¬ → boolean λ°ν
- μ’
λ₯
- allMatch(): λͺ¨λ μμλ€μ΄ λ§€κ°κ°μΌλ‘ μ£Όμ΄μ§ Predicate 쑰건μ λ§μ‘±νλμ§ κ²μ¬
- anyMatch(): μ΅μν ν κ°μ μμκ° λ§€κ°κ°μΌλ‘ μ£Όμ΄μ§ Predicate 쑰건μ λ§μ‘±νλμ§ κ²μ¬
- noneMatch(): λͺ¨λ μμλ€μ΄ λ§€κ°κ°μΌλ‘ μ£Όμ΄μ§ Predicateμ 쑰건μ λ§μ‘±νμ§ μλμ§ κ²μ¬
int[] intArr = {2,4,6};
boolean result = Arrays.stream(intArr).allMatch(a -> a % 2 == 0);
System.out.println("λͺ¨λ 2μ λ°°μμΈκ°? " + result); // true
result = Arrays.stream(intArr).anyMatch(a -> a % 3 == 0);
System.out.println("νλλΌλ 3μ λ°°μκ° μλκ°? " + result); // true
result = Arrays.stream(intArr).noneMatch(a -> a % 3 == 0);
System.out.println("3μ λ°°μκ° μλκ°? " + result); // false
κΈ°λ³Έ μ§κ³ - sum(), count(), average(), max(), min()
- μμλ€μ μ°μ°νμ¬ νλμ κ°μΌλ‘ μ°μΆνλ κ²
int[] intArr = {1,2,3,4,5};
long count = Arrays.stream(intArr).count();
System.out.println("intArrμ μ 체 μμ κ°μ: " + count);
long sum = Arrays.stream(intArr).sum();
System.out.println("intArrμ μ 체 μμ ν©: " + sum);
double avg = Arrays.stream(intArr).average().getAsDouble();
System.out.println("μ 체 μμμ νκ· κ°: " + avg);
int max = Arrays.stream(intArr).max().getAsInt();
System.out.println("μ΅λκ°: " + max);
int min = Arrays.stream(intArr).min().getAsInt();
System.out.println("μ΅μκ°: " + min);
int first = Arrays.stream(intArr).findFirst().getAsInt();
System.out.println("λ°°μ΄μ 첫λ²μ§Έ μμ: " + first);
λ€μν μ§κ³ κ²°κ³Όλ¬Ό - reduce()
- λμ νμ¬ νλλ‘ μμΆ(reduce)νλ λ°©μ
- μμ λ μμμ μ°μ° κ²°κ³Όλ₯Ό λ°νμΌλ‘ λ€μ μμμ μ°μ°
- Accumulator: κ° μμλ₯Ό κ³μ°ν μ€κ° κ²°κ³Όλ₯Ό μμ±νκΈ° μν΄ μ¬μ©
- Identity: κ³μ°μ μννκΈ° μν μ΄κΈ°κ°
- Combiner: λ³λ ¬ μ€νΈλ¦Ό(Parlallel Stream)μμ λλμ΄ κ³μ°λ κ²°κ³Όλ₯Ό νλλ‘ ν©μΉ¨
int[] intArr = {1,2,3,4,5};
long sum = Arrays.stream(intArr).sum();
System.out.println("intArrμ μ 체 μμ ν©: " + sum);
int sum1 = Arrays.stream(intArr)
.map(el -> el*2)
.reduce((a,b) -> a+b)
.getAsInt();
System.out.println("μ΄κΈ°κ° μλ reduce: " + sum1);
int sum2= Arrays.stream(intArr)
.map(el -> el*2)
.reduce(0, (a,b) -> a+b);
System.out.println("μ΄κΈ°κ° μ‘΄μ¬νλ reduce: " + sum2)
Streamμμ λ€λ₯Έ μ’ λ₯λ‘ μμ§ - collect()
- Streamμ μμλ€μ λ€λ₯Έ μ’ λ₯(List, Set, Map λ±)μ κ²°κ³Όλ‘ μμ§
- μΌλ°μ μΌλ‘ Listλ‘ Streamμ μμλ€μ λ§μ΄ μμ§
⇒ μμ£Ό μ¬μ©νλ μμ μ Collectors κ°μ²΄μμ static λ©μλλ‘ μ 곡
⇒ μνλ κ²μ΄ μμΌλ©΄ Collector μΈν°νμ΄μ€λ₯Ό μ§μ ꡬννμ¬ μ¬μ©
π€ Optional<T>
- NullPointerException(NPE)μμ null κ°μΌλ‘ μΈν΄ μλ¬κ° λ°μνλ νμμ κ°μ²΄ μ°¨μμμ ν¨μ¨μ μΌλ‘ λ°©μ§νκ³ μ λμ
- μ°μ° κ²°κ³Όλ₯Ό Optionalμ λ΄μμ λ°ννλ©΄, λ°λ‘ 쑰건문μ μμ±νμ§ μμλ NPEκ° λ°μνμ§ μλλ‘ μ½λμμ± κ°λ₯
Optional κ°μ²΄ μμ±
- of() μ¬μ©
- μ°Έμ‘°λ³μμ κ°μ΄ nullμΌ κ°λ₯μ±μ΄ μλ€λ©΄ ofNullable() μ¬μ©
Optional<String> opt1 = Optional.ofNullable(null);
Optional<String> opt2 = Optional.ofNullable("123");
Optional<String> opt3 = Optional.<String>empty(); // κΈ°λ³Έκ°μΌλ‘ μ΄κΈ°ν
λ©μλ 체μ΄λ
- μ¬λ¬ λ©μλλ₯Ό μ°κ²°ν΄μ μμ±
β Ref.
- Optional κ°μ²΄μμ μ 곡νλ μ 체 λ©μλ
https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html
Optional (Java Platform SE 8 )
A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value. Additional methods that depend on the presence or absence of a contained value are provided, such as orEl
docs.oracle.com
π μ€λμ μκ° μ‘°κ°λͺ¨μ
- λ©μλ λ νΌλ°μ€λ λλ€μμΌλ‘ κ°λ¨ν΄μ§ μ΅λͺ κ°μ²΄λ₯Ό λ κ°λ¨νκ² μ¬μ©νλ κ²μ΄λ€. κ°λ¨ν΄μ§ μ΅λͺ κ°μ²΄λ₯Ό λ! κ°λ¨!νκ²!! μΈκ°μ μμ¬μ λμ΄ μκ³ … λλ 무μ¨λ§μΈμ§ λͺ¨λ₯΄κ² κ³ … π΅π«
- I/Oλ λ΄μΌ ... γ .γ
'SEB > TIL' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
023 | Data Structure - Stack & Queue (0) | 2022.07.25 |
---|---|
019 | Java - I/O, Thread, JVM (0) | 2022.07.19 |
017 | Java - π₯ Practical | Collection Framework (0) | 2022.07.16 |
016 | Java - Generics, Collection Framework (2) | 2022.07.15 |
015 | Java (0) | 2022.07.13 |