java根据一个数字 怎么能快速的查询到 他在哪个A B 之间?

java给定数字a b,求出a到b之间有多少个数思路~

a 与b 之间 有 a-b-1 个数, 不包含 a和b;

ajax请求后,将后台查询出的数据转换成json格式,接受后就可以在页面中可以用json.来获取数据

首先要确定你的地域信息是怎么判定的,需要IP的哪些位?
IP分为4段,每段3位。从头到尾,不足不零。
可以形成。最大不超过12位的IP整数。要追求速度,首先需要将12位完整的IP中的某一部分脱离开。现在只需要除开IP段中的某一个段。即能取到9位的整数。这时,java中int类型支持的数字大小是20亿,即10位数,那仅取三段的IP满足int【Integer】的条件。当然,如果像LZ说的,取startIP和endIP,是否就是只二段?这样也可。我说的三段,是需要舍去一段。
现在将刚才我们得到的int型做为Map的key。
为什么要用Integer?下面分析。
首先查询最快的,肯定是HashMap。这里不得不说下HashMap的原理。
1、HashMap里添加一个元素。hashMap.put(key,value);是取Key值的hashCode,经过HashMap的hash(int)的运算,直接得到在HashMap中键数组中应该位于的下标。再将Key和Value的Entry【含Key,Value】放到HashMap里。注意。。是没有明显的遍历操作的。
2、从HashMap中取值,是怎么做的呢?同样,hashMap.get(key)是直接由key值的hashCode得key在键数组中的下标,再取出对应Entry【含Key,Value】。。同样。。没有明显的遍历操作的。
上面2步可以统称为:HashMap的hash算法。具体的实现。你可以去看jdk的源码。
现在就可以回到最开始的,为什么要用Integer类型做key,因为。。Integer重写了hashCode方法。他是直接返回Integer的value字段的,而Integer的eqauls方法甚至直接用的==操作符,这两点决定了高效性,。而String的eqauls和hashCode也重写了,但运算量远大于Integer的。对于HashMap来说,hashCode()和equals()方法,是取值,添加值都会用的。以下会把相关JDK代码贴出来。------如果实在不能用Integer,建议用Long。long的equals用的也是==,hashCode只是对value值进行了无符号右移32位再与原value值取“异或运算”。return (int)(value ^ (value >>> 32));

为什么不用TreeMap呢。我分析了TreeMap的实现。他是这样做的。
1、TreeMap里添加元素。put(key,value),是首先,TreeMap,需要一个对Key的比较器,因为TreeMap是有序的,他的添加是由Key,先找到Key在键数组的位置,再将key,value的Entry放到对应位置。同时设置Entry的前一个和后一个Entry。形成有序Map。在查找Key的位置时,用的是树查找【二叉查找】,从根节点,依次查找。
2、TreeMap里取元素:同样的。用二叉查询方法,找到Key对应的Entry。从而得到Key,Value值。

我做了实验。分别在
1、HashMap里添加1000000条Integer键,String值的随机元素。用时,2500左右毫秒,然后再循环查询10000条随机数据,用时70毫秒左右。
2、TreeMap里做相同的操作,耗时分别为:2800毫秒和95毫秒。
可以认证上述观点。
综上所述。你应该用HashMap做为容器,用Integer做为键。能达到最快查询速度。
下面贴出相关代码。是在JDK1.6的源码里贴出来的。有兴趣的话,可以看一下。
HashMap:
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}

modCount++;
addEntry(hash, key, value, i);
return null;
}
static int indexFor(int h, int length) {
return h & (length-1);
}
public V get(Object key) {
if (key == null)
return getForNullKey();
int hash = hash(key.hashCode());
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
}
return null;
}

TreeMap:
public V put(K key, V value) {
Entry<K,V> t = root;
if (t == null) {
// TBD:
// 5045147: (coll) Adding null to an empty TreeSet should
// throw NullPointerException
//
// compare(key, key); // type check
root = new Entry<K,V>(key, value, null);
size = 1;
modCount++;
return null;
}
int cmp;
Entry<K,V> parent;
// split comparator and comparable paths
Comparator<? super K> cpr = comparator;
if (cpr != null) {
do {
parent = t;
cmp = cpr.compare(key, t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
else {
if (key == null)
throw new NullPointerException();
Comparable<? super K> k = (Comparable<? super K>) key;
do {
parent = t;
cmp = k.compareTo(t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
Entry<K,V> e = new Entry<K,V>(key, value, parent);
if (cmp < 0)
parent.left = e;
else
parent.right = e;
fixAfterInsertion(e);
size++;
modCount++;
return null;
}

public V get(Object key) {
Entry<K,V> p = getEntry(key);
return (p==null ? null : p.value);
}

final Entry<K,V> getEntry(Object key) {
// Offload comparator-based version for sake of performance
if (comparator != null)
return getEntryUsingComparator(key);
if (key == null)
throw new NullPointerException();
Comparable<? super K> k = (Comparable<? super K>) key;
Entry<K,V> p = root;
while (p != null) {
int cmp = k.compareTo(p.key);
if (cmp < 0)
p = p.left;
else if (cmp > 0)
p = p.right;
else
return p;
}
return null;
}

Integer 的hashCode 和 equals方法:
public int hashCode() {
return value;
}
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
String:的hashCode 和 equals方法:
public int hashCode() {
int h = hash;
int len = count;
if (h == 0 && len > 0) {
int off = offset;
char val[] = value;

for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}

楼主的意思是把这个语句翻译成java代码:
select a.ip信息,b.地域信息 from ip表 a,地域信息表 b
where a.ip = 1234567890
and a.ip between b.startip_col and b. endip_col;

那么java代码中明显需要用到map,不管是哪个map,key都是拿来做比较、做确认的,所以用Long类型的ip段来做key是最佳的选择。

由于楼主用来比对的内容本身不等于各begin(或end),所以我个人认为都可以,但treemap更好。

treemap比较强大,界面要显示这个参数列表都不用去排序了。
比如这样的(key是startip):1,10,20,30,40,50,60……,一趟不走完就能确定33是在30到40之间。【省去比endip的过程】

而用hashmap的话,就直接用对象(startip+endip+地域)做key,value就随便写个啥;反正next之后有可能就会碰到startip<ip<endip的情况。

数组里面也是放的对象,下标++之后可能就会找到地域信息,方法同hashmap。

你把startip和endip组成一个HashMap,这个hashMap要以startip升序排序,你拿到keyset循环,定义一个临时ip,比较你想判断的IP与当前startip的大小,如果前者比后者大,临时IP记录好当前startip接着循环下一次,直到后者比前者大,之前记录的临时IP就是你想判断的IP的startip,当然endip就通过map拿到了

想你写这样的程序是不需要数据库的 要IO流就OK

相关兴趣推荐

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网