Problem Description:
1. The question involves knowledge points.
- The problem of program stop (as mentioned in this question, that is, I don't know when it will end, it just says that the last eight are printed each time, so it is still handled as usual, and only the last one in the printing container can be printed at the last printing.)
- Net filename: The value is the filename.
2. Solve it yourself.
- Use
LinkedHashMap
to store data. - Store it
净文件名+" "+行号
as a key, and use the corresponding number of errors as a value. - It is judged whether it contains the use
map.containsKey(key)
to judge, and if it contains, the value is +1. - This question should be noted that
HashMap
the output order of is not necessarily the same as the input order. So it is recommended to useLinkedHashMap
.
package com.chaoxiong.niuke.huawei;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class HuaWei_19_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
while (sc.hasNext()) {
String str = sc.next();
int linenum = sc.nextInt();
// 截取
str = str.substring(str.lastIndexOf("\\")+1);
// 算长度
int len = str.length();
if(len>16)
str = str.substring(len-16);
String key = str+" "+linenum;
if(map.containsKey(key)){
map.put(key,map.get(key)+1);
}else
map.put(key,1);
}
// 打印处理, 如果长度大于8 只打印最后8个.
int mapSize = map.keySet().size();
if(mapSize<=8){
for(String string:map.keySet()){
System.out.println(string+" "+map.get(string));
}
}else {
int flag = 1;
for(String string:map.keySet()){
if(flag>mapSize-8){
//打印
System.out.println(string+" "+map.get(string));
}
flag += 1;
}
}
}
}
3. Quality answers.
null
4. Summary of this question.
When processing data containing key-value pairs, the advantages of map are clearly reflected.
If you need to ensure that the input order and output order are the same, you need to use it LinkedHashMap
.