import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BlackWordsUtil {
	public static boolean isBlackWords(List<String> blackWords, String word) {
		Set<String> filteredBlackWords = getFiteredBlackWords(blackWords, word);
		return (filteredBlackWords.size() > 0) ? true : false;
	}
	
	public static Set<String> getFiteredBlackWords(List<String> blackWords, String word) {
		if(word == null) {
			return new HashSet<>();
		}
		
		String blackWordsRegEx = "";
		for(String bWord : blackWords) {
			blackWordsRegEx +=  bWord + "|";
		}

		if(blackWordsRegEx.length() > 0) {
			blackWordsRegEx = blackWordsRegEx.substring(0, blackWordsRegEx.length() - 1);
		}
		
		Set<String> fiteredBlackWords = new HashSet<>();
		Pattern p = Pattern.compile(blackWordsRegEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(word);
		while(m.find()) {
			fiteredBlackWords.add(m.group());
		 }
		
		return fiteredBlackWords;
	}
}

'language > java' 카테고리의 다른 글

리플렉션이란  (0) 2020.09.03
자바 List 중복제거  (0) 2020.08.20
자바 배열을 List처럼 사용하기(Arrays.asList())  (0) 2020.08.14
자바 stream Grouping  (0) 2020.08.13
자주 사용되는 Stream  (1) 2020.08.07

+ Recent posts