gpt4 book ai didi

java - 计算文档中字符串的唯一出现次数

转载 作者:行者123 更新时间:2023-12-01 12:10:11 25 4
gpt4 key购买 nike

我正在将日志文件读取到 java 中。对于日志文件中的每一行,我都会检查该行是否包含 IP 地址。如果该行包含 IP 地址,我想将该 IP 地址在日志文件中出现的次数+1。我怎样才能在Java中完成这个任务?

下面的代码成功地从包含 ip 地址的每一行中提取 ip 地址,但计算 ip 地址出现次数的过程不起作用。

void read(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
int counter = 0;
ArrayList<IPHolder> ips = new ArrayList<IPHolder>();
try {
String line;
while ((line = br.readLine()) != null) {
if(!getIP(line).equals("0.0.0.0")){
if(ips.size()==0){
IPHolder newIP = new IPHolder();
newIP.setIp(getIP(line));
newIP.setCount(0);
ips.add(newIP);
}
for(int j=0;j<ips.size();j++){
if(ips.get(j).getIp().equals(getIP(line))){
ips.get(j).setCount(ips.get(j).getCount()+1);
}else{
IPHolder newIP = new IPHolder();
newIP.setIp(getIP(line));
newIP.setCount(0);
ips.add(newIP);
}
}
if(counter % 1000 == 0){System.out.println(counter+", "+ips.size());}
counter+=1;
}
}
} finally {br.close();}
for(int k=0;k<ips.size();k++){
System.out.println("ip, count: "+ips.get(k).getIp()+" , "+ips.get(k).getCount());
}
}

public String getIP(String ipString){//extracts an ip from a string if the string contains an ip
String IPADDRESS_PATTERN =
"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";

Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
Matcher matcher = pattern.matcher(ipString);
if (matcher.find()) {
return matcher.group();
}
else{
return "0.0.0.0";
}
}

持有者类别是:

public class IPHolder {

private String ip;
private int count;

public String getIp(){return ip;}
public void setIp(String i){ip=i;}

public int getCount(){return count;}
public void setCount(int ct){count=ct;}
}

最佳答案

本例中要搜索的关键字是 HashMap。HashMap 是键值对的列表(在本例中是 ip 及其计数对)。

"192.168.1.12" - 12
"192.168.1.13" - 17
"192.168.1.14" - 9

等等。与总是迭代容器对象数组以查明是否已经存在该 IP 的容器相比,使用和访问要容易得多。

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(/*Your file */)));

HashMap<String, Integer> occurrences = new HashMap<String, Integer>();

String line = null;

while( (line = br.readLine()) != null) {

// Iterate over lines and search for ip address patterns
String[] addressesFoundInLine = ...;


for(String ip: addressesFoundInLine ) {

// Did you already have that address in your file earlier? If yes, increase its counter by
if(occurrences.containsKey(ip))
occurrences.put(ip, occurrences.get(ip)+1);

// If not, create a new entry for this address
else
occurrences.put(ip, 1);
}
}


// TreeMaps are automatically orered if their elements implement 'Comparable' which is the case for strings and integers
TreeMap<Integer, ArrayList<String>> turnedAround = new TreeMap<Integer, ArrayList<String>>();

Set<Entry<String, Integer>> es = occurrences.entrySet();

// Switch keys and values of HashMap and create a new TreeMap (in case there are two ips with the same count, add them to a list)
for(Entry<String, Integer> en: es) {

if(turnedAround.containsKey(en.getValue()))
turnedAround.get(en.getValue()).add((String) en.getKey());
else {
ArrayList<String> ips = new ArrayList<String>();
ips.add(en.getKey());
turnedAround.put(en.getValue(), ips);
}

}

// Print out the values (if there are two ips with the same counts they are printed out without an special order, that would require another sorting step)
for(Entry<Integer, ArrayList<String>> entry: turnedAround.entrySet()) {
for(String s: entry.getValue())
System.out.println(s + " - " + entry.getKey());
}

就我而言,输出如下:

192.168.1.19 - 4
192.168.1.18 - 7
192.168.1.27 - 19
192.168.1.13 - 19
192.168.1.12 - 28

我回答了this question大约半小时前,我想这正是您正在寻找的内容,因此如果您需要一些示例代码,请看一下。

关于java - 计算文档中字符串的唯一出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27325368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com