java 程序编程出现警告

源代码wordRate.java用于计算文章单词词频,如下:
import java.util.*;
import java.io.*;

public class Test{

public static void main(String[] args) throws Exception {

BufferedReader infile =new BufferedReader(new FileReader("source.txt"));//打开一篇文章source.txt

String string;
String words[];//用于存放文章所有单词
String file = null;

while ((string = infile.readLine()) != null) {
file += string;//读出整篇文章,存入String中。
}

file=file.toLowerCase();//所有字母小写化
file=file.replaceAll("[^A-Za-z]"," ");//正则表达式,匹配非英文字符为空格

words=file.split("\\s");//取出单词,空格为单词分界,将分词后的单词存入数组words中

Map hashMap = new HashMap();

for(int i=0;i<words.length;i++){
String key = words[i];
if (hashMap.get(key) != null) {
int value = ((Integer) hashMap.get(key)).intValue();
value++;
hashMap.put(key, new Integer(value));

} else {
hashMap.put(key, new Integer(1));
}
}

//按照单词字母的先后次序输出。
Map treeMap = new TreeMap(hashMap);
Set entrySet = treeMap.entrySet();
Iterator iterator = entrySet.iterator();

while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
请问:
1、为什么编译会出现如所示警告?
2、Set entrySet = treeMap.entrySet();
Iterator iterator = entrySet.iterator(); 这两句是什么意思?
3、输出时希望按词频大小输出,那如何修改该代码呢?

1.告警是你使用Map时没有指定具体类型,你可以为其指定相应的类型Map<String,Integer>map = new HashMap<String,Integer>();

2.第一句是获取Map的Entry集合,你可以查看JDK帮助文档看看Entry的数据结构.
第二句是获取Set的迭代器,后面的代码是通过迭代器,迭代出每个Entry对象.

3.自己写个冒泡算法,把Map中的记录排序下.
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-12-27
Map是属于泛型:Map<String,Object> map = new TreeMap<String,Objcet>();
如果你没有设定泛型,Java会认为不安全,但是可以通过,你可以找一些泛型的资料学习一下,就知道其中的原理了!
第2个回答  2011-12-27
没错啊,我复制过来运行就没问题啊追问

但编译出现警告,为什么呢?

追答

没看见,啊,哦,我直接在eclipse下运行的,是什么警告

相似回答