利用java集合框架统计单词的频率

import java.util.*;


public class Test1 {
	static Map<Character, Integer> map =new HashMap<>();
	static Set<String> set =new HashSet<>();
	public static void PrintMap(){
		Set<Character> set2=map.keySet();
		Iterator<Character> iterator=set2.iterator();
		while(iterator.hasNext()){
			char xx=iterator.next();
			System.out.println(xx+" "+map.get(xx));
		}
	}
	public static void main(String []args){
		char ch[]=new char[100];
	//	Scanner sc=new Scanner(System.in);
		String s="hellowword";
		ch=s.toCharArray();

		for(int j=0;j<ch.length;j++){
			
			if(map.containsKey(ch[j])){
				int x=map.get(ch[j]);
				x++;
				map.put(ch[j],x);
				
			}else{
				map.put(ch[j],1);
				
			}
		}
		
	
		
		PrintMap();

		
		
		
		
		
	}
	
	 


}

你可能感兴趣的:(利用java集合框架统计单词的频率)