Listes Vs HashTable Vs Queues
Looping through set,map,list
There are mainly 3 common segregation :
Listes = Similar to array but has add,remove,sort,not need to declare size and can store any datatype.
Set =Similar to Lists but no duplicates
Map = Similar to dictionary
Lists
Lists allow duplicate items, can be accessed by index, and support linear traversal.Array Vs List
(2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted.
(3) Deletion not possbile with arrays until unless some special techniques are used.
(4) Tests executing the following statements:
- createArray10000:
Integer[] array = new Integer[10000];
- createList10000:
List<Integer> list = new ArrayList<Integer>(10000);
Results (in nanoseconds per call, 95% confidence):
a.p.g.a.ArrayVsList.CreateArray1 [10.933, 11.097]
a.p.g.a.ArrayVsList.CreateList1 [10.799, 11.046]
a.p.g.a.ArrayVsList.CreateArray10000 [394.899, 404.034]
a.p.g.a.ArrayVsList.CreateList10000 [396.706, 401.266]
Conclusion: no noticeable difference in performance.
(6) Also Arraylist becomes the most flexible , when you have ArrayList Objects .ArrayList object satisfies all the above points and also can store any Datatype .
Example :
import java.util.ArrayList;
import java.util.List;
public class ArrayLst {
public static void main(String... args)
{
ArrayList al = new ArrayList();
al.add("Java4s");
al.add(12);
al.add(12.54f);
for(int i=0;i<al.size();i++)
{
Object o = al.get(i);
if(o instanceof String || o instanceof Float || o instanceof Integer)
System.out.println("Value is "+o.toString());
}
}
}
Output :
Value is Java4s
Value is 12
Value is 12.54
Value is 12
Value is 12.54
Maps Vs Dictionary
Hashes are look-ups in which you give each item in a list a "key" which will be used to retrieve it later. . Duplicate keys are not allowed.- HashMap - A basic key-value-pair map that functions like an indexed list.
- Dictionary - A hashtable that supports generic types and enforces type-safety.
- HashMap is implemented as a hash table, and there is no ordering on keys or values.
- TreeMap is implemented based on red-black tree structure, and it is ordered by the key.
- LinkedHashMap preserves the insertion order
- HashTable do not allow null keys and null values in the HashTable(Slow and Old )
Note :Why use HashMap instead of Dictionary and Hastable
http://stackoverflow.com/questions/2884068/what-is-the-difference-between-a-map-and-a-dictionary
Dictionary
= abstract class in Java
Map = interface.
Since, Java
does not support multiple inheritances, if a class extends Dictionary,
it cannot extend any other class.Therefore, the Map interface was
introduced.Dictionary class is obsolete and use of Map is preferred.
Queues
Queues control how items in a list are accessed. You typically push/pop records from a queue in a particular direction (from either the front or back). Not used for random access in the middle.- Stack - A LIFO (last in, first out) list where you push/pop records on top of each other.
- Queue - A FIFO (first in, first out) list where you push records on top and pop them off the bottom.
No comments:
Post a Comment