import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class Print {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入密码:");
String s=sc.nextLine();
sc.close();
TreeMap<Character, Integer> tm=new TreeMap<Character,Integer>();
//HashMap<Character, Integer> tm=new HashMap<Character,Integer>();----------1
//TreeSet<Character, Integer> tm=new TreeSet<Character,Integer>();----------2
//TreeSet<Character, Integer> tm=new TreeSet<Character,Integer>();----------3
String str="nihao";
if (str.equals(s)) {
FileReader fr=new FileReader("d:/a.txt");
int ch=0;
while( (ch=fr.read())!=-1) {
Integer count=tm.get((char)ch);
if (count==null) {
tm.put((char)ch, 1);
}else {
count++;
tm.put((char)ch, count);
}
}
fr.close();
Set<Character> set =tm.keySet();
for (Character character : set) {
Integer in=tm.get(character);
System.out.print(character+"("+in+")");
}
}else {
System.out.println("密码输入错误!");
}
}
}
里面注释为什么用TreeMap,如果用HashMap呢,只能双列集合来实现吗,如果单列集合Set呢。对集合的概念清晰,清楚,而且知道每个的区别和用途。 |