java使用map统计某个数组中单词出现的次数

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
 
 
public class MapTest {
    public static void main(String[] args) {
        String[] data = {"hello","world","tt","hello","world","hello","world"};
        Map<String,Integer> map = new HashMap<String,Integer>();
        for(String str : data){
            if(map.containsKey(str)){
                map.put(str, map.get(str) + 1);
            }else{
                map.put(str, 1);
            }
        }
        Iterator it = map.entrySet().iterator();
        while(it.hasNext()){
            Entry ex = (Entry) it.next();
            System.out.println(ex.getKey() + ":" + ex.getValue());
        }
 
    }
 
}
点赞