Java

[Effective Java] ์•„์ดํ…œ 30. ์ด์™•์ด๋ฉด ์ œ๋„ค๋ฆญ ๋ฉ”์„œ๋“œ๋กœ ๋งŒ๋“ค๋ผ.

quedevel 2023. 3. 21. 09:51
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ ์•„์ดํ…œ 30. ์ด์™•์ด๋ฉด ์ œ๋„ค๋ฆญ ๋ฉ”์„œ๋“œ๋กœ ๋งŒ๋“ค๋ผ.

์ œ๋„ค๋ฆญ ํƒ€์ž…๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ํด๋ผ์ด์–ธํŠธ์—์„œ ์ž…๋ ฅ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋ฐ˜ํ™˜๊ฐ’์„ ๋ช…์‹œ์ ์œผ๋กœ ํ˜•๋ณ€ํ™˜ํ•ด์•ผ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ณด๋‹ค
์ œ๋„ค๋ฆญ ๋ฉ”์„œ๋“œ๊ฐ€ ๋” ์•ˆ์ „ํ•˜๋ฉฐ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ์‰ฝ๋‹ค.


  • ๋กœ ํƒ€์ž… ์‚ฌ์šฉ - ์ˆ˜์šฉ ๋ถˆ๊ฐ€!
    public static Set union(Set s1, Set s2) {
      Set result = new HashSet<>(s1);
      result.addAll(s2);
      return result;
    }

์ปดํŒŒ์ผ์€ ๋˜์ง€๋งŒ Raw use of parameterized class 'Set', Unchecked call to 'addAll(Collection<? extends E>)' as a member of raw type 'java.util.Set' ์ด๋Ÿฌํ•œ ๋น„๊ฒ€์‚ฌ ๊ฒฝ๊ณ ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. ์‹ค์ œ IntelliJ์—์„œ ์†Œ์Šค๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด ๋…ธ๋ž€์ค„์ด ๋ฒ…๋ฒ… ๊ฐ€์žˆ๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.



  • ๋น„๊ฒ€์‚ฌ ๊ฒฝ๊ณ ๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ์ œ๋„ค๋ฆญ ๋ฉ”์„œ๋“œ๋กœ ๋ฆฌํŽ™ํ† ๋ง
public static <E> Set<E> union(Set<E> s1, Set<E> s2) {
    Set<E> result = new HashSet<>(s1);
    result.addAll(s2);
    return result;
}

๋ฉ”์„œ๋“œ ์„ ์–ธ์—์„œ์˜ ์„ธ ์ง‘ํ•ฉ์˜ ์›์†Œ ํƒ€์ž…์„ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ช…์‹œํ•˜๊ณ , ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ๋„ ์ด ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๊ฒŒ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค.


์ฐธ๊ณ  ์ž๋ฃŒ

Joshua Bloch, ใ€ŽEffective Java 3/Eใ€, ๊ฐœ์•ž๋งต์‹œ ์˜ฎ๊น€, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ธ์‚ฌ์ดํŠธ(2018)
http://www.kyobobook.co.kr/product/detailViewKor.laf?ejkGb=KOR&mallGb=KOR&barcode=9788966262281&orderClick=LEa&Kc=

728x90
๋ฐ˜์‘ํ˜•