`

HashMap的操作-统计词频、遍历、排序、删除

    博客分类:
  • Java
阅读更多
package com.DocExpansion.TFIDF;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class TFfreq {
	public static HashMap<String,String> TFfreq(ArrayList<String> terms){
		//计算词频
		HashMap<String,String> resTF=new HashMap<String,String>();
		HashMap<String,Integer> TFtimes=TFfreq.TFtimes(terms);
		int length=terms.size();
		System.out.println(length);
		//遍历Map的value
		Iterator iter=TFtimes.entrySet().iterator();
		while(iter.hasNext()){
			 Map.Entry entry=(Map.Entry)iter.next();
			 //当两个整数相除时,由于小数点以后的数字会被截断,使运算结果为整数,
			 //故此时若希望得到运算结果为所预期的浮点数,则此时必须将两整数其一或是全部强制转换类型为浮点数。
			 DecimalFormat decimalFormat = new DecimalFormat("#,##0.0000000000");//格式化设置
			 float freq=Float.parseFloat(entry.getValue().toString())/length;
			 resTF.put(entry.getKey().toString(),decimalFormat.format(freq));
		}
		return resTF;
	}
	public static HashMap<String,Integer> TFtimes(ArrayList<String> terms){
		//统计词出现的次数
		HashMap<String,Integer> TFtimes=new HashMap<String,Integer>();
		for(String term:terms){
			if(!TFtimes.containsKey(term)){
				TFtimes.put(term, 1);
			}
			else {
				TFtimes.put(term, TFtimes.get(term)+1);
			}
		}
		//因为Iterator在循环的时候是不可以删除元素的。
		/*Iterator it=TFtimes.keySet().iterator();
		while(it.hasNext()){
			Object key=it.next();
			if(TFtimes.get(key)>2&&TFtimes.get(key)<1000){
				TFtimes.remove(key);
			}
		}*/
	//遍历HashMap	
		//1、使用迭代器遍历Map的值,根据键取值 
		Iterator it=TFtimes.keySet().iterator();
		while(it.hasNext()){
			Object key=it.next();
			Object value=TFtimes.get(key);
			System.out.println("-------"+key+"  "+value);
		}
		//2、使用迭代器遍历Map的记录Map.Entry
		Iterator iter=TFtimes.entrySet().iterator();
		while(iter.hasNext()){
			//一个Map.Entry代表一条记录
			Map.Entry entry=(Map.Entry)iter.next();
			//通过entry可以获得记录的键和值
			Object key=entry.getKey();
			Object value=entry.getValue();
			System.out.println("++++++++"+key+"  "+value);
		}
		return TFfreq.Sort(TFtimes);
	}
	//排序HashMap并删除元素
	public static HashMap<String,Integer> Sort(HashMap<String,Integer> map){
		HashMap<String,Integer> resMap=new HashMap<String,Integer>();
		List<Map.Entry<String, Integer>> infoIds=new ArrayList<Map.Entry<String,Integer>>(map.entrySet());
		Collections.sort(infoIds,new Comparator<Map.Entry<String, Integer>>(){
			public int compare(Map.Entry<String, Integer> o1,Map.Entry<String, Integer> o2){
				//根据value排序,整型
				return(o1.getValue()-o2.getValue());
				//根据key排序,字符型
			//	return(o1.getKey().toString().compareTo(o2.toString()));
			}
		});
		//根据值的大小删除元素
		for(int i=0;i<infoIds.size();i++){
			Entry<String,Integer> id=infoIds.get(i);
			if(id.getValue()>2&&id.getValue()<1000){
				resMap.put(id.getKey(), id.getValue());
			}
		}
		return resMap;
	}
}



删除集合中的某些元素。有些可能会这么写。
Iterator it=TFtimes.keySet().iterator();
		while(it.hasNext()){
			Object key=it.next();
			if(TFtimes.get(key)>2&&TFtimes.get(key)<1000){
				TFtimes.remove(key);
			}
		}

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
at java.util.HashMap$KeyIterator.next(HashMap.java:828)


因为Iterator在循环的时候是不可以删除元素的。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics