在Java中创建Hashtable作为final

时间:2023-02-24 17:34:06

As we know the purpose of "final" keyword in java. While declaring a variable as final, we have to initialize the variable. like "final int a=10;" We can not change the value of "a". But if we go for HashTable its possible to add some value even declaring the HashTable as final.

我们知道java中“final”关键字的用途。在将变量声明为final时,我们必须初始化变量。比如“final int a = 10;”我们无法改变“a”的价值。但是如果我们选择HashTable,它甚至可以添加一些值,甚至可以将HashTable声明为final。

Example::

 private static final Hashtable<String,Integer> MYHASH = new Hashtable<String,Integer>() 
{{     put("foo",      1);     
       put("bar",      256);     
       put("data",     3);     
       put("moredata", 27);     
       put("hello",    32);     
       put("world",    65536);  }}; 

Now I am declaring the MYHASH HashTable as final. If I try to add some more elements to this, its accepting.

现在我宣布MYHASH HashTable为最终版。如果我尝试添加更多元素,它接受。

MYHASH.put("NEW DATA",      256);

Now the "NEW DATA" is added to the HashTable. My questions is Why its allowing to add even its declaring as final????

现在,“NEW DATA”被添加到HashTable中。我的问题是为什么它允许添加甚至它的声明作为最终????

6 个解决方案

#1


67  

Because final marks the reference, not the object. You can't make that reference point to a different hash table. But you can do anything to that object, including adding and removing things.

因为final标记了引用,而不是对象。您不能将该引用指向另一个哈希表。但是你可以对该对象做任何事情,包括添加和删除东西。

Your example of an int is a primitive type, not a reference. Final means you cannot change the value of the variable. So, with an int you cannot change the value of the variable, e.g. make the value of the int different. With an object reference, you cannot change the value of the reference, i.e. which object it points to.

您的int示例是基本类型,而不是引用。最终意味着您无法更改变量的值。因此,使用int,您无法更改变量的值,例如使int的值不同。使用对象引用,您无法更改引用的值,即它指向的对象。

#2


12  

Only the reference is final, its methods can of course be called just as for a non-final reference.

只有参考是最终的,它的方法当然可以被称为非最终参考。

Use Collections.unmodifiableMap(map) to make a map unmodifiable.

使用Collections.unmodifiableMap(map)使地图不可修改。

#3


10  

You can use Collections.unmodifiableMap to get an unmodifiable wrapper over your hash table.

您可以使用Collections.unmodifiableMap在哈希表上获取不可修改的包装器。

Example:

import java.util.*;
class Test{

    public static final Map<String,Integer> MYHASH;
    static{
        Hashtable<String,Integer> tmp = 
            new Hashtable<String,Integer>();
        tmp.put("A",65);
        tmp.put("C",67);
        MYHASH = Collections.unmodifiableMap(tmp);
    }

    public static void main(String[] args){

        System.out.println(MYHASH.get("A"));

        //this will throw
        //java.lang.UnsupportedOperationException
        MYHASH.put("B",66);    

    }

}

#4


6  

Try and wrap your Hashtable with an unmodifiable map using Collections.unmodifiableMap( MYHASH ). This should throw exceptions when you try to change something in the map, i.e. add or remove entries. (Note that MYHASH should then be of type Map<String, String> and not Hashtable<String, String>.)

使用Collections.unmodifiableMap(MYHASH)尝试使用不可修改的地图包装Hashtable。当您尝试更改地图中的某些内容(即添加或删除条目)时,这应该抛出异常。 (注意,MYHASH应该是Map 类型而不是Hashtable 。) ,string> ,string>

Regarding the final keyword: as the others already said, it means that you can't assign another Hashtable to MYHASH but the map itself is still mutable. To change this you have to wrap it with some immutable wrapper, like the UnmodifiableMap mentioned above.

关于final关键字:正如其他人已经说过的那样,这意味着你不能将另一个Hashtable分配给MYHASH,但地图本身仍然是可变的。要改变它,你必须用一些不可变的包装器包装它,就像上面提到的UnmodifiableMap一样。

#5


4  

As Joe said, this is because final marks the reference not the object.

正如Joe所说,这是因为final标记的引用不是对象。

However, you can do what you want with Collections.unmodifiableMap (see here)

但是,你可以用Collections.unmodifiableMap做你想要的(见这里)

#6


0  

For fields and variables, using final means they can be assigned only once. Either immediately, or at some later time (e.g. in a constructor for fields). This doesn't mean the actual instance becomes immutable. A Hashtable is a data structure that you can add entries to, regardless of how it was assigned to some variable. Only the reference is final.

对于字段和变量,使用final意味着它们只能分配一次。立即或稍后(例如在字段的构造函数中)。这并不意味着实际的实例变得不可变。 Hashtable是一种可以添加条目的数据结构,无论它如何分配给某个变量。只有参考是最终的。

#1


67  

Because final marks the reference, not the object. You can't make that reference point to a different hash table. But you can do anything to that object, including adding and removing things.

因为final标记了引用,而不是对象。您不能将该引用指向另一个哈希表。但是你可以对该对象做任何事情,包括添加和删除东西。

Your example of an int is a primitive type, not a reference. Final means you cannot change the value of the variable. So, with an int you cannot change the value of the variable, e.g. make the value of the int different. With an object reference, you cannot change the value of the reference, i.e. which object it points to.

您的int示例是基本类型,而不是引用。最终意味着您无法更改变量的值。因此,使用int,您无法更改变量的值,例如使int的值不同。使用对象引用,您无法更改引用的值,即它指向的对象。

#2


12  

Only the reference is final, its methods can of course be called just as for a non-final reference.

只有参考是最终的,它的方法当然可以被称为非最终参考。

Use Collections.unmodifiableMap(map) to make a map unmodifiable.

使用Collections.unmodifiableMap(map)使地图不可修改。

#3


10  

You can use Collections.unmodifiableMap to get an unmodifiable wrapper over your hash table.

您可以使用Collections.unmodifiableMap在哈希表上获取不可修改的包装器。

Example:

import java.util.*;
class Test{

    public static final Map<String,Integer> MYHASH;
    static{
        Hashtable<String,Integer> tmp = 
            new Hashtable<String,Integer>();
        tmp.put("A",65);
        tmp.put("C",67);
        MYHASH = Collections.unmodifiableMap(tmp);
    }

    public static void main(String[] args){

        System.out.println(MYHASH.get("A"));

        //this will throw
        //java.lang.UnsupportedOperationException
        MYHASH.put("B",66);    

    }

}

#4


6  

Try and wrap your Hashtable with an unmodifiable map using Collections.unmodifiableMap( MYHASH ). This should throw exceptions when you try to change something in the map, i.e. add or remove entries. (Note that MYHASH should then be of type Map<String, String> and not Hashtable<String, String>.)

使用Collections.unmodifiableMap(MYHASH)尝试使用不可修改的地图包装Hashtable。当您尝试更改地图中的某些内容(即添加或删除条目)时,这应该抛出异常。 (注意,MYHASH应该是Map 类型而不是Hashtable 。) ,string> ,string>

Regarding the final keyword: as the others already said, it means that you can't assign another Hashtable to MYHASH but the map itself is still mutable. To change this you have to wrap it with some immutable wrapper, like the UnmodifiableMap mentioned above.

关于final关键字:正如其他人已经说过的那样,这意味着你不能将另一个Hashtable分配给MYHASH,但地图本身仍然是可变的。要改变它,你必须用一些不可变的包装器包装它,就像上面提到的UnmodifiableMap一样。

#5


4  

As Joe said, this is because final marks the reference not the object.

正如Joe所说,这是因为final标记的引用不是对象。

However, you can do what you want with Collections.unmodifiableMap (see here)

但是,你可以用Collections.unmodifiableMap做你想要的(见这里)

#6


0  

For fields and variables, using final means they can be assigned only once. Either immediately, or at some later time (e.g. in a constructor for fields). This doesn't mean the actual instance becomes immutable. A Hashtable is a data structure that you can add entries to, regardless of how it was assigned to some variable. Only the reference is final.

对于字段和变量,使用final意味着它们只能分配一次。立即或稍后(例如在字段的构造函数中)。这并不意味着实际的实例变得不可变。 Hashtable是一种可以添加条目的数据结构,无论它如何分配给某个变量。只有参考是最终的。