统计年龄分布情况问题
@Slf4j
public class AgeTotal {
public static String file = "/Desktop/age.txt";
public static int total = 10000 * 10000;
public static int maxAge = 180;
public static Random random = new Random();
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
productData();
ageTotal();
log.info("总用时:{}ms", System.currentTimeMillis() - start);
}
/**
* 创建age文件
*
* @throws IOException
*/
public static void productData() throws IOException {
OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(new File(file)), "UTF-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStream);
for (int i = 0; i < total; i++) {
if (i != 0) {
bufferedWriter.newLine();
}
bufferedWriter.write(random.nextInt(maxAge + 1) + "");
}
bufferedWriter.flush();
bufferedWriter.close();
}
/**
* 统计年龄数据分布
*
* @throws Exception
*/
public static void ageTotal() throws Exception {
int[] ageArray = new int[maxAge + 1];
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(file)), "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
ageArray[Integer.valueOf(str)]++;
}
for (int i = 0; i < ageArray.length; i++) {
System.out.println("年龄:" + i + ",人数:" + ageArray[i]);
}
}
}
缘于生活,而归于工作。本人所书,而意于分享。
如有转载,请注明出处!
--活出自己范儿

浙公网安备 33010602011771号