What is Collection?
In simple term, you can say collection is a container — is simply an object that groups multiple elements into a single unit.
For instance, Collection correspond to a bag. Typically, they represent data items that form a natural group such as a collection of cards,a collection of letters, and a mapping of names to phone numbers.
What is a Collection Framework?
A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:
- Interfaces Represent different types of collections, such as sets, lists, and maps. These interfaces form the basis of the framework.
- Implementations Primary implementations of the collection interfaces.
- Algorithms Static methods that perform useful functions on collections, such as sorting a list.
Advantages of a collections framework
- A usable set of collection interfaces : By implementing one of the basic interfaces -- Collection, Set, List, or Map -- you ensure your class conforms to a common API and becomes more regular and easily understood
- A basic set of collection implementations : Using an existing, common implementation makes your code shorter and quicker to download. Also, using existing Core Java code core ensures that any improvements to the base code will also improve the performance of your code.
- Reduces programming effort by providing data structures and algorithms so you don't have to write them yourself.
- Provides interoperability between unrelated APIs by establishing a common language to pass collections back and forth.
- Reduces the effort required to learn APIs by requiring you to learn multiple ad hoc collection APIs.
- Reduces the effort required to design and implement APIs by not requiring you to produce ad hoc collections APIs.
Collection is an interface with declarations of the methods common to most collections including add(), remove(), contains(), size(), and iterator().
- Collection Framework Map
- Key Interfaces and Classes of the Collections Framework
- Key Classes of the Collections Framework
Arraylist
This is Resizable-array implementation of the List interface
- ArrayList is an array based implementation where elements can be accessed directly via the get and set methods.
- Default choice for simple sequence.
- It gives you fast iteration and fast random access
- It is an ordered collection (by index), but not sorted
Vector
A Vector is basically the same as an ArrayList, but Vector methods are synchronized for thread safety.
You'll normally want to use ArrayList instead of Vector because the synchronized methods add a performance hit you might not need.
A Vector is basically the same as an ArrayList, but Vector methods are synchronized for thread safety.
You'll normally want to use ArrayList instead of Vector because the synchronized methods add a performance hit you might not need.
Difference between ArrayList and Vector?
- Vectors are synchronized, ArrayLists are not.
If multiple threads access an ArrayList concurrently then we must externally synchronize the block of code which modifies the list either structurally or simply modifies an element. Structural modification means addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification. - Data Growth Methods
A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.
Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. It's always best to set the object's initial capacity to the largest capacity that your program will need.
Vector is often considered deprecated nowadays.
LinkedList
A LinkedList is ordered by index position, like ArrayList, except that the elements are doubly-linked to one another.
This linkage gives you new methods (beyond what you get from the List interface) for adding and removing from the beginning or end, which makes it an easy choice for implementing a stack or queue.
A LinkedList is ordered by index position, like ArrayList, except that the elements are doubly-linked to one another.
This linkage gives you new methods (beyond what you get from the List interface) for adding and removing from the beginning or end, which makes it an easy choice for implementing a stack or queue.
Difference between ArrayList and LinkedList?
LinkedList and ArrayList are two different implementations of the List interface.
LinkedList implements it with a doubly-linked list.
LinkedList implements it with a doubly-linked list.
ArrayList implements it with a dynamically resizing array.
- LinkedList<E> allows for constant-time insertions or removals using iterators, but only sequential access of elements. In other words, you can walk the list forwards or backwards, but finding a position in the list takes time proportional to the size of the list.
- ArrayList<E>, on the other hand, allow fast random read access, so you can grab any element in constant time. But adding or removing from anywhere but the end requires shifting all the latter elements over, either to make an opening or fill the gap.
- Use LinkedList when you need fast insertion and deletion whereas which is slow in ArrayList
- Use ArrayList when you need fast iteration and fast random access whereas which is slow in LinkedList
HashSet
- A HashSet is an unsorted, unordered Set.
- It uses the hashcode of the object being inserted, so the more efficient your hashCode() implementation the better access performance you'll get.
- Use this class when you want a collection with no duplicates and you don't care about order when you iterate through it.
- This class permits the null element
- If you attempt to add an element to a set that already exists in the set, the duplicate element will not be added, and the add() method will return false
LinkedHashSet
- A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements.
- Use this class instead of HashSet when you care about the iteration order.
- When you iterate through a HashSet the order is unpredictable, while a LinkedHashSet lets you iterate through the elements in the order in which they were inserted.
- permits null elements
It provides constant-time performance for the basic operations (add, contains and remove), assuming the hash function disperses elements properly among the buckets.
Difference between HashSet and LinkedHashSet?
LinkedHashSet performance is likely to be just slightly below that of HashSet, due to the added expense of maintaining the linked list with one exception:- Iteration over a LinkedHashSet requires time proportional to the size of the set, regardless of its capacity.
- Iteration over a HashSet is likely to be more expensive, requiring time proportional to its capacity.
TreeSet
The TreeSet is one of two sorted collections (the other being TreeMap). It uses a Red-Black tree structure, and guarantees that the elements will be in ascending order, according to natural order.
The TreeSet is one of two sorted collections (the other being TreeMap). It uses a Red-Black tree structure, and guarantees that the elements will be in ascending order, according to natural order.
- Guarantees log(n) time cost for the basic operations (add, remove and contains).
- Offers a few handy methods to deal with the ordered set like first(), last(), headSet(), and tailSet() etc
- Tress set will not allow null object .if you try to add null value i will be throw null pointer exception
Difference between HashSet and TreeSet?
HashSet- Hash set allow null object
- class offers constant time performance for the basic operations (add, remove, contains and size).
- Does not guarantee that the order of elements will remain constant over time
- Iteration performance depends on the initial capacity and the load factor of the HashSet.
- It's quite safe to accept default load factor but you may want to specify an initial capacity that's about twice the size to which you expect the set to grow
TreeSet
- Tress set will not allow null object .if you try to add null value i will be throw null pointer exception
- log(n) time cost for the basic operations (add, remove and contains).
- elements of set will be sorted (ascending, natural, or the one specified by you via it's constructor)
- Doesn't offer any tuning parameters for iteration performance
HashMap
- The HashMap gives you an unsorted, unordered Map, it does not guarantee that the order will remain constant over time.
- HashMap allows one null key and multiple null values in a collection
- When you need a Map and you don't care about the order (when you iterate through it), then HashMap is the way to go; the other maps add a little more overhead
- The more efficient your hashCode() implementation, the better access performance you'll get.
- The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
- This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets.
Hashtable
- Just as Vector is a synchronized counterpart to the sleeker, more modern ArrayList, Hashtable is the synchronized counterpart to HashMap.
- Remember that you don't synchronize a class, so when we say that Vector and Hashtable are synchronized, we just mean that the key methods of the class are synchronized
How Hashtable differ from HashMap?
- Hashtable doesn't let you have anything that's null whereas HashMap allows one null key and multiple null values in a collection
- HashMap is not Synchronized where as HashTable is Synchronized.
- HashMap cannot be shared with multiple thread without proper synchronization where HashTable is thread safe and can be shared between multiple thread
- Iterator in HashMap if fail fast where as Enumerator in HashTable is not fail fast
LinkedHashMap
- Like its Set counterpart, LinkedHashSet, the LinkedHash- Map collection maintains insertion order (or, optionally, access order).
- Slower than HashMap for adding and removing elements, you can expect faster iteration with a LinkedHashMap.
TreeMap
- TreeMap is a sorted Map.
[출처]원본



댓글 없음:
댓글 쓰기