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();
'TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
19์ผ์ฐจ) ๊ฐ์ฒด๊ฐ์ ๊ฒฐํฉ๋์ ๋คํ์ฑ, ์ธํฐํ์ด์ค (2) | 2024.11.29 |
---|---|
Java ํค์ค์คํฌ ๊ณผ์ - ์ฑ๋ฆฐ์ง Lv1 ํธ๋ฌ๋ธ ์ํ (1) | 2024.11.28 |
14์ผ์ฐจ) ์๋ฐ ๋๋ฑ์ฑ๊ณผ ๋์ผ์ฑ , equals()์ hashCode() (0) | 2024.11.22 |
Java ๊ณ์ฐ๊ธฐ ๊ณผ์ - level 2 ํธ๋ฌ๋ธ์ํ (0) | 2024.11.20 |
JAVA ๊ณ์ฐ๊ธฐ ๊ณผ์ - level 1 ํธ๋ฌ๋ธ์ํ (0) | 2024.11.20 |
๋๊ธ