λ™μž‘ νŒŒλΌλ―Έν„°ν™” μ½”λ“œ μ „λ‹¬ν•˜κΈ°

2024. 10. 10. 00:25·개발

 

#개발/μžλ°”/λͺ¨λ˜μžλ°”μΈμ•‘μ…˜

 

λ™μž‘ νŒŒλΌλ―Έν„°ν™”λ₯Ό μ΄μš©ν•˜λ©΄ 자주 λ°”λ€ŒλŠ” μš”κ΅¬μ‚¬ν•­μ— 효과적으둜 λŒ€μ‘ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

λ™μž‘ νŒŒλΌλ―Έν„°λž€? 아직은 μ–΄λ–»κ²Œ μ‹€ν–‰ν•  것인지 κ²°μ •ν•˜μ§€ μ•Šμ€ μ½”λ“œ 블둝이닀.

 

예λ₯Ό λ“€μ–΄, λ‚˜μ€‘μ— 싀행될 λ©”μ„œλ“œμ˜ 인수둜 μ½”λ“œ 블둝을 전달할 수 μžˆμŠ΅λ‹ˆλ‹€. 결과적으둜 μ½”λ“œ 블둝에 따라 λ©”μ„œλ“œμ˜ λ™μž‘μ΄ νŒŒλΌλ―Έν„°ν™” λ©λ‹ˆλ‹€. μ΄λ₯Ό μ΄ν•΄ν•˜κΈ° μœ„ν•΄ 책에 μžˆλŠ” ν•˜λ‚˜μ˜ 예제λ₯Ό 톡해 μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€. 기쑴의 농μž₯ 재고λͺ©λ‘ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ— λ¦¬μŠ€νŠΈμ—μ„œ 녹색 μ‚¬κ³Όλ§Œ 필터링 ν•˜λŠ” κΈ°λŠ₯을 μΆ”κ°€ν•œλ‹€κ³  κ°€μ •ν•©μ‹œλ‹€.

enum Color { RED, GREEN }

public static List<Apple> filterGreenApples(List<Apple> inventory) {
	List<Apple> result = new ArrayList<>();
	
	for (Apple apple : inventory) {
		if (GREEN.equals(apple.getColor()) {
			result.add(apple);
		}
	}
	return result;
}

 

그런데 κ°‘μžκΈ° 농뢀가 μ—¬λŸ¬ κ°€μ§€ λ‹€μ–‘ν•œ μƒ‰μœΌλ‘œ 필터링 ν•˜κ³ μ‹Άμ–΄μ§€λ©΄, μ—¬λŸ¬ λ©”μ„œλ“œλ₯Ό λ§Œλ“€κ³  if문을 μ—¬λŸ¬κ°œ λ§Œλ“€μ–΄μ•Ό ν• κΉŒμš”? 이런 μƒν™©μ—μ„œλŠ” λ‹€μŒκ³Ό 같은 쒋은 κ·œμΉ™μ΄ μžˆμŠ΅λ‹ˆλ‹€.

 

거의 λΉ„μŠ·ν•œ μ½”λ“œκ°€ 반볡 μ‘΄μž¬ν•œλ‹€λ©΄ κ·Έ μ½”λ“œλ₯Ό μΆ”μƒν™”ν•œλ‹€.

 

 

2. 색을 νŒŒλΌλ―Έν„°ν™”

색을 νŒŒλΌλ―Έν„°ν™”ν•  수 μžˆλ„λ‘ λ©”μ„œλ“œμ— νŒŒλΌλ―Έν„°λ₯Ό μΆ”κ°€ν•˜λ©΄ λ³€ν™”ν•˜λŠ” μš”κ΅¬μ‚¬ν•­μ— μ’€ 더 μœ μ—°ν•˜κ²Œ λŒ€μ‘ν•˜λŠ” μ½”λ“œλ₯Ό λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

public static List<Apple> filterApplesByColor(List<Apple> inventory, Color color) {
	List<Apple> result = new ArrayList<>();

	for (Apple apple: inventory) {
		if (apple.getColor().equals(color)) {
			result.add(apple);
		}
	}
	return result;
}

 

그럼 λ‹€μŒμ²˜λŸΌ κ΅¬ν˜„ν•œ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  수 μžˆμŠ΅λ‹ˆλ‹€.

List<Apple> greenApples = filterApplesByColor(inventory, GREEN);
List<Apple> redApples = filterApplesByColor(inventory, RED);

 

그런데 κ°‘μžκΈ° 농뢀가 λ‹€μ‹œ λ‚˜νƒ€λ‚˜μ„œλŠ” λ¬΄κ²Œκ°€ 150그램 이상인 사과가 무거운 사과인데, κ°€λ²Όμš΄ 사과와 무거운 μ‚¬κ³Όλ‘œ ꡬ뢄할 수 μžˆλ‹€λ©΄ 쒋을 것 κ°™λ‹€κ³  ν•©λ‹ˆλ‹€. 이전에, 색을 κ΅¬ν˜„ν•˜λ©΄μ„œ λ‹€μ–‘ν•œ 색에 λŒ€μ‘ν•  수 μžˆλ„λ‘ λ©”μ„œλ“œλ₯Ό λ§Œλ“€μ—ˆμœΌλ‹ˆ ν˜Ήμ‹œ λͺ¨λ₯΄λŠ” μš”κ΅¬μ‚¬ν•­μ„ λŒ€λΉ„ν•˜κΈ° μœ„ν•΄ λ¬΄κ²Œλ„ μ—¬λŸ¬ 무게λ₯Ό κΈ°μ€€μœΌλ‘œ κ΅¬λΆ„ν•˜λ„λ‘ κ΅¬ν˜„ν•©λ‹ˆλ‹€.

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;
}

 

쒋은 ν•΄κ²°μ±… κ°™μŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ, κ΅¬ν˜„ μ½”λ“œλ₯Ό μžμ„Ένžˆλ³΄λ©΄ for문으둜 inventoryλ₯Ό νƒμƒ‰ν•˜κ³ , 각 사과에 필터링 쑰건을 μ μš©ν•˜λŠ” λΆ€λΆ„μ˜ μ½”λ“œκ°€ 색 필터링 μ½”λ“œμ™€ λŒ€λΆ€λΆ„ μ€‘λ³΅λ©λ‹ˆλ‹€. 그럼, μ„±λŠ₯ κ°œμ„ μ„ μœ„ν•΄ μ€‘λ³΅λ˜λŠ” 뢀뢄을 μˆ˜μ •ν•˜λ €λ©΄ λͺ¨λ“  λ©”μ„œλ“œλ₯Ό μˆ˜μ •ν•˜λŠ” 참사가 λ²Œμ–΄μ§ˆ 것 κ°™λ„€μš”.

 

μ•„μ˜ˆ 색과 무게λ₯Ό ν•˜λ‚˜μ˜ λ©”μ„œλ“œλ‘œ 합쳐 filterλΌλŠ” λ©”μ„œλ“œλ₯Ό λ§Œλ“€μ–΄λ΄…μ‹œλ‹€.

 

 

3. κ°€λŠ₯ν•œ λͺ¨λ“  μ†μ„±μœΌλ‘œ 필터링

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 && apple.getColor().equals(color)) ||
			(!flag && apple.getWeight() > weight)) {
			result.add(apple);
		}
	}
	return result;
}

 

정말 λ”λŸ¬μš΄ μ½”λ“œκ°€ λ˜μ–΄λ²„λ ΈμŠ΅λ‹ˆλ‹€. λ†λΆ€μ˜ μš”κ΅¬μ‚¬ν•­μ΄ 점점 λŠ˜μ–΄λ‚˜λ©΄ λ©”μ„œλ“œμ˜ μΈμˆ˜λ„ 점점 λŠ˜μ–΄λ‚˜κ²Œ 될텐데, 그러면 if문은 또 μˆ˜μ •λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€. 이λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ λ™μž‘ νŒŒλΌλ―Έν„°ν™”λ₯Ό μ΄μš©ν•΄λ³΄λ„λ‘ ν•©μ‹œλ‹€!

 

 

3.2 λ™μž‘ νŒŒλΌλ―Έν„°ν™”

μ•žμ„  예제λ₯Ό 톡해 νŒŒλΌλ―Έν„° μΆ”κ°€κ°€ μ•„λ‹Œ λ³€ν™”ν•˜λŠ” μš”κ΅¬μ‚¬ν•­μ— μ’€ 더 μœ μ—°ν•˜κ²Œ λŒ€μ‘ν•  수 μžˆλŠ” 방법이 ν•„μš”ν•˜λ‹€λŠ” 것을 μ•Œκ²Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€. 선택 쑰건듀을 μ’€ 더 λΆ„μ„ν•΄λ΄…μ‹œλ‹€.

 

μ‚¬κ³Όμ˜ 속성에 κΈ°μ΄ˆν•΄ λΆˆλ¦¬μ–Έ 값을 λ°˜ν™˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. (사과가 녹색인가? yes-no, λ¬΄κ²Œκ°€ 150그램 이상인가? yes-no). μ΄λ ‡κ²Œ μ°Έ λ˜λŠ” 거짓을 λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜λ₯Ό ν”„λ ˆλ””μΌ€μ΄νŠΈλΌκ³  ν•©λ‹ˆλ‹€. 선택 쑰건을 κ²°μ •ν•˜λŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ •μ˜ν•©μ‹œλ‹€.

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());
	}
}

 

μœ„ 쑰건에 따라 filter λ©”μ„œλ“œκ°€ λ‹€λ₯΄κ²Œ λ™μž‘ν•  것이라고 μ˜ˆμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€. 이λ₯Ό μ „λž΅ λ””μžμΈ νŒ¨ν„΄μ΄λΌκ³  ν•©λ‹ˆλ‹€. 이것을 μ΄μš©ν•΄μ„œ filterApplesμ—μ„œ ApplePredicate 객체λ₯Ό λ°›μ•„ μ• ν”Œμ˜ 쑰건을 κ²€μ‚¬ν•˜λ„λ‘ λ©”μ„œλ“œλ₯Ό κ³ μ³λ΄…μ‹œλ‹€!

 

 

4. 좔상적 쑰건으둜 필터링

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;
}

 

처음의 상황과 λΉ„κ΅ν•˜λ©΄ μ™„μ „ μœ μ—°ν•΄μ§„ μ½”λ“œλ₯Ό λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€. μš°λ¦¬κ°€ μ „λ‹¬ν•œ ApplePredicate 객체에 μ˜ν•΄ filterApples λ©”μ„œλ“œμ˜ λ™μž‘μ΄ κ²°μ •λ©λ‹ˆλ‹€. 즉, μš°λ¦¬λŠ” filterApples λ©”μ„œλ“œμ˜ λ™μž‘μ„ νŒŒλΌλ―Έν„°ν™” ν•œ κ²ƒμž…λ‹ˆλ‹€!

 

ν•œ 개의 νŒŒλΌλ―Έν„°, λ‹€μ–‘ν•œ λ™μž‘

μ»¬λ ‰μ…˜ 탐색 둜직과 각 ν•­λͺ©μ— μ μš©ν•  λ™μž‘μ„ λΆ„λ¦¬ν•œλ‹€λŠ” 것이 'λ™μž‘ νŒŒλΌλ―Έν„°'의 κ°•μ μž…λ‹ˆλ‹€.

μ €μž‘μžν‘œμ‹œ (μƒˆμ°½μ—΄λ¦Ό)

'개발' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

MVC νŒ¨ν„΄  (5) 2024.10.15
BeanFactory와 ApplicationContext, Configuration  (0) 2024.10.04
μŠ€ν”„λ§ λΆ€νŠΈλ₯Ό μ΄μš©ν•œ νŽ˜μ΄μ§• κ΅¬ν˜„  (3) 2024.09.27
JWT 토큰을 μ΄μš©ν•œ 둜그인 방법 이해  (5) 2024.09.15
'개발' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • MVC νŒ¨ν„΄
  • BeanFactory와 ApplicationContext, Configuration
  • μŠ€ν”„λ§ λΆ€νŠΈλ₯Ό μ΄μš©ν•œ νŽ˜μ΄μ§• κ΅¬ν˜„
  • JWT 토큰을 μ΄μš©ν•œ 둜그인 방법 이해
율무;
율무;
  • 율무;
    πŸ₯Š
    율무;
  • 전체
    였늘
    μ–΄μ œ
    • λΆ„λ₯˜ 전체보기 (65)
      • 개발 (30)
        • μš°μ•„ν•œν…Œν¬μ½”μŠ€ (13)
        • 운영체제 (12)
      • κ°œλ°œμ„œμ  (2)
        • μžλ°”-μŠ€ν”„λ§ μ‹€μš©μ£Όμ˜ ν”„λ‘œκ·Έλž˜λ° (2)
      • μ•Œκ³ λ¦¬μ¦˜ 문제 (28)
      • κ²Œμž„κ°œλ°œ (5)
  • λΈ”λ‘œκ·Έ 메뉴

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

  • 곡지사항

  • 인기 κΈ€

  • νƒœκ·Έ

    μΈλ””κ²Œμž„
    κ²Œμž„κ°œλ°œ
    bsp
    이것이C++이닀
    개발
    μ•Œκ³ λ¦¬μ¦˜
    λ°±μ€€
    dfs
    python
    둜그라이크
    μ…€λ£°λŸ¬μ˜€ν† λ§ˆνƒ€
    둜그
    2048(Hard)
    μš°μ•„ν•œν…Œν¬μ½”μŠ€
    파이썬
    BFS
    C++
    κ°œλ°œμ—°μŠ΅
    2048
    μ΄λ™μƒμ„±μž
    μš°ν…Œμ½”
    κ²Œμž„
    μžλ°”
    μ ˆμ°¨μ μƒμ„±
    μ½”λ”©
  • 졜근 λŒ“κΈ€

  • 졜근 κΈ€

  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.3
율무;
λ™μž‘ νŒŒλΌλ―Έν„°ν™” μ½”λ“œ μ „λ‹¬ν•˜κΈ°
μƒλ‹¨μœΌλ‘œ

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