【发布时间】:2017-06-11 21:17:18
【问题描述】:
我想在另一个Hashmap中检索一个Hashmap中key的值,
static HashMap<String , HashMap<String, Float>> terms = new HashMap();
static String date;
public static void main(String[] args) throws FileNotFoundException, ParseException, IOException {
InputStream ips=new FileInputStream(filePath);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
while ((ligne=br.readLine())!=null){
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(ligne);
date = (String) jsonObject.get("created_at");
String text = (String) jsonObject.get("text");
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
List<String> ss=TokenizewithAnalyzer.tokenizeString(analyzer, text);
for(String s : ss){
ajoutFrequence(s, date);}
System.out.print("==>"+ss+" \n");}
for(Entry <String, HashMap<String, Float>> entry : terms.entrySet()){
// float res=entry.getValue().get(date).floatValue();
System.out.println(entry.getValue().get(date).floatValue());
}
br.close();
}
static void ajoutFrequence(String token, String date){
if(terms.containsKey(token)){
HashMap<String, Float> freqdate = terms.get(token);
if(freqdate.containsKey(date)){
freqdate.put( date, freqdate.get(date)+1);
}else{
freqdate.put(date, Float.valueOf(1));
}
}else{
HashMap<String, Float> freqdate = new HashMap<>();
freqdate.put(date, Float.valueOf(1));
terms.put(token, freqdate);
} }}
在输出中,我得到了列表中的频率,例如: 无效的 无效的 1.0 无效的 无效的 无效的 我想做这样的事情: float freq=entry.getValue().values();但这是不可能的。提前谢谢你。
【问题讨论】:
-
你想做什么?您想打印条目中所有地图的所有值吗?
-
我需要得到float类型的频率,因为以后我必须对这个float进行计算(乘法,除法)。
-
您不能将数组转换为浮点数...您的 entry.getValue().values() 将返回浮点数集合,
-
以及如何恢复这个浮动?
标签: java