중복 된 값만 검색하고 싶지만 올바른 결과를 찾을 수없는 다음 코드가 있습니다.
public class TestDummy {
public static void main(String args[]){
String arr[] ={"lady", "bird", "is","bird","lady","cook"};
int len = arr.length;
System.out.println("Size "+len);
for(int i=0 ; i<=len;i++){
for(int j=1 ; j< len-1;j++){
if(arr[i]==arr[j]){
System.out.println("Duplicate "+arr[i]);
}
}
}
}
}
String arr[] ={"lady", "bird", "is","bird","lady","cook"};
Map<String, Integer> map = new HashMap<>();
for(String str: arr) {
if(map.containsKey(str)) {
map.put(str, map.get(str)+1);
} else{
map.put(str, 1);
}
}
for(String str: map.keySet()) {
if(map.get(str) > 1) {
System.out.println("Duplicate: "+ str+" count:"+map.get(str));
}
}
산출:
Duplicate: bird count:2
Duplicate: lady count:2
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다