์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- Spring
- ๋ฐ์ดํฌ์คํธ๋ผ
- ๊ตฌํ
- GCP
- error
- dfs
- ์ฌ๊ท
- DP
- ์ด๋ถ ํ์
- ์๋ฎฌ๋ ์ด์
- dynamic debugging
- java
- ์คํ
- CVE
- OS
- web
- ๋ฌธ์์ด
- BFS
- ๋ถํ ์ ๋ณต
- ์ต๋จ ๊ฒฝ๋ก
- JPA
- ๋ฐฑํธ๋ํน
- ์ฐ์ ์์ ํ
- Reversing
- thymeleaf
- ๋งต
- ๊ทธ๋ฆฌ๋
- ์์ ์ ๋ ฌ
- ๋์ ํฉ
- c++
- Today
- Total
hades
[๋ชจ๋ ์๋ฐ ์ธ ์ก์ ] ๋์ ํ๋ผ๋ฏธํฐํ ์ฝ๋ ์ ๋ฌํ๊ธฐ ๋ณธ๋ฌธ
[๋ชจ๋ ์๋ฐ ์ธ ์ก์ ] ๋์ ํ๋ผ๋ฏธํฐํ ์ฝ๋ ์ ๋ฌํ๊ธฐ
hades1 2024. 9. 3. 13:45ํ์์ฑ
๋์ ํ๋ผ๋ฏธํฐํ๋ฅผ ์ด์ฉํ๋ฉด ์์ฃผ ๋ฐ๋๋ ์๊ตฌ์ฌํญ์ ํจ๊ณผ์ ์ผ๋ก ๋์ํ ์ ์๋ค. ๋์ ํ๋ผ๋ฏธํฐํ๋ ์์ง์ ์ด๋ป๊ฒ ๋์ํ ๊ฒ์ธ์ง ๊ฒฐ์ ํ์ง ์์ ์ฝ๋ ๋ธ๋ก์ ์๋ฏธํ๋ค.
๋ณํํ๋ ์๊ตฌ์ฌํญ์ ๋์ํ๊ธฐ
์ฒซ ๋ฒ์งธ ์๋ : ๋ น์ ์ฌ๊ณผ ํํฐ๋ง
์ฌ๊ณผ ์ฌ๊ณ ๋ชฉ๋ก์์ ์ด๋ก์ ์ฌ๊ณผ๋ง ํํฐ๋งํ๋ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ฉด, ๋ค์๊ณผ ๊ฐ๋ค.
public static List<Apple> filterGreenApples(List<Apple> inventory){
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory){
if (Color.GREEN.equals(apple.getColor())){
result.add(apple);
}
}
return result;
}
์์ ๊ฒฝ์ฐ์์, ๋นจ๊ฐ์ ์ฌ๊ณผ๋ฅผ ํํฐ๋งํ๋ ๊ฒ์ผ๋ก ๋ฐ๊พธ๊ณ ์ถ์ ๋๋ ์กฐ๊ฑด๋ฌธ์ ์๊น์ ๋ฐ๊พผ ๋ฉ์๋๋ฅผ ์์ฑํ๋ฉด ๋๋ค. ํ์ง๋ง, ์๊น์ด ํจ์ฌ ๋ ๋ค์ํ ๊ฒฝ์ฐ, ์์ฑํด์ผ ํ๋ ๋ฉ์๋๊ฐ ๋งค์ฐ ๋ง์์ง๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. ์ด๋ฐ ์ํฉ์์ ์ข์ ๊ท์น์ด ์๋ค.
'๊ฑฐ์ ๋น์ทํ ์ฝ๋๊ฐ ๋ฐ๋ณต ์กด์ฌํ๋ค๋ฉด, ๊ทธ ์ฝ๋๋ฅผ ์ถ์ํํ๋ค.'
๋ ๋ฒ์งธ ์๋ : ์์ ํ๋ผ๋ฏธํฐํ
์๊น์ ์ถ์ํํ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
public static List<Apple> filterApplesByColor(List<Apple> inventory, Color color){
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory){
if (color.equals(apple.getColor())){
result.add(apple);
}
}
return result;
}
์ ์ฝ๋๋ฅผ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ํํฐ๋งํ ์ ์์ ๊ฒ์ด๋ค.
List<Apple> greenApples = filterApplesByColor(inventory, Color.GREEN);
List<Apple> redApples = filterApplesByColor(inventory, Color.RED);
๋น์ทํ๊ฒ, ๋ฌด๊ฒ๋ฅผ ๊ฐ์ง๊ณ ํํฐ๋งํ๊ณ ์ถ์ ๋๋ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌํํ๋ค.
public static List<Apple> filterApplesByWeight(List<Apple> inventory, int weight){
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory){
if (apple.getWeight() > weight){
result.add(apple);
}
}
return result;
}
ํ์ง๋ง, ์๊น๋ก ํํฐ๋งํ๋ ๋ฉ์๋์ ๋ฌด๊ฒ๋ก ํํฐ๋งํ๋ ๋ฉ์๋๋ฅผ ๋น๊ตํด๋ณด๋ฉด, ์๋น ๋ถ๋ถ์ด ์ค๋ณต๋๋ ๊ฒ์ ๋ฐ๊ฒฌํ ์ ์๋ค. ์ด๋ ์ํํธ์จ์ด๊ณตํ์ DRY(๊ฐ์ ๊ฒ์ ๋ฐ๋ณตํ์ง ๋ง ๊ฒ) ์์น์ ์ด๊ธฐ๋ ๊ฒ์ด๋ค. ๊ฐ์ฅ ์ฝ๊ฒ ๋ ์ฌ๋ฆด ์ ์๋ ํด๊ฒฐ์ฑ ์ ํ๋๊ทธ๋ฅผ ์ด์ฉํ์ฌ ์๊น๊ณผ ๋ฌด๊ฒ๋ฅผ ๋์์ ํํฐ๋งํ๋ ๊ฒ์ด๋ค.
์ธ ๋ฒ์งธ ์๋ : ๊ฐ๋ฅํ ๋ชจ๋ ์์ฑ์ผ๋ก ํํฐ๋ง
public static List<Apple> filterApples(List<Apple> inventory, Color color, int weight, boolean flag){
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory){
if ((flag && color.equals(apple.getColor()) || !flag && apple.getWeight() > weight)){
result.add(apple);
}
}
return result;
}
List<Apple> redApples = filterApples(inventory, Color.RED, 0, true);
List<Apple> heavyApples = filterApples(inventory, null, 100, false);
flag๊ฐ true์ด๋ฉด, ์๊น๋ก ํํฐ๋งํ๊ณ , flag๊ฐ false์ด๋ฉด, ๋ฌด๊ฒ๋ก ํํฐ๋งํ๋ค.
๋ฌธ์ ๋ ์ฌ์ฉ ์, true์ false๊ฐ ๋ฌด์์ ์๋ฏธํ๋์ง ํ๋์ ํ์ ํ๊ธฐ๋ ์ด๋ ต๋ค. ์ด๋ค ๊ธฐ์ค์ผ๋ก ํํฐ๋งํ ๊ฒ์ธ์ง ์ ํํ, ๊ทธ๋ฆฌ๊ณ ํจ๊ณผ์ ์ผ๋ก ์ ๋ฌํด์ผ ํ๋ค.
๋์ ํ๋ผ๋ฏธํฐํ
๋ค ๋ฒ์งธ ์๋ : ์ถ์์ ์กฐ๊ฑด์ผ๋ก ํํฐ๋ง
์ฌ๊ณผ์ ์ด๋ค ์์ฑ์ ๊ธฐ์ดํด์ ๋ถ๋ฆฌ์ ๊ฐ์ ๋ฐํํ๋ ๋ฉ์๋๋ฅผ ์ ์ํ ์ ์๋ค. ์ฐธ ๋๋ ๊ฑฐ์ง์ ๋ฐํํ๋ ํจ์๋ฅผ Predicate๋ผ๊ณ ํ๋ค. ์ ํ ์กฐ๊ฑด์ ๊ฒฐ์ ํ๋ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
public interface ApplePredicate {
boolean test(Apple apple);
}
public class AppleHeavyWeightPredicate implements ApplePredicate{
@Override
public boolean test(Apple apple) {
return apple.getWeight() > 100;
}
}
public class AppleGreenColorPredicate implements ApplePredicate{
@Override
public boolean test(Apple apple) {
return apple.getColor().equals(Color.GREEN);
}
}
์ด๋ฅผ ์ ๋ต ๋์์ธ ํจํด์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค. ์ ๋ต ๋์์ธ ํจํด์ ๊ฐ ์๊ณ ๋ฆฌ์ฆ์ ์บก์ํํ๋ ์๊ณ ๋ฆฌ์ฆ ํจ๋ฐ๋ฆฌ(์ธํฐํ์ด์ค)๋ฅผ ์ ์ํด ๋ ๋ค์์ ๋ฐํ์์ ์๊ณ ๋ฆฌ์ฆ(๊ตฌํ ๋ฉ์๋)์ ์ ํํ๋ ๋ฐฉ๋ฒ์ด๋ค.
public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate applePredicate){
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory){
if (applePredicate.test(apple)){
result.add(apple);
}
}
return result;
}
์ด๋ ๊ฒ ๋ฉ์๋(filterApples)๊ฐ ๋ค์ํ ๋์ ๋๋ ์ ๋ต(applePredicate)๋ฅผ ๋ฐ์์ ๋ด๋ถ์ ์ผ๋ก ๋ค์ํ ๋์(test)์ ์ํํ ์ ์๋ค.
๊ธฐ์กด์ ์ฝ๋์ ๋นํด, ํจ์ฌ ๋ ์ ์ฐํ ์ฝ๋๋ฅผ ์ป์์ผ๋ฉฐ ๊ฐ๋ ์ฑ๊ณผ ์ฌ์ฉ ์ฉ์ด์ฑ์ด ๋์์ก๋ค. ํ ๊ฐ์ ํ๋ผ๋ฏธํฐ์ ๋ฉ์๋๋ฅผ ๋ฐ๊ฟ ์ ๋ฌํ๋ ๊ฒ์ผ๋ก ๋ค์ํ ๋์์ด ๊ฐ๋ฅํด์ก๋ค.
์ฌ๊ณผ์ ๋ํ ์ ๋ณด๋ฅผ ๋์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํ์ฌ ๋ค๋ฅด๊ฒ ์ถ๋ ฅํ๋ ๊ฒ์ ์๋์ ๊ฐ์ด ๊ตฌํํ ์ ์๋ค.
public interface AppleFormatter {
public String accept(Apple apple);
}
public class AppleSimpleFormatter implements AppleFormatter{
@Override
public String accept(Apple apple) {
return apple.getColor() + " ์ " + apple.getWeight() + "g ์ฌ๊ณผ";
}
}
public class AppleDetailFormatter implements AppleFormatter{
@Override
public String accept(Apple apple) {
return "์ด ์ฌ๊ณผ์ ์๊น์ " + apple.getColor() + ", ๋ฌด๊ฒ๋ " + apple.getWeight() + "g ์
๋๋ค.";
}
}
prettyPrintApple(inventory, new AppleDetailFormatter());
prettyPrintApple(inventory, new AppleSimpleFormatter());
๋ณต์กํ ๊ณผ์ ๊ฐ์ํ
์ต๋ช ํด๋์ค
์ต๋ช ํด๋์ค๋ ์๋ฐ์ ์ง์ญ ํด๋์ค(๋ธ๋ก ๋ด๋ถ์ ์ ์ธ๋ ํด๋์ค)์ ๋น์ทํ ๊ฐ๋ ์ด๋ค. ์ต๋ช ํด๋์ค๋ ๋ง ๊ทธ๋๋ก ์ด๋ฆ์ด ์๋ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค๋ก, ์ต๋ช ํด๋์ค๋ฅผ ์ด์ฉํ๋ฉด, ์ฆ์์์ ํ์ํ ๊ตฌํ์ ๋ง๋ค์ด์ ์ฌ์ฉํ ์ ์๋ค.
๋ค์ฏ ๋ฒ์งธ ์๋ : ์ต๋ช ํด๋์ค ์ฌ์ฉ
ApplePredicate์ ๊ตฌํ์ฒด๋ฅผ ์ผ์ผ์ด ์์ฑํ์ง ์๊ณ , ์ต๋ช ํด๋์ค๋ฅผ ์์ฑํ์ฌ ๊ฐ๋จํ ํ ์ ์๋ค.
List<Apple> redApples = filterApples(inventory, new ApplePredicate() {
@Override
public boolean test(Apple apple) {
return Color.RED.equals(apple.getColor());
}
});
์ฌ์ฏ ๋ฒ์งธ ์๋ : ๋๋ค ํํ์ ์ฌ์ฉ
๋๋ค์์ ์ต๋ช ํด๋์ค๋ก ์ฌ์ฉํ ์ ์๋ค.
List<Apple> redApples = filterApples(inventory, (Apple apple) -> Color.RED.equals(apple.getColor()));
์ผ๊ณฑ ๋ฒ์งธ ์๋ : ๋ฆฌ์คํธ ํ์์ผ๋ก ์ถ์ํ
public static <T> List<T> filter(List<T> list, Predicate<T> predicate){
List<T> result = new ArrayList<>();
for (T t : list) {
if (predicate.test(t)){
result.add(t);
}
}
return result;
}
List<Apple> redApples = filter(inventory, (Apple apple) -> Color.RED.equals(apple.getColor()));
List<Grape> heavyGrape = filter(inventory, (Grape grape) -> grape.getWeight() > 100);
์ค์ ์์
Comparator๋ก ์ ๋ ฌํ๊ธฐ
List์ ํ๋ผ๋ฏธํฐ๋ Comparator<T>์ธ๋ฐ, ์ธํฐํ์ด์ค์ด๋ค. ๋ฐ๋ผ์, ์ต๋ช ํด๋์ค๋ ๋๋ค ํํ์์ ์ด์ฉํ ์ ์๋ค.
inventory.sort(
(Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight())
);
'๐๐ปโโ๏ธ ๊ธฐ๋ณธํ๋ จ > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ชจ๋ ์๋ฐ ์ธ ์ก์ ] ์คํธ๋ฆผ (1) | 2024.09.05 |
---|---|
[๋ชจ๋ ์๋ฐ ์ธ ์ก์ ] ๋๋ค ํํ์ (0) | 2024.09.04 |
[Java] ์๋ฐ ๊ธฐ์ด (0) | 2024.08.27 |
[JPA] ์๋ช ์ฃผ๊ธฐ ์ผ์น ์ ์ฐธ์กฐ ๋ณ๊ฒฝ์ ์ํ์ฑ (0) | 2024.08.19 |
[JPA] JPA ๊ธฐ์ด (0) | 2024.08.02 |