개발중/Java
문자열이 List에 포함 되었는지 여부 확인
Binsoo
2021. 5. 18. 15:49
728x90
반응형
String[] columnNameList = {"category_use", "category_public", "category_name"};
입력받는 문자열이 columnNameList 에 포함되었는지의 여부를 'for 문을 돌려서 문자열이 같다면 ?' 으로 구현하고 있었다.
int columnNameCheck = 0;
for( String columnName : columnNameList) {
if(vo.getCategory_field_name().equals(columnName)) {
columnNameCheck++;
}
}
if(columnNameCheck==0) {
throw new RuntimeException("UPDATE CATEGORY ERROR - Category use / Category public / Category Name 만 수정 가능합니다.");
}
이렇게 하면 한 줄이면 끝나는데 😂
boolean isCheck = Arrays.stream(columnNameList).anyMatch(입력받은 문자열::equals);
빈수 코드 😅
String[] binsooList = {"AAA", "BBB", "CCC"};
public void binsooTest( String str ) {
// 필드명
int check = 0;
for( String binsooStr : binsooList) {
if( binsooStr.equals(str)) {
check++;
}
}
if(check==0) {
throw new RuntimeException("str 은 binsooList에 없어 ");
}
}
다영선배 코드 🤗
import java.util.Arrays;
import org.apache.commons.lang3.StringUtils;
public void binsooTest( String str ) {
String[] binsooList = {"AAA", "BBB", "CCC"};
boolean isCheck = Arrays.stream(binsooList).anyMatch(str::equals);
if(isCheck) {
throw new RuntimeException("str 은 binsooList에 없어 ");
}
}
728x90
반응형