我試圖執行基於時間間隔的搜索,我從文件加載並試圖找到我的ip地址所在的間隔。以下是我的代碼。此代碼只適用於很長時間,但不適用於整數版本不是很長的IP地址。TreeMap floorEntry函數不能使用整數值(Treemap定義爲long)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class RangeBasedSearchAsn {
public static class AsnInfo {
private long asn;
private String ipSubnet;
private String isp;
@Override
public String toString() {
return "Here are the details:\n"
+ this.asn + " " + this.ipSubnet + " " + this.isp ;
}
public AsnInfo(long asn, String ipSubnet, String isp) {
this.asn = asn;
this.ipSubnet = ipSubnet;
this.isp = isp;
}
public long getAsn() {
return asn;
}
public void setAsn(long asn) {
this.asn = asn;
}
public String getIpSubnet() {
return ipSubnet;
}
public void setIpSubnet(String ipSubnet) {
this.ipSubnet = ipSubnet;
}
public String getIsp() {
return isp;
}
public void setIsp(String isp) {
this.isp = isp;
}
}
public static class Range {
private long upper;
private AsnInfo asnInfo;
public Range(long upper, AsnInfo value) {
this.upper = upper;
this.asnInfo = value;
}
public long getUpper() {
return upper;
}
public void setUpper(long upper) {
this.upper = upper;
}
public AsnInfo getValue() {
return asnInfo;
}
public void setValue(AsnInfo value) {
this.asnInfo = value;
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
long key = 848163455L;
NavigableMap<Long, Range> asnTreeMap = new TreeMap<>();
System.out.println(System.currentTimeMillis());
System.out.println("Loading isp Map.");
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream("C:\\Talend\\TalendTestArea\\rbl_ipv4_zone.txt");
sc = new Scanner(inputStream, "UTF-8");
while (sc.hasNextLine()) {
String line = sc.nextLine();
StringTokenizer st = new StringTokenizer(line, ";");
while (st.hasMoreTokens() && st.countTokens() == 7) {
st.nextToken();
st.nextToken();
long token1 = Long.parseLong(st.nextToken());
System.out.println("here is token1:" + token1);
long token2 = Long.parseLong(st.nextToken());
System.out.println("here is token1:" + token2);
long token3 = Long.parseLong(st.nextToken());
System.out.println("here is token1:" + token3);
asnTreeMap.put(token1, new Range(token2, new AsnInfo(token3,st.nextToken(),st.nextToken())));
}
}
if (sc.ioException() != null) {
throw sc.ioException();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}
System.out.println("Loading Over.");
System.out.println(System.currentTimeMillis());
System.out.println("Starting Lookup.");
long[] ips = {30503936L};
for(int i = 0 ; i < ips.length;i++){
System.out.println(asnTreeMap.size());
Map.Entry<Long, Range> entry = asnTreeMap.floorEntry(ips[i]);
if (entry == null) {
System.out.println("Value not valid");
} else if (key <= entry.getValue().upper) {
System.out.println("Carrier = " + entry.getValue().asnInfo.toString() + "\n");
} else {
System.out.println("Not found");
}
System.out.println(System.currentTimeMillis());
}
}
}
- 下面是輸出運行:1432262970924加載ISP地圖。正在加載 。 1432262975089開始查找。 540772未找到 1432262975089 \ n BUILD SUCCESSFUL(總時間:4秒)
請清理你的代碼 - 發佈*最小*,完整和可驗證的例子。用一半*註釋出來的例子*不是*最小*,我不打算閱讀或試圖理解它。 –