java使用map优化,任何人知道为低内存使用优化的java.util.Map实现?

I've looked in the usual places (apache commons, google) and not been able to find one ...

It should be opensource.

Pretty much looking for one based on a linked list. The use case is 10'000's of maps, with not necessarily many values in. It does not need to scale up, as i can convert it when it gets too big.

Some numbers, sizes using some calculated jvm values (8bytes/java.lang.Object, 4bytes/ref) the HashMap is about 100+32n bytes, the theoretical best is 12+20*n.

解决方案

Ok, implemented it myself in the end. I did a speed comparison, and found when compared to a HashMap that it was still slightly faster with 4 entries, but slower with 5 or more. I did the tests with a longish list of keys that I tried to give a similar makeup as a list of random english words.

import java.util.*;

// PUBLIC DOMAIN

public class SmallMap extends AbstractMap {

private Entry entry = null;

public void clear() { entry = null; }

public boolean isEmpty() { return entry==null; }

public int size() {

int r = 0;

for(Entry e = entry; e!=null; e = e.next) r++;

return r;

}

public boolean containsKey(Object key) {

for(Entry e = entry; e!=null; e = e.next){

if(e.key.equals(key)){

return true;

}

}

return false;

}

public boolean containsValue(Object value) {

for(Entry e = entry; e!=null; e = e.next){

if(e.value==null){

if(value==null) return true;

}else if(e.value.equals(value)){

return true;

}

}

return false;

}

public Object get(Object key) {

for(Entry e = entry; e!=null; e = e.next){

if(e.key.equals(key)){

return e.value;

}

}

return null;

}

public Object put(Object key, Object value) {

for(Entry e = entry; e!=null; e = e.next){

if(e.key.equals(key)){

Object r = e.value;

e.value = value;

return r;

}

}

entry = new Entry(key, value, entry);

return null;

}

public Object remove(Object key) {

if(entry!=null){

if(entry.key.equals(key)){

Object r = entry.value;

entry = entry.next;

return r;

}

for(Entry e = entry; e.next!=null; e = e.next){

if(key.equals(e.next.key)){

Object r = e.next.value;

e.next = e.next.next;

return r;

}

}

}

return null;

}

public Set entrySet() { return new EntrySet(); }

class EntrySet extends AbstractSet{

public Iterator iterator() {

return new Iterator(){

Entry last = null;

Entry e = entry;

public boolean hasNext() { return e!=null; }

public Object next() {

last = e;

e = e.next;

return last;

}

public void remove() {

if(last == null) throw new IllegalStateException();

SmallMap.this.remove(last.key);

}

};

}

public int size() { return SmallMap.this.size();}

}

static private class Entry implements java.util.Map.Entry {

final Object key;

Object value;

Entry next;

Entry(Object key, Object value, Entry next){

if(key==null) throw new NullPointerException();

this.key = key;

this.value = value;

this.next = next;

}

public Object getKey() { return key; }

public Object getValue() { return value; }

public Object setValue(Object value) {

Object r = this.value;

this.value = value;

return r;

}

public int hashCode() {

return (key == null ? 0 : key.hashCode()) ^

(value == null ? 0 : value.hashCode());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值