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在循环的时候是不可以删除元素的。
分享到:
相关推荐
看完这篇 HashMap,和面试官扯皮就没问题了 - HashMap 概述 - HashMap 和 HashTable 的区别 - 相同点 - 不同点 - HashMap 和 HashSet 的... - HashMap 的遍历方式 - HashMap 中的移除方法 - 关于 HashMap 的面
Java 实例 - HashMap遍历源代码-详细教程.zip
主要介绍了Java HashMap 如何正确遍历并删除元素的方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Java不同数据存储类型使用不同遍历方法效率研究 GitHub代码仓库 数据存储类型 ArrayList HashSet HashMap LinkedList 遍历方法 传统遍历方法 for(int i=0;i<list.size();i++) { String str = list.get(i); ... ...
哈希树遍历 HashMap遍历和使用 HashMap遍历和使用
在本篇文章中小编给大家分享了关于HashMap和List遍历方法及如何遍历删除元素知识点总结,需要的朋友们参考下。
HashMap的用法---马克-to-win java视频的详细描述与介绍
20-集合框架020-HashMap-1080P 高清-AVC20
主要介绍了Java编程中HashMap的初始化以及遍历的方法,是Java入门学习中的基础知识,需要的朋友可以参考下
讲述了hashmap的几种遍历方法,相当经典
hashMap排序,hashmap使用还是比较频繁。这时自己写的一个实现hashmap排序的例子
其主要特点是通过键的哈希值存储值,并提供了添加、获取和操作存储值的方法。 HashMap的底层数据结构是由数组和链表组成的。数组是HashMap的主体,而链表则是为了解决哈希冲突而存在的。当两个或更多的键的哈希值...
高性能:由于使用了哈希表,HashMap在查找、插入和删除元素时具有较高的性能。但是,如果哈希函数的设计不合理,可能会导致哈希冲突,从而影响性能。 无序:HashMap中的元素没有固定的顺序,可以通过迭代器或for-...
下面小编就为大家带来一篇基于HashMap遍历和使用方法(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
hashmap源码 - - - - - - - - - - 基础篇 1、 Java语言有哪些特点 1、简单易学、有丰富的类库 2、面向对象(Java最重要的特性,让程序耦合度更低,内聚性更高) 3、与平台无关性(JVM是Java跨平台使用的根本) 4、...
HASHMAP排序,对一个无序以KEY为数字的MAP进行排序,比较简单操作
hashmap源码 Excellent-Blog-share Welcome to share excellent blogs . 当你的能力还驾驭不了你的目标时,就应该沉下心来历练 当你的才华还撑不起你的野心时,那你就应该静下心来学习 #目录 ###附录 java系列 java8...
hashmap源码 java-notes 算法 数据结构 设计模式 基础 集合 ConcurrentHashMap IO 并发 [Java 并发]( 并发.md) AQS 源码 JVM web 基础 [NGINX 简介](./docs/nginx/NGINX 简介.md) 框架 Spring [观察 Spring bean ...
hashmap源码 Notes 原理 basic 1 数据结构中各种东西的数量很很重要!!!可以考虑在在数据结构中定义一下 2 Java hashtable的contains用来查找是否存在value,和containsValue类似 查找key使用containskey方法, 3 ...