Sageが提案したように、私はTreeMapを使用します。重複はないと仮定します。
public class IPSegment implements Comparable<IPSegement>{
private long start;
private long end;
public IPSegement(String startIp, String endIp){
//convert the ip address to a 32 bit integer.
}
public int compareTo(IPSegement other){
return this.start - other.start; //assume no overlap
}
public int equals(IPSegement other){
// check both start and end
}
public boolean contains(long ip){
//check whether 'ip' is in this range
}
}
public class IPSegmentMap{
private TreeMap<IPSegement> map = new TreeMap<IPSegement>();
public void add(String start, String end, String location){
//...
}
public String find(String ipAddress){
long ip = convertIPtoInt(ipAddress);
IPSegement request = new IPSegement(ip,ip);
IPSegement exist = map.floorKey(request);
if(exist.contains(ip)){
return map.get(exist);
}
}
}