黑马程序员技术交流社区
标题:
哪位大神给说一说,求解释
[打印本页]
作者:
环球信息中心
时间:
2014-4-22 20:53
标题:
哪位大神给说一说,求解释
package javase2.day03;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class CharCounterDemo {
public static void main(String[] args)
throws IOException {
String file = "src/javase2/day03/book.txt";
Map<Character, Integer> map = countAll(file, "GBK");
//System.out.println(map);
int all = 0;//字符数量
Collection<Integer> values = map.values();
// for(Iterator<Integer> i=values.iterator(); i.hasNext();){
// int n = i.next();
// all+=n;
// }
for(int n:values){
all+=n;
}
//排序输出:
List<Entry<Character, Integer>> list =
new ArrayList<Map.Entry<Character,Integer>>(
map.entrySet());
Collections.sort(list,
new Comparator<Entry<Character, Integer>>() {
public int compare(Entry<Character, Integer> o1,
Entry<Character, Integer> o2) {
return -(o1.getValue() - o2.getValue());
}
});
for(int i=0;i<10; i++){
Entry<Character, Integer> entry = list.get(i);
char ch = entry.getKey();
int n = entry.getValue();
System.out.println(ch + ":"+n+" "+((double)n/all)*100);
}
System.out.println("不重复字符数量:"+map.size());
System.out.println("共计字符:"+all);
}
public static Map<Character, Integer>
countAll(String file, String encoding)
throws IOException{
InputStreamReader in = new InputStreamReader(
new BufferedInputStream(
new FileInputStream(file)), encoding);
Set<Character> chs = new HashSet<Character>();
chs.add('\n');
chs.add('\r');
chs.add(' ');
chs.add(',');
chs.add(',');
chs.add('。');
chs.add(' ');
Map<Character, Integer> map =
new HashMap<Character, Integer>();
int ch;
while((ch=in.read())!=-1){
char c = (char)ch;
if(chs.contains(c)){//忽略掉 回车等字符
continue;
}
Integer n = map.get(c);
map.put(c, n==null ? 1 : n+1);
}
in.close();
return map;
}
}
Map<Character, Integer> map = countAll(file, "GBK");是什么意思??怎么理解??
Collection<Integer> values = map.values();是什么意思??怎么理解??
List<Entry<Character, Integer>> list =
new ArrayList<Map.Entry<Character,Integer>>(
map.entrySet());是什么意思??怎么理解??就是这句,我什么都不懂!
为什么要定义这个呢: Set<Character> chs = new HashSet<Character>();是什么意思??怎么理解??
作者:
sheng6699
时间:
2014-4-22 20:59
Set<Character> chs = new HashSet<Character>();
这句话的作用就是先用Set<E>模板生成了一个Set类,这个类使用的元素数据类型是Character,然后用这个Set类声明了一个句柄:chs。
之后在用HashSet<E>模板生成了一个Set类的子类HashSet类,因为HashSet<E>模板与Set<E>模板是继承关系,所以生成的对应类也是继承关系。再用HashSet生成一个对象(就是 “new” 一下)然后赋给之前声明的父类(Set)句柄chs。使用的时候只能使用Set类中定义了的方法和属性,如果要使用HashSet类中的方法和属性还需要将类型转换一下,或者当初声明的时候就是用HashSet声明。
Map<Character, Integer> map = countAll(file, "GBK");
这句话就是声明一个以Character,Integer为元素的Map
类对象
的
句柄
“map”,等号后边是使用了一个方法来生成这个
句柄
引用的具体对象,在方法里还可以设置对象的属性。
Collection<Integer> values = map.values();
这句和前一个差不多,不过是使用了map对象的方法来生成所需的对象,具体的可以查一下java的api文档。
List<Entry<Character, Integer>> list =
new ArrayList<Map.Entry<Character,Integer>>(
map.entrySet());
作者:
大漠孤烟
时间:
2014-4-22 23:05
赞一下,学习啦、、、、
作者:
zzkang0206
时间:
2014-4-25 16:18
这多代码 好牛逼的样子!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2