https://examples.javacodegeeks.com/java-basics/data-types/java-dictionary-example/
Java Dictionary example
In this tutorial we will discuss about dictionaries in Java. A is an abstract class that maps keys to values. Every key is associated with a unique value and key are unique. Any non-null object can be used for either a key or a value. An attempt to insert either a null key or a null value to a dictionary will result to a .
However, the original class is now deprecated and instead, all new implementations should implement the interface. The interface provides the functionality of a dictionary, using exactly the same semantics. A can provide three views, which allow the contents of the map to be viewed as a set of keys, collection of values, or set of key-value mappings. Finally, some implementations of the interface maintain an order among its values.
The Map Interface
A map has the form where:
- K: specifies the type of keys maintained in this map.
- V: defines the type of mapped values.
Furthermore, the interface provides a set of methods that must be implemented. In this section, we will present some of the most fundamental methods of a map:
- containsKey: Returns true if the map contains the requested key.
- containsValue: Returns true if the map contains the requested value.
- get: Retrieve the value of the requested key.
- keySet: Returns a Set that contains all keys of the map.
- put: Adds the requested key-value pair in the map.
The interface is implemented by different Java classes, such as , and . These classes are able to provide the full functionality of a dictionary. However, these classes differ in some key aspects, as presented below:
Null Keys
|
Null Values
|
Order
|
Synchronized
| |
| HashMap |
Permitted
|
Permitted
|
No
|
No
|
| HashTable |
Prohibited
|
Prohibited
|
No
|
Yes
|
| LinkedHashMap |
Permitted
|
Permitted
|
Yes
|
No
|
HashTable – HashMap
The class implements a hash table and maps keys to values. A is a hash table based implementation of the interface. They both contain two fundamental parameters: initial capacity and performance. The capacity is defined as the number of buckets in the hash table, while the load factor is a measure that indicates the maximum value the hash table can reach, before being automatically increased.
An example that uses a as a dictionary is shown below. The program can also be executed properly if we change the type of our map to a :
CountWords_v1.java:
In this example we used a to store the words of a file and how many times each word appears in that file.
A sample execution is shown below:
LinkedHashMap
The class provides an implementation of a map that has a predictable iteration order.
The same example that counts the references of a word in a file and stores the key-value pairs in a is shown below:
CountWords_v2.java:
A sample execution is shown below:
Notice that the usage of a enables us to print the stored keys in the way in which the words were read and stored in the map.
댓글 없음:
댓글 쓰기