This is a discussion on Map interface in Collection Api within the Java Programming forums, part of the Software Development category; .........................
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction. The Map interface follows. public interface Map<K,V> { // Basic operations V put(K key, V value); V get(Object key); V remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); int size(); boolean isEmpty(); // Bulk operations void putAll(Map<? extends K, ? extends V> m); void clear(); // Collection Views public Set<K> keySet(); public Collection<V> values(); public Set<Map.Entry<K,V>> entrySet(); // Interface for entrySet elements public interface Entry { K getKey(); V getValue(); V setValue(V value); } } The Java platform contains three general-purpose Map implementations: HashMap, TreeMap, and LinkedHashMap. Their behavior and performance are precisely analogous to HashSet, TreeSet, and LinkedHashSet, as described in The Set Interface section. Also, Hashtable was retrofitted to implement Map.
__________________ cheers Aman |
| |||
| The Map interface is not an extension of Collection interface. Instead the interface starts of it’s own interface hierarchy, for maintaining key-value associations. The interface describes a mapping from keys to values, without duplicate keys, by defination. The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Hash table is nothting which is the based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important. An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets. As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur. If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table. Note that this implementation is not synchronized. Code: Example
---------------
import java.util.*;
import java.io.*;
public class Hmap {
public static void main(String [] args) throws IOException {
BufferedReader Source = new BufferedReader(
new FileReader( args[0]));
HashMap HostCount = new HashMap();
String Line;
while ((Line = Source.readLine()) != null) {
String Host = (new Access(Line)).Host;
int was = 0;
Object Got;
if ((Got = HostCount.get(Host)) != null)
was = (((Integer)Got).intValue());
HostCount.put(Host, new Integer(was+1));
}
Set HostKeys = HostCount.keySet();
Iterator It = HostKeys.iterator();
while (It.hasNext()) {
String HostNow = (String)(It.next());
System.out.println(HostNow + " - " + HostCount.get(HostNow));
}
}
}
__________________ cheers Aman |
| |||
| * Map is an interface, while Hashtable is a concrete implementation if you've used Hashtable, you're already familiar with the general basics of Map. The following are the major differences: * Map provides Collection views instead of direct support for iteration via Enumeration objects. Collection views greatly enhance the expressiveness of the interface, as discussed later in this section. *Map allows you to iterate over keys, values, or key-value pairs; Hashtable does not provide the third option. *Map provides a safe way to remove entries in the midst of iteration; Hashtable did not. *Finally, Map fixes a minor deficiency in the Hashtable interface. Hashtable has a method called contains, which returns true if the Hashtable contains a given value. Given its name, you'd expect this method to return true if the Hashtable contained a given key, because the key is the primary access mechanism for a Hashtable. The Map interface eliminates this source of confusion by renaming the method containsValue. Also, this improves the interface's consistency — containsValue parallels containsKey
__________________ cheers Aman |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Garbage Collection C# | Balasubramanian.S | C# Programming | 2 | 03-17-2008 09:44 PM |
| What is Collection API ? | H2o | ASP and ASP.NET Programming | 2 | 01-07-2008 10:19 PM |
| garbage collection | anbuchezhians | Java Programming | 3 | 08-21-2007 02:46 AM |
| Do we have interface to schedule a task like ITaskscheduler interface for desktop.... | theone | Mobile Software Development | 1 | 07-24-2007 11:29 PM |
| Garpage collection | leoraja8 | Java Programming | 0 | 05-14-2007 04:47 AM |