例如:数组里有元素,{"aa","bb","cc","aa","cc","aa"},计算出个元素出现的次数。
结果应为:aa出现3次,bb出现1次,cc出现2次。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class Test {
/**
* code:JAVA
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String, Integer> map = new HashMap<String, Integer>();
String[] str = {"aa","bb","cc","aa","cc","aa"};
List l;
l = Arrays.asList(str);
for(int i = 0;i<l.size();i++){
if(map.containsKey(l.get(i))){
int num = map.get(l.get(i));
num++;
map.put((String) l.get(i), num);
}else{
map.put((String) l.get(i), 1);
}
}
System.out.println(map);
}
}