What to write this time:
--Hashtable class --Enumeration interface
Property files for entering keys and values in Hashtable etc.
temp.properties
key1 = value1
key2 = value2
key3 = value3
key4 = value4
test.properties
key11 = value11
key12 = value12
key13 = value13
enumeration_sample.properties
key1 = Java
key2 = JavaScript
key3 = Scala
key4 = Python2
key5 = Python3
use.
A class that implements a hash table. This class is from Java 1.0 and is still an active (?) Class, like the ancestors of ConcurrentHashMap. Hash tables are called associative arrays and mix well with hash maps. See the links below for the differences between hashtables and hashmaps. -[Java] Difference between HashMap and Hashtable
The point is that HashMap is HashMap and Hashtable is Hashtable.
In the following example, the property is set by default in advance, copied to Hashtable, the property you want to set is set, and the property is newly set with Hashtable, which is a copy of the default property. Program to import to.
HashtableSample.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;
public class HashtableSample {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws FileNotFoundException, IOException {
Hashtable<String, String> hashTable = new Hashtable<String, String>();
Properties properties = new Properties();
properties.load(new FileInputStream("./Property/temp.properties"));
hashTable = (Hashtable<String, String>) properties.clone();
System.out.println("Default Property: " + properties);
properties.load(new FileInputStream("./Property/test.properties"));
properties.putAll(hashTable);
System.out.println("Second Property: " + properties);
if(properties.containsKey("key4")){
System.out.println("key4 is exist.");
} else {
System.out.println("key4 isn't exist.");
}
}
}
Result is
Default Property: {key4=value4, key3=value3, key2=value2, key1=value1}
key13 isn't exist.
Second Property: {key13=value13, key12=value12, key11=value11, key4=value4, key3=value3, key2=value2, key1=value1}
key13 is exist.
Will be. Here, key13 indicates a key that is not the default.
A method defined in the Map interface that determines whether the key specified by the argument is in the Hashtable. Returns true if the Hashtable has that key.
A method defined in the Map interface that copies all the argument maps to the Hashtable.
Since the Properties class inherits from the Hashtable class, you can use Hashtable methods.
This is also from Java 1.0 and is like an ancestor of Iterator. The operation is the same as Iterator. Then why not use Iterator? I think, but that's not the case. Depending on the class, there are methods that return Enumeration type (java.util.Hashtable # elements (), java.util.Hashtable # keys (), java.util.Properties # propertyNames (), etc.), so you need to use them properly.
The following example is a program that puts a list of property keys into an Enumeration type, searches for the value of that key, and outputs it to the console.
EnumerationSample.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class EnumerationSample {
public static void main(String[] args) throws FileNotFoundException, IOException{
Properties properties = new Properties();
properties.load(new FileInputStream("./Property/enumration_sample.properties"));
for(Enumeration<Object> enumeration = properties.keys(); enumeration.hasMoreElements();){
String name = (String) enumeration.nextElement();
System.out.println(properties.getProperty(name));
}
}
}
Result is
Python3
Python2
Scala
JavaScript
Java
Will be.
Methods defined in the Enumeration interface. This method determines if the enumeration has the following elements: Returns true if the following method exists.
Methods defined in the Enumeration interface. A method that returns the next element if the enumeration has one or more elements. Returns NoSuchElementException if there are no more elements.