Giken Dev
[Java] stream API ์™€ Optional

์‹ค๋ฌด์—์„œ ์ž์ฃผ ๋ฐœ์ƒํ•˜๋Š” ์ƒํ™ฉ์— stream API์™€ Optional ์ ์šฉํ•˜๊ธฐ ๋ฐฑ์—”๋“œ ์‹ค๋ฌด์—์„œ ์ž์ฃผ ๋ฐœ์ƒํ•˜๋Š” ์ผ ๋ฐ์ดํ„ฐ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•œ๋‹ค. ํŠน์ •ํ•œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”๊ฐ€? ์—†๋‹ค -> ์˜ˆ์™ธ ๋ฐœ์ƒ! ์žˆ๋‹ค -> ๋‹ค์Œ ์ž‘์—… ์‹คํ–‰ for + if๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ vs stream API + Optional๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ list ์—์„œ 1234 ๋ฅผ ์ฐพ๋Š” ๊ณผ์ •์„ ์‚ดํŽด๋ณด๊ฒ ๋‹ค. for + if๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ public class ForAndIfFilterExampleMain { public static void main(String[] args) { int[] integerArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; List integerList = Arrays.stream(integerAr..

[Java] stream API

stream API public class ForIterationExampleMain { public static void main(String[] args) { List integerList = new ArrayList(); integerList.add(10); integerList.add(20); integerList.add(30); integerList.add(40); integerList.add(50); integerList.add(60); integerList.add(70); for (int i = 0; i stream API ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ. for ..

[Java] getter, setter, ์ƒ์„ฑ์ž๋กœ ์•Œ์•„๋ณด๋Š” ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ์ฝ”๋“œ

https://giken.tistory.com/entry/Java-if%EB%AC%B8-%EC%A0%9C%EA%B1%B0%ED%95%98%EA%B8%B0 [Java] if๋ฌธ ์ œ๊ฑฐํ•˜๊ธฐ ๋„ˆ๋ฌด ๋งŽ์€ if, else ๋Š” ์ฝ๊ธฐ ์–ด๋ ค์šด ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค .. ์ด ๋•Œ๋Š” ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ๋„ ์–ด๋ ต๊ณ  ์ฝ”๋“œ๋ฅผ ๋””๋ฒ„๊น…ํ•  ๋•Œ๋„ ์–ด๋ ค์šธ ์ˆ˜ ๋ฐ–์—. if ๋ฌธ์ด ๋งŽ์€ ์ฝ”๋“œ์—์„œ ์–ด๋–ป๊ฒŒ if ๋ฌธ์„ ์ œ๊ฑฐํ• ์ง€ ํ•œ๋ฒˆ ์ƒ๊ฐํ•ด๋ณด์ž. public giken.tistory.com ์œ„ ํฌ์ŠคํŒ…์˜ ์ฝ”๋“œ์™€ ์ด์–ด์„œ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. getter ublic class Client { public int someMethod(CalculateCommand calculateCommand) { CalculateType calculateType = calculateCommand.g..

[Java] if๋ฌธ ์ œ๊ฑฐํ•˜๊ธฐ - ๋ฆฌํŒฉํ† ๋ง

๋„ˆ๋ฌด ๋งŽ์€ if, else ๋Š” ์ฝ๊ธฐ ์–ด๋ ค์šด ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค .. ์ด ๋•Œ๋Š” ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ๋„ ์–ด๋ ต๊ณ  ์ฝ”๋“œ๋ฅผ ๋””๋ฒ„๊น…ํ•  ๋•Œ๋„ ์–ด๋ ค์šธ ์ˆ˜ ๋ฐ–์—. if ๋ฌธ์ด ๋งŽ์€ ์ฝ”๋“œ์—์„œ ์–ด๋–ป๊ฒŒ if ๋ฌธ์„ ์ œ๊ฑฐํ• ์ง€ ํ•œ๋ฒˆ ์ƒ๊ฐํ•ด๋ณด์ž. public class CalculateCommand { private CalculateType calculateType; private int num1; private int num2; public CalculateCommand(CalculateType calculateType, int num1, int num2) { this.calculateType = calculateType; this.num1 = num1; this.num2 = num2; } public CalculateType getCalcul..

[Java] Optional - nullPointException์„ ์ž˜ ๋‹ค๋ค„๋ณด์ž

Optional ์€ null ๊ฐ’์„ ์ž˜ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. without Optional public class SimpleNpeExampleMain { public static void main(String[] args) { String string = getNullString(); System.out.println("string=" + string); System.out.println(string.toUpperCase()); } private static String getNullString() { return null; } } ๊ทผ๋ฐ ์ด๋ ‡๊ฒŒ ๋‹น์—ฐํ•˜๊ฒŒ null ์ด ํ„ฐ์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์‹ค์ œ ์ƒํ™ฉ์—์„œ ์—†์„ ๊ฒƒ์ด๋‹ค. ์ข€ ๋” ํ˜„์‹ค์ ์ธ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž. public class MapRepository { p..

article thumbnail
[Java] Object ํด๋ž˜์Šค์™€ ์ฃผ์š” ๋ฉ”์„œ๋“œ - equals(), hashCode(), toString()

Object ํด๋ž˜์Šค๋ž€? ๋ชจ๋“  ํด๋ž˜์Šค๋Š” Object ํด๋ž˜์Šค์˜ ์ž์† ํด๋ž˜์Šค์ด๋‹ค. public class SomeObject { } public class SombeObject extends Object { } ๋”ฐ๋ผ์„œ, extends Object ๊ฐ€ ์ƒ๋žต๋˜์–ด ์žˆ๋Š”๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. Object ํด๋ž˜์Šค์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ๋“ค clone(), equals(), finalize(), getClass(), hashCode(), notify(), notifyAll(), toString(), wait() ์ด ์ค‘์—์„œ๋„ ํŠนํžˆ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์„ธ ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๋ฅผ ์•Œ์•„๋ณด๋ คํ•œ๋‹ค -> equals(), hashCode(), toString() equals() ๋™์ผ์„ฑ : ๋น„๊ต ๋Œ€์ƒ์ด ์‹ค์ œ๋กœ '๋˜‘๊ฐ™์€' ๋Œ€์ƒ์ด์–ด์•ผ ํ•จ ( = ๋‘˜์€ ์‹ค์ œ๋กœ๋Š” ํ•˜..

[Java] Checked & Unchecked exception ์˜ ์ฐจ์ด

Checked Exception ๊ณผ Unchecked Exception ์€ ๋ฌด์Šจ ์ฐจ์ด์ธ๊ฐ€์š”? (์ค‘์š”) ์ž์ฃผ ์ด์•ผ๊ธฐํ•˜๋Š” ์˜ค๋‹ต Checked Exception ์€ ์ปดํŒŒ์ผํ•  ๋•Œ ๋ฐœ์ƒํ•˜๊ณ , Unchecked Exception ์€ ๋Ÿฐํƒ€์ž„์— ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ๋‹ต๋ณ€ ! Checked Exception ์€ ์ปดํŒŒ์ผํ•  ๋•Œ ์˜ˆ์™ธ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ•์ œ Unchecked Exception ์€ ์˜ˆ์™ธ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ•์ œํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ •์˜๋˜์–ด์žˆ์ง€ ์•Š์€, ์ƒˆ๋กœ์šด ์˜ˆ์™ธ๋ฅผ ์ •์˜ํ•ด์•ผํ•  ๋•Œ๋Š” → Unchecked Exception ์„ ์ƒ์†๋ฐ›์•„์„œ ์“ด๋‹ค ! ๋Œ€๋ถ€๋ถ„์˜ ์˜ˆ์™ธ๋Š” ๋กœ์ง์—์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ Checke Exception์„ ์“ฐ๋ฉด, ๋˜์ง€๋Š” ์˜ˆ์™ธ๋ผ๋Š” ๊ฒƒ์ด ์™ธ๋ถ€์— ์•Œ๋ ค์ง→ ์บก์Šํ™”๊ฐ€ ๊นจ์ง ์˜ˆ์™ธ๋ฅผ ์ปดํŒŒ์ผ ๋„์ค‘์— ์ฒ˜๋ฆฌํ•˜๊ณ ์‹ถ..

[Java] Enum ์ด๋ž€

import java.util.function.BiFunction; // ๋žŒ๋‹ค๋กœ ๋ฐ”๊พผ ์‚ฌ์น™์—ฐ์‚ฐ์„ ํ‘œํ˜„ํ•˜๋Š” enum enum Expression { ADD((x, y) -> x + y), SUBTRACT((x, y) -> x - y), MULTIPLY((x, y) -> x * y), DIVIDE((x, y) -> { if (y != 0) { return x / y; } else { throw new ArithmeticException("Division by zero"); } }); private final BiFunction operation; Expression(BiFunction operation) { this.operation = operation; } int apply(int x, int y) { ..