`
blueyanghualong
  • 浏览: 227419 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

HashOjbect之HashObjectMap(4)

 
阅读更多
  public BaseHashObject get(BaseHashObject object) {
        if (object == null)
            return null;
        int hash = object.keyHashCode();
        for (BaseHashObject e = table[indexFor(hash, table.length)];
             e != null;
             e = e.getNext()) {
            if (e.keyHashCode() == hash && e.keyEquals(object)) {
                return e;
            }
        }
        return null;
    }
   
    public boolean containsKey(BaseHashObject object){
    return get(object)==null ? false : true;
    }
    public HashObject remove(BaseHashObject Object) {
    BaseHashObject e = removeObjectForKey(Object);
        return e;
    }

    final BaseHashObject removeObjectForKey(BaseHashObject object) {
        int hash = (object == null) ? 0 : object.keyHashCode();
        int i = indexFor(hash, table.length);
        BaseHashObject prev = table[i];
        BaseHashObject e = prev;

        while (e != null) {
        BaseHashObject next = e.getNext();
            if (e.keyHashCode() == hash && e.keyEquals(object)) {
                modCount++;
                size--;
                if (prev == e)
                    table[i] = next;
                else
                    prev.setNext(next);
                e.recordRemoval(this);
                if(logger.isDebugEnabled())
                {
                logger.debug("size--:"+size);
                }
                return e;
            }
            prev = e;
            e = next;
        }

        return e;
    }
    public void clear() {
        modCount++;
        BaseHashObject[] tab = table;
        for (int i = 0; i < tab.length; i++)
            tab[i] = null;
        size = 0;
    }
   
    public boolean containsObject(BaseHashObject object) {
        return getObject(object) != null;
    }
   
    final T getObject(BaseHashObject object) {
    int hash = (object == null) ? 0 : object.keyHashCode();
        for (BaseHashObject e = table[indexFor(hash, table.length)];
             e != null;
             e = e.getNext()) {
            if (e.keyHashCode() == hash && e.keyEquals(object)){
            return (T)e;
            }
        }
        return null;
    }
   
    Iterator<T> newObjectIterator(){
        return new ObjectIterator();
    }
   
    private final class ObjectIterator extends HashIterator {
    public T next() {
            return nextObject();
        }
    }
   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics