簡體   English   中英

擴展AbstractStringBuilder並在Hashtable中使用子類時出現的問題

[英]Issues when extending AbstractStringBuilder and using the subclass in a Hashtable

我想擴展AbstractStringBuilder並獲得與StringBuilder相同的類,但具有與String.hashCode()相同的hashCode()方法。 目的是使用這個新的子類作為Hashtable中的鍵。 我想試驗Hashtable和這個新的子類,看看會發生什么,因為最后我想解決一些內存問題,StringBuilder解決了其中的一些但不是全部。 所以,這是子類,

import java.lang.*;

public final class StringCon extends AbstractStringBuilder implements java.io.Serializable, CharSequence
{
    public StringCon()
    {
        super( 16);
    }

    public StringCon( int capacity)
    {
        super( capacity);
    }

    public StringCon( String str)
    {
        super( str.length() + 16);
        append( str);
    }

    public StringCon append( Object obj)
    {
        return append( String.valueOf( obj));
    }

    public StringCon append( String str)
    {
        super.append( str);
        return this;
    }

    public StringCon delete( int start, int end)
    {
        super.delete( start, end);
        return this;
    }

    public StringCon delete( int start)
    {
        super.delete( start, this.length());
        return this;
    }

    public int indexOf( String str)
    {
        return indexOf( str, 0);
    }

    public int indexOf( String str, int fromIndex)
    {
        return String.indexOf( value, 0, count, str.toCharArray(), 0, str.length(), fromIndex);
    }

    public int hashCode()
    {
        int hash = 0;
        int h = hash;
        if( h == 0 && count > 0)
        {
            int off = 0;
            char val[] = value;
            int len = count;
            for( int i = 0; i < len; i++)
                h = 31*h + val[ off++];

            hash = h;
        }
        return h;
    }
}

我只實現了我將要使用的方法。 問題是,

1)編譯器找不到符號值,計數甚至是關鍵字super。 這些符號在AbstarctStringBuilder中定義,StringBuilder自由使用它們。 為什么?

2)編譯器找不到方法AbstractStringBuilder.substring()。 為什么?

3)我收到錯誤,

error: type argument StringCon is not within bounds of type-variable E

在一份聲明中

hth = ( j + 1 < al.size()) ? new Hashtable< StringCon, LinkedList< StringCon>>( al.get( j + 1).size(), 0.75F) : null; 

4)我收到了錯誤

error: method containsKey in class Hashtable<K,V> cannot be applied to given types;
                    if( hth.isEmpty() || !hth.containsKey( nextKey))
                                             ^
required: Object
found: StringCon
reason: actual argument StringCon cannot be converted to Object by method invocation conversion
where K,V are type-variables:
 K extends Object declared in class Hashtable
 V extends Object declared in class Hashtable

其中nextKey是一個StringCon對象。

上面的各種方法我已經從StringBuilder和String類中復制了它們。

什么是我不明白,我的錯誤在哪里? 如果這有任何重要性,我在Hadoop的上下文中使用上述所有內容。

java.lang.AbstractStringBuilder不是public ,因此它只能由java.lang的其他類擴展。 嘗試編譯代碼會產生第一個錯誤:

StringCon.java:3: java.lang.AbstractStringBuilder is not public in java.lang;
cannot be accessed from outside package

通常不建議使用可變類作為哈希表中的鍵( 可變哈希映射鍵是一種危險的做法嗎? )。 可能有更好的方法來解決您的問題:

因為最后我想解決一些內存問題,StringBuilder解決了其中一些而不是全部問題

考慮直接詢問有關內存問題的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM