--- id: java-lambda title: "Java Lambda" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["lambda expressions", "μžλ°” λžŒλ‹€"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["java", "programming", "w3schools", "lambda", "functional-interface"] raw_sources: ["https://www.w3schools.com/java/java_lambda.asp"] applied_in: [] github_commit: "" --- # [[Java Lambda]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A lambda can only replace an anonymous class when the target interface is "functional" (exactly ONE abstract method) β€” the tutorial's own rule of thumb draws the line precisely there: lambda for short single-method interfaces, anonymous class when you need multiple methods, extra fields, or to extend a class. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Lambda expression** (Java 8+) β€” a short, nameless block of code taking parameters and returning a value. [S1] - **Syntax** β€” `parameter -> expression`; multiple parameters need parentheses; complex bodies need `{ }` + explicit `return`. [S1] - **Functional interface requirement** β€” a lambda's target type must be an interface with EXACTLY one abstract method. [S1] - **Common use** β€” passed as arguments to methods like `ArrayList.forEach()`. [S1] - **Built-in functional interfaces** β€” e.g. `Consumer` from `java.util.function`. [S1] - **Custom functional interfaces** β€” user-defined single-method interfaces work as lambda targets too. [S1] - **Lambda vs. anonymous class rule of thumb** β€” lambda for short single-method interfaces; anonymous class for multiple methods/fields/extending a class. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - forEach with lambda: `numbers.forEach((n) -> { System.out.println(n); });`. [S1] - Lambda stored in a variable (Consumer): `Consumer method = (n) -> { System.out.println(n); }; numbers.forEach(method);`. [S1] - Lambda as method parameter (custom functional interface): `interface StringFunction { String run(String str); } ... StringFunction exclaim = (s) -> s + "!"; printFormatted("Hello", exclaim);`. [S1] - Same task via anonymous class vs. lambda: `Greeting g = new Greeting() { public void sayHello() {...} };` vs. `Greeting g = () -> System.out.println("Hello from lambda");`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **λžŒλ‹€ μ‚¬μš©μ˜ μ „μ œ 쑰건**: λŒ€μƒ μΈν„°νŽ˜μ΄μŠ€κ°€ λ°˜λ“œμ‹œ ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€(좔상 λ©”μ„œλ“œ 1개)μ—¬μ•Ό 읡λͺ… 클래슀λ₯Ό λžŒλ‹€λ‘œ λŒ€μ²΄ν•  수 μžˆλ‹€λŠ” 점이 κ·œμΉ™μœΌλ‘œ λͺ…μ‹œλ¨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” ArrayList.forEach()에 λžŒλ‹€λ₯Ό μ „λ‹¬ν•˜λŠ” νŒ¨ν„΄μ΄ κ°€μž₯ ν”ν•œ μ‹€μ „ ν™œμš©μ΄λ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Lambda passed to forEach() (Java): ```java ArrayList numbers = new ArrayList(); numbers.add(5); numbers.add(9); numbers.add(8); numbers.add(1); numbers.forEach((n) -> { System.out.println(n); }); ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[Java Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[Java Anonymous]], [[Java Interface]], [[Java Threads]], [[Java Advanced Sorting]] - **μ°Έμ‘° λ§₯락:** 읡λͺ… 클래슀의 μΆ•μ•½ν˜• β€” κ³ κΈ‰ μ •λ ¬(Comparator λžŒλ‹€) μ±•ν„°λ‘œ 직접 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” Java Lambda Expressions β€” https://www.w3schools.com/java/java_lambda.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "Java Lambda Expressions" page (Astra wiki-curation, P-Reinforce v3.1 format).