greenkang
그린 개발log
greenkang
전체 방문자
였늘
μ–΄μ œ
  • λΆ„λ₯˜ 전체보기 (28)
    • μ•Œκ³ λ¦¬μ¦˜ (20)
    • MySQL (0)
    • 생각 (0)
    • 컴퓨터ꡬ쑰 (6)
    • Spring Β· SpringBoot (1)
    • Java (1)
    • μž₯μ•  λŒ€μ‘ 회고 (0)

λΈ”λ‘œκ·Έ 메뉴

  • ν™ˆ
  • νƒœκ·Έ
  • λ°©λͺ…둝

곡지사항

인기 κΈ€

νƒœκ·Έ

  • 컴퓨터ꡬ쑰
  • λ°±μ€€7569
  • μž₯μ•  λŒ€μ‘ 회고
  • λ°±μ€€ 24479번 μžλ°”
  • λ°±μ€€ 1065번 μžλ°”
  • λ°±μ€€ 17265번
  • λ¦¬νŠΈμ½”λ“œ
  • TOPCODERμ•Œκ³ λ¦¬μ¦˜νŠΈλ ˆμ΄λ‹
  • μ•Œκ³ λ¦¬μ¦˜
  • μ–΄μ…ˆλΈ”λ¦¬μ–΄
  • λ°±μ€€
  • λ°±μ€€7569 java
  • java
  • python
  • 41. First Missing Positive
  • ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€
  • ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 거리두기 μžλ°”
  • 파이썬
  • μžλ°”
  • ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 거리두기 java

졜근 λŒ“κΈ€

졜근 κΈ€

ν‹°μŠ€ν† λ¦¬

hELLO Β· Designed By μ •μƒμš°.
greenkang
Java

[λͺ¨λ˜ μžλ°” 인 μ•‘μ…˜] λ™μž‘ νŒŒλΌλ―Έν„°ν™” μ½”λ“œ μ „λ‹¬ν•˜κΈ°

Java

[λͺ¨λ˜ μžλ°” 인 μ•‘μ…˜] λ™μž‘ νŒŒλΌλ―Έν„°ν™” μ½”λ“œ μ „λ‹¬ν•˜κΈ°

2023. 8. 24. 21:46

πŸ“š λ™μž‘ νŒŒλΌλ―Έν„°λž€?

  • 아직은 μ–΄λ–»κ²Œ μ‹€ν–‰ν•  것인지 κ²°μ •ν•˜μ§€ μ•Šμ€ μ½”λ“œ λΈ”λ‘μœΌλ‘œ λ‚˜μ€‘μ— 싀행될 λ©”μ„œλ“œμ˜ 인수둜 μ½”λ“œ 블둝을 μ „λ‹¬ν•˜λŠ” 것

즉, μ›ν•˜λŠ” λ™μž‘μ„ λ©”μ„œλ“œμ˜ 인수둜 전달 ν•  수 있음

 

πŸ“š λ™μž‘ νŒŒλΌλ―Έν„°ν™” μ‚¬μš© 이유

  • λ°”λ€ŒλŠ” μš”κ΅¬ 사항에 효과적으둜 λŒ€μ‘ν•  수 있음 

πŸ“š 예제

농μž₯ 재고 λͺ©λ‘ μ• ν”Œλ¦¬μΌ€μ΄μ…˜ 리슀트의 μš”κ΅¬μ‚¬ν•­

  1. 녹색 μ‚¬κ³Όλ§Œ 필터링
  2. λΉ¨κ°„ μ‚¬κ³Όλ§Œ 필터링
  3. 무거운 μ‚¬κ³Όλ§Œ 필터링
  4. κ°€λ²Όμš΄ μ‚¬κ³Όλ§Œ 필터링
  5. ...

μ²˜μŒμ—λŠ” 1번의 μš”κ΅¬μ‚¬ν•­λ§Œ 쑴재 ν•˜λ‹€κ°€, λ‚˜μ€‘μ— 2번의 μš”κ΅¬μ‚¬ν•­μ΄ 좔가될 수 μžˆμŠ΅λ‹ˆλ‹€.

이럴 경우 μ•„λž˜μ™€ 같이 색을 νŒŒλΌλ―Έν„°ν™” μ‹œμΌœμ„œ 필터링 λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

pulbic static List<Apple> filterAppleByColor(List<Apple> aplleList, Color color) {
	
    List<Apple> result = new ArrayList<>();
    for (Apple apple: inventory) {
    	if ( apple.getColor().equals(color) ) {
        	result.add(apple);
        }
    }
    return result;
}

 

ν•˜μ§€λ§Œ 3번, 4번과 같이 νŒŒλΌλ―Έν„°μ˜ λŒ€μƒμ΄ 바뀐닀면?

 

λ‹€μŒκ³Ό 같은 μƒν™©μ—μ„œ λ™μž‘νŒŒλΌλ―Έν„°ν™”λ₯Ό μ‚¬μš©ν•˜λ©΄ λ°”λ€ŒλŠ” μš”κ΅¬ 사항에 효과적으둜 λŒ€μ‘ν•  수 μžˆλŠ” λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 총 4κ°€μ§€ λ°©λ²•μœΌλ‘œ λ™μž‘ νŒŒλΌλ―Έν„°λ₯Ό κ΅¬ν˜„ν•΄λ³΄κ³ , λΉ„κ΅ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€!

 

1️⃣ 좔상적 쑰건으둜 필터링

선택 쑰건을 κ²°μ •ν•˜λŠ” μΈν„°νŽ˜μ΄μŠ€ μ •μ˜

public interface ApplePredicate {
	boolean test (Apple apple);
}

 

λ‹€μ–‘ν•œ 선택 쑰건을 λŒ€ν‘œν•˜λŠ” μ—¬λŸ¬ λ²„μ „μ˜ ApplePredicateλ₯Ό μ •μ˜

// 무거운 사과 선택
public class AppleHeavyWeightPredicate implements ApplePredicate {
	public boolean test(Apple apple) {
    	return apple.getWeight() > 150;
    }
}


// 녹색 μ‚¬κ³Όλ§Œ 선택
public class AppleGreenColorPredicate implements ApplePredicate {
	public boolean test(Apple apple) {
    	return GREEN.equals(apple.getColor());
    }
}

 

μœ„μ—μ„œ μ •μ˜ν•œ ApplePredicateλ₯Ό μ΄μš©ν•œ ν•„ν„° λ©”μ„œλ“œ

public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate p) {

    List<Apple> result = new ArrayList<>();
    for (Apple apple: inventory) {
    	if ( p.test(apple) ) {
        	result.add(apple);
        }
    }
    return result;
}

 

2️⃣ 읡λͺ…ν΄λž˜μŠ€ μ‚¬μš©

읡λͺ…ν΄λž˜μŠ€λ₯Ό μ΄μš©ν•˜λ©΄ 클래슀 μ„ μ–Έκ³Ό μΈμŠ€ν„΄μŠ€ν™”λ₯Ό λ™μ‹œμ— ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

List<Apple> redApples = filterApples(inventory, new ApplePredicate() {
	public boolean test(Apple apple) {
    	return RED.equals(apple.getColor());
    }

});

 

3️⃣ λžŒλ‹€ ν‘œν˜„μ‹ μ‚¬μš©

λžŒλ‹€ ν‘œν˜„μ‹μ„ μ‚¬μš©ν•˜λ©΄ 읡λͺ…ν΄λž˜μŠ€ 방식보닀 훨씬 κ°„κ²°ν•˜κ³  μ§κ΄€μ μœΌλ‘œ κ΅¬ν˜„ κ°€λŠ₯ ν•©λ‹ˆλ‹€.

List<Apple> redApples = filterApples(inventory, (Apple apple) -> RED.equals(apple.getColor()));

 

4️⃣ 리슀트 ν˜•μ‹μœΌλ‘œ 좔상화

ν˜•μ‹ νŒŒλΌλ―Έν„°Tλ“±μž₯

public interface Predicate<T> {
	boolean test(T t);
}

public static <T> List<T> filter(List<T> list, Predicate<T> p) {
	List<T> result = new ArrayList<>();
    for (T e: list) {
    	if ( p.test(e) ) {
        	result.add(e);
        }
    }
    
    return result;
}

 

μ‚¬κ³ΌλΏλ§Œ μ•„λ‹ˆλΌ λ°”λ‚˜λ‚˜, μ˜€λ Œμ§€, μ •μˆ˜ λ“±μ˜ λ¦¬μŠ€νŠΈμ— ν•„ν„° λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

List<Apple> redApples = filter(inventory, (Apple apple) -> RED.equals(apple.getColor()));

List<Integer> evenNumbers = filter(numbers, (Integer i) -> i % 2 == 0);

 

리슀트 ν˜•μ‹μœΌλ‘œ 좔상화 방식을 μ΄μš©ν•˜λ©΄ μœ μ—°μ„±κ³Ό 간결함을 높일 수 μžˆμŠ΅λ‹ˆλ‹€.

  • 1️⃣ 좔상적 쑰건으둜 필터링
  • 2️⃣ 읡λͺ…ν΄λž˜μŠ€ μ‚¬μš©
  • 3️⃣ λžŒλ‹€ ν‘œν˜„μ‹ μ‚¬μš©
  • 4️⃣ 리슀트 ν˜•μ‹μœΌλ‘œ 좔상화
greenkang
greenkang

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”

단좕킀

λ‚΄ λΈ”λ‘œκ·Έ

λ‚΄ λΈ”λ‘œκ·Έ - κ΄€λ¦¬μž ν™ˆ μ „ν™˜
Q
Q
μƒˆ κΈ€ μ“°κΈ°
W
W

λΈ”λ‘œκ·Έ κ²Œμ‹œκΈ€

κΈ€ μˆ˜μ • (κΆŒν•œ μžˆλŠ” 경우)
E
E
λŒ“κΈ€ μ˜μ—­μœΌλ‘œ 이동
C
C

λͺ¨λ“  μ˜μ—­

이 νŽ˜μ΄μ§€μ˜ URL 볡사
S
S
맨 μœ„λ‘œ 이동
T
T
ν‹°μŠ€ν† λ¦¬ ν™ˆ 이동
H
H
단좕킀 μ•ˆλ‚΄
Shift + /
⇧ + /

* λ‹¨μΆ•ν‚€λŠ” ν•œκΈ€/영문 λŒ€μ†Œλ¬Έμžλ‘œ 이용 κ°€λŠ₯ν•˜λ©°, ν‹°μŠ€ν† λ¦¬ κΈ°λ³Έ λ„λ©”μΈμ—μ„œλ§Œ λ™μž‘ν•©λ‹ˆλ‹€.