import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 一个文本文件中约有 10万多行的数据,每个数据占一行(一行一个整数)。
* 要求:统计出总行数,并找出出现次数最多的那个整数。
*/
public class Print1 {
public static void main(String[] args) {
Map<Integer,Integer> map = new HashMap<>();
int count = 0;
try {
FileReader fr = new FileReader("D://FTP//test2.txt");
BufferedReader bf = new BufferedReader(fr);
String arr;
// 按行读取字符串
while ((arr = bf.readLine()) != null) {
//记录行数
count++;
int k = Integer.valueOf(arr);
if(!map.containsKey(k)) {
map.put(k, 1);
}else {
map.put(k, map.get(k)+1);
}
}
//初始化
int maxkey = 0;
int maxvalue = 0;
for(Integer key : map.keySet()) {
if( map.get(key)!=null){
maxkey = key;
maxvalue = map.get(key);
break;
}
}
//遍历Map,寻找次数最多的
for(Integer key : map.keySet()) {
if(maxvalue < map.get(key)){
maxkey = key;
maxvalue = map.get(key);
}
}
System.out.println("总行数"+count);
System.out.println("出现次数最多的整数:"+maxkey+",出现:"+maxvalue);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
统计出文本的总行数,并找出出现次数最多的那个整数
猜你喜欢
转载自blog.csdn.net/weixin_42131843/article/details/105053886
今日推荐
周排行