Java

[Effective Java] ์•„์ดํ…œ 45. ์ŠคํŠธ๋ฆผ์€ ์ฃผ์˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ผ.

quedevel 2023. 3. 22. 15:28
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ ์•„์ดํ…œ 45. ์ŠคํŠธ๋ฆผ์€ ์ฃผ์˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ผ.

์ŠคํŠธ๋ฆผ API๋Š” ๋‹ค์žฌ๋‹ค๋Šฅํ•˜์—ฌ ์‚ฌ์‹ค์ƒ ์–ด๋– ํ•œ ๊ณ„์‚ฐ์ด๋ผ๋„ ํ•ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ด์ง€, ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋œป์€ ์•„๋‹ˆ๋‹ค!!

์ŠคํŠธ๋ฆผ์„ ์ œ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์งง๊ณ  ๊น”๊ธˆํ•ด์ง€์ง€๋งŒ, ์ž˜๋ชป ์‚ฌ์šฉํ•˜๋ฉด ์ฝ๊ธฐ ์–ด๋ ต๊ณ  ์œ ์ง€๋ณด์ˆ˜๋„ ํž˜๋“ค์–ด์ง„๋‹ค.


  • ์‚ฌ์ „ ํ•˜๋‚˜๋ฅผ ํ›‘์–ด ์›์†Œ ์ˆ˜๊ฐ€ ๋งŽ์€ ์•„๋‚˜๊ทธ๋žจ ๊ทธ๋ฃน๋“ค์„ ์ถœ๋ ฅํ•œ๋‹ค.

    public class IterativeAnagrams {
      public static void main(String[] args) throws IOException {
          String[] sArr = new String[]{"abc","euq","test","zzfq","que","acb"};
          int minGroupSize = Integer.parseInt("1");
    
          Map<String, Set<String>> groups = new HashMap<>();
    
          for (String s : sArr) {
              String word = s;
              groups.computeIfAbsent(alphabetize(word),
                      (unused) -> new TreeSet<>()).add(word);
          }
    
          for (Set<String> group : groups.values())
              if (group.size() >= minGroupSize)
                  System.out.println(group.size() + ": " + group);
      }
    
      private static String alphabetize(String s) {
          char[] a = s.toCharArray();
          Arrays.sort(a);
          return new String(a);
      }
    }

  • ์ŠคํŠธ๋ฆผ์„ ๊ณผํ•˜๊ฒŒ ์‚ฌ์šฉํ–ˆ๋‹ค.

    public class StreamAnagrams {
      public static void main(String[] args) throws IOException {
          int minGroupSize = Integer.parseInt("1");
          Stream<String> words = Stream.of("abc","euq","test","zzfq","que","acb");
    
          words.collect(
                          groupingBy(word -> word.chars().sorted()
                                  .collect(StringBuilder::new,
                                          (sb, c) -> sb.append((char) c),
                                          StringBuilder::append).toString()))
                  .values().stream()
                  .filter(group -> group.size() >= minGroupSize)
                  .map(group -> group.size() + ": " + group)
                  .forEach(System.out::println);
      }
    }
  • ์ŠคํŠธ๋ฆผ์„ ๊ณผ์šฉํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์ฝ๊ฑฐ๋‚˜ ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์–ด๋ ค์›Œ์ง„๋‹ค.*


  • ์ŠคํŠธ๋ฆผ์„ ์ ์ ˆํžˆ ํ™œ์šฉํ•˜๋ฉด ๊น”๋”ํ•˜๊ณ  ๋ช…๋ฃŒํ•ด์ง„๋‹ค.

    public class HybridAnagrams {
      public static void main(String[] args) throws IOException {
          Stream<String> words = Stream.of("abc","euq","test","zzfq","que","acb");
          int minGroupSize = Integer.parseInt("1");
    
          words.collect(groupingBy(word -> alphabetize(word)))
                  .values().stream()
                  .filter(group -> group.size() >= minGroupSize)
                  .forEach(g -> System.out.println(g.size() + ": " + g));
      }
    
      private static String alphabetize(String s) {
          char[] a = s.toCharArray();
          Arrays.sort(a);
          return new String(a);
      }
    }

๊ธฐ์กด ์ฝ”๋“œ๋Š” ์ŠคํŠธ๋ฆผ์„ ์‚ฌ์šฉํ•˜๋„๋ก ๋ฆฌํŒฉํ„ฐ๋งํ•˜๋˜, ์ƒˆ ์ฝ”๋“œ๊ฐ€ ๋” ๋‚˜์•„ ๋ณด์ผ ๋•Œ๋งŒ ๋ฐ˜์˜ํ•˜์ž.

์ŠคํŠธ๋ฆผ์ด ์•„์ฃผ ์•ˆ์„ฑ๋งž์ถค์ธ ์กฐ๊ฑด

  1. ์›์†Œ๋“ค์˜ ์‹œํ€€์Šค๋ฅผ ์ผ๊ด€๋˜๊ฒŒ ๋ณ€ํ™˜ํ•œ๋‹ค.
  2. ์›์†Œ๋“ค์˜ ์‹œํ€€์Šค๋ฅผ ํ•„ํ„ฐ๋งํ•œ๋‹ค.
  3. ์›์†Œ๋“ค์˜ ์‹œํ€€์Šค๋ฅผ ํ•˜๋‚˜์˜ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•ด ๊ฒฐํ•ฉํ•œ๋‹ค.
  4. ์›์†Œ๋“ค์˜ ์‹œํ€€์Šค๋ฅผ ์ปฌ๋ ‰์…˜์— ๋ชจ์€๋‹ค.
  5. ์›์†Œ๋“ค์˜ ์‹œํ€€์Šค์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์›์†Œ๋ฅผ ์ฐพ๋Š”๋‹ค.

์ฐธ๊ณ  ์ž๋ฃŒ

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
๋ฐ˜์‘ํ˜•