should not be duplicated, but map can have duplicated value, no? it is the key that can't be duplicated. no duplicated, no order , no special function. I think i will pick Set. Set can't have duplicated elements, no order and no special like the class collection. List can have duplicated values. Map can have duplicated values. Collection does have special functions. Correct me if i am wrong.
[This message has been edited by FEI NG (edited December 03, 2001).]
Here's the basic breakdown: Set interface-- extends Collection interface, no duplicates: TreeSet - sorted HashSet - no order List interface-- extends Collection interface, can have duplicates ArrayList - ordered by order of added Vector - same as ArrayList, but thread-safe LinkedList - can insert new entry to specified index Map interface-- DOESN'T extend Collection interface, no duplicate keys HashMap - values catalogued by keys HashTable - same as HashMap, but thread-safe TreeMap - keys sorted This is what I used to make sense of the collections framework in preparation for the exam. Hope it helps.
Originally posted by darryl failla: Here's the basic breakdown
Unfortunately, the breakdown given mixes interfaces with implementations; bad thing, IMHO. Rather:
Collection A "bag" of items, which may or may not be ordered, may or may not be sorted, and may or may not contain duplicate elements. There is no class that directly implements Collection itself.
List An ordered Collection. There are three implementations of List: ArrayList, which has O(1) performance for get() but O(n) performance for any add() or remove() except at the end of the list, LinkedList, which has O(n) performance for any get(), add() or remove() other than either the start or the end of the list, and Vector which has been largely superseded by ArrayList.
Set An unordered Collection that cannot contain duplicate elements. The HashSet class implements Set. It has O(1) behaviour for its contains(), add() and remove() operations.
SortedSet A Set with a sorting order, implemented by TreeSet. The contains(), add() and remove() operations take O(n log(n)) time.
Map A relation between a Set and a Collection, associating each element from the Set (the "key") with exactly one element of the Collection (the "value"). Multiple keys can be associated with the same value but not the other way around. The key Set may or may not be ordered, may or may not be sorted, but contains no duplicate elements. The value Collection may or may not be ordered, may or may not be sorted, and may or may not contain duplicate elements. It is implemented by HashMap, which has O(1) performance for contains(), get() and put(), and Hashtable which has been largely superseded by HashMap.
SortedMap A Map with a sorted key Set. It is implemented by TreeMap which has O(n log(n)) performance for contains(), get(), and put().
Don't be confused between sorted and ordered, they are two different concepts (although sorted can be seen as a special case of ordered). A sorting order can only be applied to objects that are Comparable or for which a Comparator is given. The Collections class contains a lot of static utility methods related to collections. Note especially the unmodifiableXXX() methods which will turn any collection in an unmodifiable one, and the synchronizedXXX() methods which produces instant synchronized collections. There are also methods to sort Lists, to search sorted Lists, methods that turn a single object into a singleton Map, Set or List, and static constants with an empty Map, Set, or List. Don't follow the herd - don't use Vector and Hashtable, they are awkward, bloated, and obsolete. Avoid using synchronized collections unless you know exactly what you're doing. - Peter [This message has been edited by Peter den Haan (edited December 04, 2001).]
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Peter, "Don't follow the herd - don't use Vector and Hashtable, they are awkward, bloated, and obsolete. Avoid using synchronized collections unless you know exactly what you're doing." Can you explain a bit more why are they awkaward, bloated and obsolete. Avoid using sysnchronized collections unless you.......... i thought it was the other way around. Can you explain that too??? THanks a lot!
Fei, using synchronized collection adds an overhead to the execution time of your program because of all synchronization issues. What Peter wanted to say is that if you know that a shared resource will not be accessed by two (or more) different objects, there is not need to use a collection providing synchronized access. You are better off using unsynchronized one and avoiding that overhead. This may seem secondary but believe me you'll encounter situation where you really gain a lot of execution time by not choosing synchronized collections. HIH ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform