TreeMap floorEntry 函数不适用于整数值(Treemap 定义为 long)

TreeMap floorEntry function not working with integer values(Treemap defined as long)

我正在尝试执行基于间隔的搜索,我正在从文件中加载并试图找到我的 ipaddress 所在的间隔。下面是我的代码。此代码仅适用于 long 但不适用于整数版本不是 long 数字的 ip 地址。

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());
        }
    }
}
  1. 下面是输出 运行:1432262970924 加载 isp 地图。加载中 超过。 1432262975089 开始查找。 540772 未找到 1432262975089\n 构建成功(总时间:4 秒)

IP 地址是一个 32 位无符号整数。在 Java 中,int 是 32 位 有符号 整数。

如果您使用 signed int 表示 IP 地址,则必须在您的代码中考虑以下事实:所有 IP 地址的 上半部分 实际上是否定。

Java 7 不为未签名的 int 提供内置支持,因此您必须实现所需的行为或找到另一个 class 包装器(来自某处) 满足您需要的整数。

Java 8 在 Integer class 中引入了用于将 int 与无符号数进行比较的方法。有关适当的方法,请参阅 https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html