[μ΄νŽ™ν‹°λΈŒ μžλ°”] Item03 μ™„λ²½κ³΅λž΅. λ©”μ†Œλ“œ μ°Έμ‘° method reference

2023. 1. 10. 09:43ㆍJAVA/Effective JAVA

728x90

 

item03. private μƒμ„±μžλ‚˜ μ—΄κ±° νƒ€μž…μœΌλ‘œ μ‹±κΈ€ν†€μž„μ„ λ³΄μ¦ν•˜λΌ. 

" p24. λ©”μ„œλ“œ μ°Έμ‘°λ₯Ό κ³΅κΈ‰μžλ‘œ μ‚¬μš©ν•  수 μžˆλ‹€. "

 

 

 

λ©”μ„œλ“œ μ°Έμ‘° Method Reference λž€? 

: λ©”μ„œλ“œ ν•˜λ‚˜λ§Œ ν˜ΈμΆœν•˜λŠ” λžŒλ‹€ ν‘œν˜„μ‹μ„ μ€„μ—¬μ“°λŠ” 방법 

 

λ©”μ„œλ“œ μ°Έμ‘°λŠ” λžŒλ‹€ ν‘œν˜„μ‹μ΄ 단 ν•˜λ‚˜μ˜ λ©”μ„œλ“œλ§Œμ„ ν˜ΈμΆœν•˜λŠ” κ²½μš°μ— ν•΄λ‹Ή λžŒλ‹€ ν‘œν˜„μ‹μ—μ„œ  λΆˆν•„μš”ν•œ λ§€κ°œλ³€μˆ˜λ₯Ό μ œκ±°ν•˜κ³  μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•œλ‹€. 

 

ν΄λž˜μŠ€μ΄λ¦„::λ©”μ„œλ“œμ΄λ¦„

μ°Έμ‘°λ³€μˆ˜μ΄λ¦„::λ©”μ„œλ“œμ΄λ¦„ 

public class Person
{
   LocalDate birthday;
   
   public Person()
   {
   }
   
   public Person(LocalDate birthday)
   {
      this.birthday = birthday;
   }
   
   public int getAge()
   {
      return LocalDate.now().getYear() - birthday.getYear();
   }
   
   public static int compareByAge(Person a, Person b)
   {
      return a.birthday.compareTo(b.birthday);
   }
}

Personμ΄λΌλŠ” ν΄λž˜μŠ€κ°€ 있고, 

 

public static void main(String[] args)
{
   List<Person> people = new ArrayList<>();
   people.add(new Person(LocalDate.of(1998, 11, 11)));
   people.add(new Person(LocalDate.of(2000, 1, 1)));
   people.add(new Person(LocalDate.of(1999, 03, 02)));
   
   // 읡λͺ… λ‚΄λΆ€ 클래슀
   people.sort(new Comparator<Person>()
   {
      // Comparator μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•œ κ΅¬ν˜„μ²΄
      @Override
      public int compare(Person o1, Person o2)
      {
         return o1.birthday.compareTo(o2.birthday);
      }
   });
   
   // lambda
   people.sort((p1, p2) -> Person.compareByAge(p1,p2));
   
   // static λ©”μ†Œλ“œ μ°Έμ‘°
   people.sort(Person::compareByAge);
   
   // μƒμ„±μž λ©”μ†Œλ“œ μ°Έμ‘°
   ArrayList<LocalDate> dates = new ArrayList<>();
   dates.add(LocalDate.of(2000, 1, 1));
   dates.add(LocalDate.of(2001, 2, 2));
   dates.add(LocalDate.of(2002, 3, 4));
   
   List<Person> personList = dates.stream().map(Person::new).collect(Collectors.toList());
   
}

λ©”μ„œλ“œ μ°Έμ‘° μ΄μ „μ—λŠ” λžŒλ‹€ ν‘œν˜„μ‹μ΄κ³ , κ·Έ μ΄μ „μ—λŠ” 읡λͺ… λ‚΄λΆ€ ν΄λž˜μŠ€λΌλŠ” 것이 μžˆλ‹€. 

읡λͺ… λ‚΄λΆ€ 클래슀둜 보면 μš°μ„  ComparatorλΌλŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό 읡λͺ… λ‚΄λΆ€ ν΄λž˜μŠ€μ—μ„œ κ΅¬ν˜„ν•œλ‹€. 

그리고 λžŒλ‹€κ°€ 이λ₯Ό μ’€ 더 κ°„λ‹¨ν•˜κ²Œ ν‘œν˜„ν•  수 μžˆλ„λ‘ ν•΄μ€€λ‹€. 

λ§ˆμ§€λ§‰μœΌλ‘œ λ©”μ„œλ“œ μ°Έμ‘°λŠ” 이제 이 λžŒλ‹€κ°€ λ©”μ„œλ“œ ν•˜λ‚˜λ§Œ ν˜ΈμΆœν•œλ‹€λ©΄ :: 기호λ₯Ό μ‚¬μš©ν•΄μ„œ μ’€ 더 μ€„μ—¬μ„œ ν‘œν˜„ν•  수 μžˆλ‹€. 

 

μœ„μ— μ˜ˆμ œμ—μ„œ compareByAgeκ°€ staticλ©”μ„œλ“œλΌμ„œ ν΄λž˜μŠ€μ΄λ¦„::λ©”μ„œλ“œμ΄λ¦„μœΌλ‘œ λ©”μ„œλ“œ μ°Έμ‘°λ₯Ό ν–ˆμ§€λ§Œ, 

λ§Œμ•½ static λ©”μ„œλ“œκ°€ μ•„λ‹ˆλΌλ©΄ 객체λ₯Ό ν•˜λ‚˜ μƒμ„±ν•œ λ‹€μŒ μ°Έμ‘°λ³€μˆ˜μ΄λ¦„::λ©”μ„œλ“œμ΄λ¦„μœΌλ‘œ ν˜ΈμΆœν•  수 μžˆλ‹€. 

 

그리고 μƒμ„±μžλ₯Ό ν˜ΈμΆœν•˜λŠ” λ©”μ„œλ“œλ„ λ©”μ„œλ“œ μ°Έμ‘°λ₯Ό μ΄μš©ν•  수 μžˆλŠ”λ°, λ‹¨μˆœνžˆ 객체λ₯Ό μƒμ„±ν•˜κ³  λ°˜ν™˜ν•˜λŠ” λžŒλ‹€ ν‘œν˜„μ‹μ€ λ©”μ„œλ“œ 참쑰둜 λ³€ν™˜ν•  수 μžˆλ‹€. 

 

μ—¬κΈ°μ„œ μ§€κΈˆ Person ν΄λž˜μŠ€λŠ” νŒŒλΌλ―Έν„°κ°€ μžˆλŠ” μƒμ„±μžμ™€ κΈ°λ³Έ μƒμ„±μž 2κ°œκ°€ μžˆμ–΄μ„œ κ·Έλƒ₯ Person::new ν•˜λ©΄ μ—λŸ¬κ°€ λ‚œλ‹€. 

μƒμ„±μž λ©”μ„œλ“œ μ°Έμ‘°λŠ” ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€μ— λ‹΄μœΌλ©΄ λ˜λŠ”λ° 

νŒŒλΌλ―Έν„°κ°€ μžˆλŠ” μƒμ„±μžλΌλ©΄ Function<T, R> 을 μ‚¬μš©ν•˜κ³ , κΈ°λ³Έ μƒμ„±μžλΌλ©΄ λ¦¬ν„΄λ§Œ μžˆλŠ” Supplierλ₯Ό μ‚¬μš©ν•˜λ©΄ λœλ‹€. 

 

 

 

 

λ©”μ„œλ“œ μ°Έμ‘° Method Reference μ‚¬μš© 방법 

 

1️⃣ 정적 λ©”μ„œλ“œ μ°Έμ‘° 

// 1. 정적 λ©”μ„œλ“œ μ°Έμ‘°
ToIntFunction<String> stringToIntFunction = (String s) -> Integer.parseInt(s); // lambda
ToIntFunction<String> stringToIntFunction1 = Integer::parseInt; // method reference

 

2️⃣ μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œ μ°Έμ‘° 

// 2. μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œ μ°Έμ‘°
Person person = new Person();
Supplier<Integer> getAge = person::getAge;

 

3️⃣ μƒμ„±μž λ©”μ„œλ“œ μ°Έμ‘° 

// 3. μƒμ„±μž λ©”μ„œλ“œ μ°Έμ‘° 
Function<LocalDate, Person> function = Person::new;
Supplier<Person> supplier = Person::new;

 

 

 

πŸ’Œ 읡λͺ… λ‚΄λΆ€ 클래슀λ₯Ό λžŒλ‹€μ‹μœΌλ‘œ λ³€ν™˜ν•˜λ©΄μ„œ μ½”λ“œλ₯Ό 더 쀄이고, 가독성 있게 κ΅¬ν˜„ν–ˆμœΌλ©°, λžŒλ‹€μ‹μ—μ„œ λ©”μ„œλ“œ ν•˜λ‚˜λ§Œ ν˜ΈμΆœν•˜λŠ”κ²Œ 닀라면 λ©”μ„œλ“œ μ°Έμ‘°λ₯Ό ν†΅ν•΄μ„œ μ’€ 더 κ°€λ…μ„±μžˆκ²Œ ν‘œν˜„ν•  수 μžˆλ‹€. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90