๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
TIL

17์ผ์ฐจ) ์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ‚ค์˜ค์Šคํฌ ๊ณผ์ œ ๋ฆฌํŒฉํ† ๋ง

by suyeoneee 2024. 11. 27.

Today I Learned ๐Ÿ‘ฉ๐Ÿฝ‍๐Ÿ’ป

- ์•Œ๊ณ ๋ฆฌ์ฆ˜ 26, 27, 28๋ฒˆ

- ํ‚ค์˜ค์Šคํฌ ๋„์ „ Lv1 + ๋ฆฌํŒฉํ† ๋ง ์ง„ํ–‰์ค‘ ..


์˜ค๋Š˜์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 3๋ฌธ์ œ ํ’€์ด์™€ ํ‚ค์˜ค์Šคํฌ ๊ณผ์ œ ๋ ˆ๋ฒจ1 ๋งˆ๋ฌด๋ฆฌ, ๋ฆฌํŒฉํ† ๋ง๊นŒ์ง€ ์ง„ํ–‰ํ•˜์˜€๋‹ค.

๋ ˆ๋ฒจ2๋„ ๋„์ „ํ•˜๊ณ  ์‹ถ์ง€๋งŒ ์‹œ๊ฐ„์ƒ ์šฐ์„  ๋ฆฌํŒฉํ† ๋ง์„ ๋จผ์ € ํ–ˆ๋‹ค..!!

์žฅ๋ฐ”๊ตฌ๋‹ˆ ๊ธฐ๋Šฅ์ด ๋“ค์–ด์˜ค๋ฉด์„œ ๋ฐ˜๋ณต๋ฌธ๊ณผ ์กฐ๊ฑด๋ฌธ์ด ๊ฑฐ์˜ 3๋ฐฐ ๋Š˜์–ด๋‚˜์„œ ์ด ๋ถ€๋ถ„์„ ์–ด๋–ป๊ฒŒ ๋ฆฌํŒฉํ† ๋งํ•˜๋ฉด ์ข‹์„์ง€ ์•Œ์•„๋ดค๋‹ค.

 

โ—๏ธ๋ฐ˜๋ณต๋ฌธ

- ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ ์ตœ์†Œํ™”

: ๋ณ„๋„์˜ ๋ฉ”์„œ๋“œ๋กœ ๋ถ„๋ฆฌํ•˜๊ฑฐ๋‚˜ ์ŠคํŠธ๋ฆผ API๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹จ์ˆœํ™”

 

- for-each ์‚ฌ์šฉ

for (String item : array) {
    System.out.println(item);
}

 

- ์ŠคํŠธ๋ฆผ API ํ™œ์šฉ

List<String> result = new ArrayList<>();
for (String item : list) {
    if (item.startsWith("A")) {
        result.add(item);
    }
}

// -------------

List<String> result = list.stream()
                          .filter(item -> item.startsWith("A"))
                          .collect(Collectors.toList());

 

โ—๏ธ์กฐ๊ฑด๋ฌธ

- ์ค‘์ฒฉ ์กฐ๊ฑด๋ฌธ ์ œ๊ฑฐ

: ์กฐ๊ธฐ ๋ฐ˜ํ™˜ ๋ฐฉ์‹์œผ๋กœ ๋‹จ์ˆœํ™”

if (user != null) {
    if (user.isActive()) {
        System.out.println("User is active");
    }
}

// --------------

if (user == null) return;
if (!user.isActive()) return;

System.out.println("User is active");

 

- switch-case ์ตœ์†Œํ™”

: Map์ด๋‚˜ ๋žŒ๋‹ค ํ‘œํ˜„์‹์œผ๋กœ ๋ฆฌํŒฉํ† ๋ง

switch (role) {
    case "ADMIN":
        System.out.println("Admin access granted");
        break;
    case "USER":
        System.out.println("User access granted");
        break;
    default:
        System.out.println("Access denied");
}

// -----------
String message = switch (role) {
    case "ADMIN" -> "Admin access granted";
    case "USER" -> "User access granted";
    default -> "Access denied";
};
System.out.println(message);

// -----------
Map<String, String> roleMessages = Map.of(
    "ADMIN", "Admin access granted",
    "USER", "User access granted"
);

System.out.println(roleMessages.getOrDefault(role, "Access denied"));

 

 

- ์กฐ๊ฑด ๋ถ„๊ธฐ ์ค„์ด๊ธฐ

if (type.equals("PDF")) {
    processPDF();
} else if (type.equals("DOC")) {
    processDOC();
} else if (type.equals("TXT")) {
    processTXT();
}

// -------------
Map<String, Runnable> actions = Map.of(
    "PDF", this::processPDF,
    "DOC", this::processDOC,
    "TXT", this::processTXT
);

actions.getOrDefault(type, this::processDefault).run();

๋Œ“๊ธ€