Turn: Hashtable, Hashmap and Properties

xiaoxiao2021-03-06  60

There are three important differences between HashTable and HashmaphashTable and HashMap. The first difference is mainly historical reasons. Hashtable is an implementation of the Old Dictionary class, HashMap is an implementation of the MAP interface introduced by Java 1.2.

Perhaps the most important difference is that the HashTable method is synchronized, while the HashMap method is not. This means that although you can use any special behavior to use a HashTable in a multi-threaded application, but you have to provide an external synchronization for a HashMap. A convenient method is to use the Static SynchronizedMap () method of the Collections class, which creates a thread secure MAP object and returns it as a package object. The method of this object allows you to synchronize the potential HashMap. The result of this is that when you don't need to synchronize, you can't cut off the synchronization in HashTable (such as in a single-threaded application), and synchronize has added a lot of processing costs.

The third point is different, only HashMap allows you to take null values ​​as the key or value of the entry of a table. There is only one record in HashMap, which can be an empty key, but any number of entries can be empty Value. That is to say, if there is no search button in the table, or if the search button is found, it is an empty value, then get () will return NULL. If necessary, use the ContaInKey () method to distinguish these two situations.

Some information suggestions when you need to synchronize, use HashTable, which is used to use HashMap. However, because when needed, HashMap can be synchronized, the HashMap's function is more than the HashTable function, and it is not based on an old class, so some people think that in all cases, Hashmap is preferred in HashTable.

About Properties Sometimes, you may want to use a HashTable to map the key string to the Value string. There are some examples in DOS, Windows, and UNIX, such as the string Path of Key is mapped to the Value string C: / Windows; C: / Windows / System. Hashtables are a simple way to say these, but Java provides another method.

The Java.util.Properties class is a subclass of Hashtable, designed for String Keys and Values. The usage of the Properties object is similar to the use of HashTable, but the class adds two ways to save time, you should know.

The Store () method saves the content of a Properties object to a file in a readable form. The LOAD () method is just the opposite to read the file and set the Properties object to include Keys and Values.

Note that because Properties expands HashTable, you can add keys and values ​​that are not String objects with a superclass's PUT () method. This is not available. Also, if you use the Store () for a Properties object that does not contain the String object, Store () will fail. As an alternative to PUT () and GET (), you should use setProperty () and getProperty (), they use String parameters. Additional:

Public HashTable (int Initialcapacity, Float LoadFactor)

Public Hashmap (Int InitiaLcapacity, Float LoadFactor)

转载请注明原文地址:https://www.9cbs.com/read-83665.html

New Post(0)