Map Implementations
Now, for the reason that Map is an interface, one would require to instantiate a functionality of the interface that is concrete so that the same can be used effectively. One has the liberty to selecting from the subsequent Map implementations that are present in the java Collections API. you need to instantiate a concrete implementation of the interface in order to use it.
- java.util.HashMap
- java.util.Hashtable
- java.util.EnumMap
- java.util.IdentityHashMap
- java.util.LinkedHashMap
- java.util.Properties
- java.util.TreeMap
- java.util.WeakHashMap
The most commonly used ones are the HashMap and TreeMap.
Each one of these behaves a bit different as far as the sequence of the elements are concerned on iterating the same and the time it takes to update or put in the elements in the sets and access the same.
Hashmaps maps a key and a value but do not assure on any elements sequence that are stored internally in the map.
On the other hand, it is opposite with the Treemap which alos maps a key and a value however it does guarantees or assures on the order or sequence in which the values or keys are iterated which is nothing but the sort order of keys or values.
Let us take on the below illustrations on the method to create a map instance:
Map map1 = new HashMap(); Map map2 = new TreeMap();
Elements Addition and Accessing
Adding of an element is done by calling the s put() method. Here are a few examples:
Map map1 = new HashMap(); Map1.put("key1", "element 1"); Map1.put("key2", "element 2"); Map1.put("key3", "element 3");
The above three mentioned put() calls task is to map a string value to a string key so that post doing the same one can then retrieve the value making use of the key. Doing the same would require to make use of the get() method:
String element1 = (String) map1.get("key1");
Either the keys or the values of a Map can be iterated. Here is how you do that:
// key iterator Iterator iterator = map1.keySet().iterator(); // value iterator Iterator iterator = map1.values();
More often than not, we use to iterate the keys of the Map and post doing the same, attempt to retrieve the corresponding values during the iteration. Here is how it looks:
Iterator iterator = map1.keySet().iterator(); while(iterator.hasNext(){ Object key = iterator.next(); Object value = map1.get(key); } //access via new for-loop for(Object key : mapA.keySet()) { Object value = mapA.get(key); }
Removal of Elements
Elements can be removed by making use of calling the remove(Object key) method thereby making you to remove the (key, value) pair that is matching the key.
Generic Maps
Object into a Map can be inserted by default if your system has before java 5 but from Java 5, there is a limitation on the object types that can be made use of for both keys as well as the values in a Map. Here is an example:
Map<String, MyObject> map = new HashSet<String, MyObject>();
And now there will be only String objects for keys and MyObject that will be accepted by the map. Post this, one can go ahead and access the keys and values and iterate the same as well without the need to casting them. Here is how it looks:
for(MyObject anObject : map.values()){ //do someting to anObject... } for(String key : map.keySet()){ MyObject value = map.get(key); //do something to value }
Let us take a look at the other subtype “Queue’s Interface”
The java.util.Queue is nothing but an interface that is a subtype of the java.util.Collection interface. It defines an ordered list of objects much on the similar lines of a List, however the way it is being used is bit different. Here in the queue is designed in such a way that the elements are always added at the end of the queue. And when it comes to removal of the elements, they are removed from the beginning of the queue.
Here is a list of the topics covered in this text:
- Queue Implementations
- Adding&Accessing_Elements
Queue Implementations
All the methods that are present in the Collection interface are present in Queue interface as well.
For the reason that Queue is an interface, you would need to instantiate a functionality that is concrete so that the same can be used effectively. One can select from the subsequent Queue implementations in the API of Java Collections.
- java.util.LinkedList
- java.util.PriorityQueue
LinkedList is a pretty benchmark queue operation.
PriorityQueue has the habit of storing the elements internally as per the natural order in case the Comparable is implemented, or as per the Comparator passed to the PriorityQueue. Here are a few instances on how to create a Queue instance:
Queue queueA = new LinkedList(); Queue queueB = new PriorityQueue();
Adding & Accessing Elements
add() method can be made use of to add the element in the queue which is inherited from the Collection interface. Here are a few examples:
Queue queue1 = new LinkedList(); queue1.add("element 1"); queue1.add("element 2"); queue1.add("element 3");
The implementation decides on the order of elements (that are stored internally) in which they will be added to the queue. The same holds valid for retrieval of elements as well from the queue. One can glance at the element at the head of the queue exclusive of taking it out of the queue and is made possible through the element() method. Here is how that looks:
Object firstElement = queue1.element();
remove() method can be made use of in order to take out the first element of the queue.
One can also go ahead and iterate all elements of a queue, as an alternative of just handing out one at a time. Here is how that looks:
Queue queue1 = new LinkedList(); queue1.add("element 0"); queue1.add("element 1"); queue1.add("element 2"); //access via Iterator Iterator iterator = queue1.iterator(); while(iterator.hasNext(){ String element = (String) iterator.next(); } //access via new for-loop for(Object object : queue1) { String element = (String) object; }
Example of Map Interface:
Here is the code of program:
import java.util.*; public class TreeMapExample{ public static void main(String[] args) { System.out.println("Tree Map Example!\n"); TreeMap <Integer, String>tMap = new TreeMap<Integer, String>(); //Addding data to a tree map tMap.put(1, "Sunday"); tMap.put(2, "Monday"); tMap.put(3, "Tuesday"); tMap.put(4, "Wednesday"); tMap.put(5, "Thursday"); tMap.put(6, "Friday"); tMap.put(7, "Saturday"); //Rerieving all keys System.out.println("Keys of tree map: " + tMap.keySet()); //Rerieving all values System.out.println("Values of tree map: " + tMap.values()); //Rerieving the value from key with key number 5 System.out.println("Key: 5 value: " + tMap.get(5)+ "\n"); //Rerieving the First key and its value System.out.println("First key: " + tMap.firstKey() + " Value: " + tMap.get(tMap.firstKey()) + "\n"); //Rerieving the Last key and value System.out.println("Last key: " + tMap.lastKey() + " Value: " + tMap.get(tMap.lastKey()) + "\n"); //Removing the first key and value System.out.println("Removing first data: " + tMap.remove(tMap.firstKey())); System.out.println("Now the tree map Keys: " + tMap.keySet()); System.out.println("Now the tree map contain: " + tMap.values() + "\n"); //Removing the last key and value System.out.println("Removing last data: " + tMap.remove(tMap.lastKey())); System.out.println("Now the tree map Keys: " + tMap.keySet()); System.out.println("Now the tree map contain: " + tMap.values()); } }
Output of this program:
