관리 메뉴

βœπŸ»κΈ°λ‘ν•˜λŠ” λΈ”λ‘œκ·Έ

μ •κ·œ ν‘œν˜„ 식 Pattern 객체의 생성 λΉ„μš©μ„ μ€„μ΄μž λ³Έλ¬Έ

Language/Java

μ •κ·œ ν‘œν˜„ 식 Pattern 객체의 생성 λΉ„μš©μ„ μ€„μ΄μž

μ©μ‹œν‚΄ 2023. 2. 19. 01:15
728x90

객체 생성 λΉ„μš©μ΄ λΉ„μ‹Ό 객체

EffectiveJava item6 - λΆˆν•„μš”ν•œ 객체 생성을 ν”Όν•˜λΌ λΌλŠ” λ‚΄μš©μ„ νšŒμ‚¬ μ½”λ“œμ— μ μš©ν•œ λ‚΄μš©μ„ μ •λ¦¬ν•˜μ—¬ κ³΅μœ ν•©λ‹ˆλ‹€.

  • μŠ€ν„°λ”” 정리 λ‚΄μš©

https://github.com/WanOnPreStudy/EffectiveJavaStudy/blob/main/Chapter2/item_6_%EA%B9%80%EC%A0%95%ED%9D%AC.md

 

 

μ±…μ—μ„œλŠ” μ•„λž˜μ™€ 같이 μ„€λͺ…ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

  • 객체 생성 λΉ„μš©μ΄ λΉ„μ‹Ό κ°μ²΄λŠ” λ°˜λ³΅ν•΄μ„œ μƒμ„±ν•˜κΈ°λ³΄λ‹€, μΊμ‹±ν•˜μ—¬κ°μ²΄λ₯Ό μž¬μ‚¬μš©ν•˜λŠ” 것이 μ’‹λ‹€.
    • 객체 생성 λΉ„μš©μ΄ λΉ„μ‹Ό 객체 = cpu λ¦¬μ†ŒμŠ€ μ‚¬μš© 倚
      • μ •κ·œν‘œν˜„μ‹μ˜ 경우 객체 생성 μ‹œ 생성 λΉ„μš©μ΄ λΉ„μ‹Έλ‹€.

String의 matches()
Pattern객체의 matches()

μ •κ·œ ν‘œν˜„μ‹μ„ 톡해 λ¬Έμžμ—΄μ„ 검사할 λ•Œ λ‚΄λΆ€ λ©”μ„œλ“œλ₯Ό 듀여닀보면 μ •κ·œν‘œν˜„μ‹μ„ νŒŒλΌλ―Έν„°λ‘œ λ°›μ•„ λ‚΄λΆ€μ μœΌλ‘œ Pattern μΈμŠ€ν„΄μŠ€λ‘œ μ»΄νŒŒμΌν•˜λŠ” 것을 확인할 수 μžˆλ‹€.


μ½”λ“œ κ°œμ„ ν•˜κΈ°

νšŒμ‚¬μ—μ„œ ν¬λ‘€λ§ν•œ ν…μŠ€νŠΈ λ°μ΄ν„°μ—μ„œ ν•„μš”ν•œ ν•­λͺ©λ§Œ μΆ”μΆœν•˜κΈ° μœ„ν•΄ κΈ°μ‘΄μ—λŠ” μ•„λž˜μ™€ 같이 λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œλ§ˆλ‹€ Pattern객체λ₯Ό μ»΄νŒŒμΌν•˜μ—¬ λΉ„νš¨μœ¨μ μΈ 객체 생성을 ν•˜κ³  μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

@Service
public class RegexService {
    private RegexRepository regexRepository;
   
    public RegexService(RegexRepository regexRepository) {
        this.regexRepository = regexRepository;
    }
    
   /**
     * email μΆ”μΆœ
     * @param s
     */
    public void startTest() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<String> combinedText = getCombinedText();
        for (String s : combinedText) {
            extractEmail(s);
            extractTel(s);
        }
        stopWatch.stop();
        System.out.println("total Time = " + stopWatch.getTotalTimeMillis()); ;
    }

    private void extractEmail(String s) {
        String regex = "[a-zA-Z0-9+-\\_.]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+";

        Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            System.out.println("Full match Email : " + matcher.group(0));
        }
    }

   /**
     * tel μΆ”μΆœ
     * @param s
     */
    private void extractTel(String s) {
        String regex = "\\d{2,3}-\\d{3,4}-\\d{4}$";
        Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            System.out.println("Full match Tel : " + matcher.group(0));
        }
    }
    
    /**
     * table column ν•˜λ‚˜μ˜ λ°μ΄ν„°λ‘œ ν•©μΉ˜λŠ” λ©”μ„œλ“œ
     * @return
     */
    private List<String> getCombinedText() {
        List<SeoulBidData> seoulBidDataList = regexRepository.findAll();
        List<String> combinTxtList = new ArrayList<>();
        seoulBidDataList.forEach(data -> {
            StringBuilder sb = new StringBuilder();
            sb.append(data.getPostId() + " ");
            sb.append(data.getPostName() + " ");
            sb.append(data.getCreateDate() + " ");
            sb.append(data.getUpdateDate() + " ");
            sb.append(data.getOrgan() + " ");
            sb.append(data.getPosition() + " ");
            sb.append(data.getTel() + " ");
            sb.append(data.getEmail() + " ");
            sb.append(data.getTagWord() + " ");
            sb.append(data.getContents() + " ");
            sb.append(data.getStartDate() + " ");
            sb.append(data.getEndDate() + " ");
            sb.append(data.getDateCheck() + " ");
            sb.append(data.getFileName() + " ");
            sb.append(data.getFileCount() + " ");
            combinTxtList.add(sb.toString());
        });
        return combinTxtList;
    }
}

 

Effective Javaμ—μ„œ μ†Œκ°œν•˜λŠ” κ²ƒμ²˜λŸΌ 객체 μƒμ„±λΉ„μš©μ΄ 큰 객체일 경우 μΊμ‹±ν•˜μ—¬ κ°μ²΄λ₯Ό μž¬μ‚¬μš©ν•˜κΈ° μœ„ν•΄ Pattern객체λ₯Ό private static final둜 μƒμˆ˜ν™” ν•˜μ—¬ 객체 생성 λΉ„μš©μ„ κ°μ†Œμ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

package com.example.regextest;

import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class RegexService {
    private RegexRepository regexRepository;
    private static final Pattern EMAIL_PATTERN = Pattern.compile("\"[a-zA-Z0-9+-\\_.]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+\"");
    private static final Pattern TEL_PATTERN = Pattern.compile("\\d{2,3}-\\d{3,4}-\\d{4}$");

    public RegexService(RegexRepository regexRepository) {
        this.regexRepository = regexRepository;
    }

    public void startTest() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<String> combinedText = getCombinedText();
        for (String s : combinedText) {
            extractEmail(s);
            extractTel(s);
        }
        stopWatch.stop();
        System.out.println("total Time = " + stopWatch.getTotalTimeMillis()); ;
    }

    private void extractEmail(String s) {
        Matcher matcher = EMAIL_PATTERN.matcher(s);
        if (matcher.find()) {
            System.out.println("Full match Email : " + matcher.group(0));
        }
    }

    private void extractTel(String s) {
        Matcher matcher = TEL_PATTERN.matcher(s);
        if (matcher.find()) {
            System.out.println("Full match Tel : " + matcher.group(0));
        }
    }

    private List<String> getCombinedText() {
        List<SeoulBidData> seoulBidDataList = regexRepository.findAll();
        List<String> combinTxtList = new ArrayList<>();
        seoulBidDataList.forEach(data -> {
            StringBuilder sb = new StringBuilder();
            sb.append(data.getPostId() + " ");
            sb.append(data.getPostName() + " ");
            sb.append(data.getCreateDate() + " ");
            sb.append(data.getUpdateDate() + " ");
            sb.append(data.getOrgan() + " ");
            sb.append(data.getPosition() + " ");
            sb.append(data.getTel() + " ");
            sb.append(data.getEmail() + " ");
            sb.append(data.getTagWord() + " ");
            sb.append(data.getContents() + " ");
            sb.append(data.getStartDate() + " ");
            sb.append(data.getEndDate() + " ");
            sb.append(data.getDateCheck() + " ");
            sb.append(data.getFileName() + " ");
            sb.append(data.getFileCount() + " ");
            combinTxtList.add(sb.toString());
        });
        return combinTxtList;
    }
}

μ˜ˆμ‹œ λ°μ΄ν„°λŠ” 곡곡데이터 ν¬ν„Έμ—μ„œ λ‹€μš΄λ‘œλ“œν•˜μ—¬ ν…ŒμŠ€νŠΈν–ˆμŠ΅λ‹ˆλ‹€.(https://www.data.go.kr/data/15072302/fileData.do)

총 36819건 λ°μ΄ν„°λ‘œ ν…ŒμŠ€νŠΈν•œ κ²°κ³Ό μ•„λž˜μ™€ 같이 12.311초 속도가 κ°œμ„ λœ 것을 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

κ°œμ„  μ „
κ°œμ„  ν›„

 

728x90
λ°˜μ‘ν˜•

'Language > Java' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

JVM의 Garbage Collector  (0) 2022.10.28
Annotation μ• λ…Έν…Œμ΄μ…˜  (1) 2021.02.04
Enum  (0) 2021.01.28
Thread - λ©€ν‹°μ“°λ ˆλ“œ ν”„λ‘œκ·Έλž˜λ°  (0) 2021.01.22
μ˜ˆμ™Έμ²˜λ¦¬  (0) 2021.01.14