for (var i = 0, keys = Object.keys(a_hashmap), ii = keys.length; i < ii; i++)
{
console.log('key : ' + keys[i] + ' val : ' + a_hashmap[keys[i]]);
}
This Blog Helps to Learn Java Technology And Its Supportive Frameworks With Working Examples.
for (var i = 0, keys = Object.keys(a_hashmap), ii = keys.length; i < ii; i++)
{
console.log('key : ' + keys[i] + ' val : ' + a_hashmap[keys[i]]);
}
| SN | Methods with Description |
|---|---|
| 1 | boolean hasMoreElements( ) When implemented, it must return true while there are still more elements to extract, and false when all the elements have been enumerated. |
| 2 | Object nextElement( ) This returns the next object in the enumeration as a generic Object reference. |
import java.util.Vector;
import java.util.Enumeration;
public class EnumerationTester {
public static void main(String args[]) {
Enumeration days;
Vector dayNames = new Vector();
dayNames.add("Sunday");
dayNames.add("Monday");
dayNames.add("Tuesday");
dayNames.add("Wednesday");
dayNames.add("Thursday");
dayNames.add("Friday");
dayNames.add("Saturday");
days = dayNames.elements();
while (days.hasMoreElements()){
System.out.println(days.nextElement());
}
}
}
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
public Object clone() throws CloneNotSupportedException {
if (this implements Cloneable)
return nativeCloneImpl();
else
throw new CloneNotSupportedException();
}
public interface Allopathic{}
public interface Homeopathic{}
public interface Ayurvedic{}
for (Medicine medicine : allMedicines) {
if (medicine instanceof Allopathic) {
//... update stats accordingly
}
else if (medicine instanceof Homeopathic) {
//... update stats accordingly
}
else if (medicine instanceof Ayurvedic) {
//... update stats accordingly
}
else {
//... handle stats for general items
}
}
Vector()
Vector(Collection<? extends E> c)
Vector(int initialCapacity)
Vector(int initialCapacity, int capacityIncrement)
Vector<Object> test = new Vector<Object>(100);
for (int i = 0; i < 100; i++) {
test.add(new Object());
}
test.add(new Object());
System.out.println(test.size());
System.out.println(test.capacity());
Vector at construction time.Vector is a dynamically growable data structure, and it would reallocate its backing array as necessary. Thus, there is no final capacity, but you can set what its initial value is.capacity and acapacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.Vector. It's worth noting that ArrayList, a similar growable data structure backed by an array, doesn't even specify the details of its growth policy, but the OpenJDK version has a growth factor of 3/2.Vector actually allows you to set a non-geometric growth factor withcapacityIncrement. It's important to realize that if you set capacityIncrement to a small non-zero value, you can in fact make Vector perform horrible asymptotically. If you set it to 1, for example, then adding N elements would be an O(N^2) operation!ArrayList doesn't let you customize its growth policy, since you're not even supposed to know (nor care, really!).Hashtable h = new Hashtable();
Hashtable h1 = new Hashtable();
Hashtable h2 = new Hashtable();
Set s = h.keySet();
int i = 0;
for (Object key : s) {
if ( i++ < 3) {
h1.put(key, h.get(key));
} else {
h2.put(key, h.get(key));
}
}
String key = "hello";
Multimap<String, Integer> myMap = HashMultimap.create();
myMap.put(key, 1);
myMap.put(key, 5000);
System.out.println(myMap.get(key)); // prints either "[1, 5000]" or "[5000, 1]"
myMap = ArrayListMultimap.create();
myMap.put(key, 1);
myMap.put(key, 5000);
System.out.println(myMap.get(key)); // always prints "[1, 5000]"
Multimap is not an exact equivalent of the home-baked solution; Hashtable synchronizes all its methods, while Multimap makes no such guarantee. This means that using a Multimap may cause you problems if you are using it on multiple threads. If your map is used only on one thread, it will make no difference (and you should have been using HashMap instead of Hashtable anyway).