java collections
Container object:- It is an object that store group of other objects. This is called as container object.
Container Class:- It is a class or collection class whose object can store group of other object.
All the container classes are defined in java.util.* package.
Group of container collection classes called “Collection Framework”.
Collection framework:- Is a class library to handle a group of objects. A container object stores the reference of other objects. Reference-unique id given to object by the JVM at the time of creation.
Operations we will use with collections:-
- Add objects to the collection.
- Remove the objects from collection.
- Find out if an object or group of objects in the collection.
- Retrieve an objects from the collection.
- Iterate through the collection, looking each element (object) one after
another.
Key interfaces of the collection Framework:-
Collection | Set | SortedSet |
List | Map | SortedMap |
Queue | NavigableSet | NavigableMap |
Core concrete implementation classes:-
MAP | SET | LIST | QUEUE | UTILITIES |
---|---|---|---|---|
Hash Map | Hash Set | Array List | Priority Queue | Collections |
Hashtable | Vector | SortedMap | Arrays | |
Tree Map | Tree Set | Linked List | ||
Linkedhash Map |
List:-
- A list cares about the index.
- The one thing that list has that non-list don’t have is a set of methods related to the index.
- Those key methods include things like get(int index), index(object o),
add(int index, object o)and so on. All three list implementations are ordered by index postion.
List implementations are:-
Array List:-
- Array List is a growable array.
- Gives fast iteration and fast random access.
- It is an ordered collection(by index), not sorted.
- After 1.4 version, Array List implemented Random-access interface
(marker interface). This supports fast random access. - Choose this over Linked List when you need fast iteration but not
likely to be doing a lot of insertion and deletion.
Vector:-
- Vector is basically the same as Array List but vector methods are synchronized for thread safety.
- Vector is the only class other than Array List that implements Random access.
- Normally we use Array List instead of vector because of synchronization methods hits performance.
- If we need thread safety, there are utility methods in class collections that can help.
- Collections.synchronizedList(list);
Linked List:-
- A Linked List is order by index position. Like Array List, except that the
elements are doubly linked to one another. - This linkage gives you new methods for adding and removing from
beginning or end. - Iteration is slower than Array List.
- Good in insertion and deletion.
Set:-
- Set cares about uniqueness. It does not allow duplicates.
- Equals () methods determines whether two objects are identical.
Set implementations:-
Hash Set:-
- Hash Set is unsorted and unordered.
- Use this class when you want a collection with no duplicate and don’t care about order when iterate through it.
Linked Hash Set:-
- Linked HashSet is a order version of HashSet that maintains the double linked list across all elements.
- Use this class inserted of HashSet when you care about the iteration order.
- Iterate through the elements in the order in which they were inserted.
Tree Set:-
- Tree Set is one of the two sorted collections (the other being TreeMap). It guarantees that the elements will be in ascending order according to the natural order.
- Optionally, we can construct a TreeSet with a constructor that lets you give the collection you own rules for what the order should be by using comparable or comparator.
Map:-
- Map cares about unique identifiers. Map provides a unique key (the id) to a specific value, where both key and the value are of course objects.
- Map implementations let you things like search for a value based on the keys.
- Map rely on the equals() method to determine whether two keys are the same of different.
Map implementations:-
Hash Map:-
- Hash map gives you an unsorted, unordered map.
- When you need a map and you don’t care about the order (when
you iterate through it). The HashMap is the best way to go. - Hash Map allows one null key and multiple null values.
Hash Table:-
- Like vector, Hashtable has existed from prehistoric java times.
- Name inconsistency: hashmap vs hashtable.
- Hashtable is synchronized counterpart of hashmap.
- Hashtable doesn’t allow anything that is null as key or value.
Linked HashMap:-
- Maintains insertion order.
- Slower than HashMap for adding and removing.
- Can expect faster iteration with LinkedHashMap.
Tree Map:-
- Tree map is a sorted map.
- By default this means sorted by the natural order of the elements.
- Like TreeSet, TreeMap lets you define a custom sort order(via comparable or comparator).
Class | Map | Set | List | Ordered | Sorted |
---|---|---|---|---|---|
HashMap | Yes | No | No | ||
Hashtable | No | No | |||
TreeMap | Yes | Sorted | By natural order or custom comparison rules | ||
LinkedHashMap | Yes | By Insertion Order | No | ||
HashSet | Yes | No | No | ||
TreeSet | Yes | Sorted | By natural order or custom comparison rules | ||
LinkedHashSet | Yes | By Insertion Order | No | ||
ArrayList | Yes | By Index | No | ||
Vector | Yes | By Index | No | ||
LinkedList | Yes | By Index | No | ||
PriorityQueue | Sorted | By to do order |
Difference between Iterator and ListIterator?
No | Iterator | ListIterator |
---|---|---|
1 | Iterator traverse the elements in forward direction only. | List Iterator traverses the elements in backward and forward directions both. |
2 | Iterator can be used in List, Set and Queeue. | ListIterator can be used in Linked only . |
Difference between Iterator and Enumeration?
No | Iterator | Enumeration |
---|---|---|
1 | Iterator can traverse legacy and non-legacy elements. | Enumeration can traverse only legacy elements. |
2 | Iterator is fall-fast. | Enumeration is not fall-fast |
2 | Iterator is slower than enumeration. | Enumeration is faster than iterator. |