Google News
logo
Java Collections
Why collection
Until now, we use arrays to store a group of elements or objects. But there are some inconveniences in this mechanism. They are as follows:
We cannot store different class objects into the same array. The reason is that an array can store only one data type of elements.
Adding the objects at the end of an array is easy. But, inserting and deleting the elements in the middle of the array is difficult. In this case, we have to re-arrange all the elements of the array.
Retrieving the elements from an array is easy but after retrieving the elements, if we want to process them, then there are no methods available to carry out this.
Also arrays are not resizable.
Due to these problems, programmers want a better mechanism to store a group of objects. The alternative is using an object to store a group of other objects. It means we can use a class object as an array. Such an object is called collection object or container object.
Collection:-
1) collection can hold both homogeneous data and heterogeneous data
2) collections are growable in nature
3) Memory wise collections are good. Recommended to use.
4) Performance wise collections are not recommended to use .
Java Images
In the collection framework we having 9 key interfaces:-
1. Collection
2. List
3. Set
4. SortedSet
5. NavigablaSet
6. Queue
7. Map
8. SotedMap
9. NavigableMap
Collection methods
1) boolean add(obj) : This method appends the specified element to the end of the ArrayList. If the element is added successfully then the preceding method return true. 

2) void add(int i, obj) : This method inserts the specified element at the specified position, i in the ArrayList. 

3) element remove(int i) : This method removes the element at the specified position, i in the ArrayList. This method also returns the element which was removed from the ArrayList. 

4) boolean remove(obj) : This method removes the first occurrence of the specified element obj from the ArrayList, if it is present. 

5) void clear() : This method removes all the elements from the ArrayList. 

6) element set(int i, obj) : This method replaces an element at the specified position, i in the ArrayList with specified element obj. 

7) boolean contains(obj) : This method returns true if the ArrayList contains specified element obj. 

8) element get(int i) : This method returns the element available at the specified position in the ArrayList.

9) int indexOf(obj) : This method returns the position of first occurrence of the specified element obj in the ArrayList, or -1 if the element is not found in the list. 

10) int lastIndexOf(obj) : This method returns the position of last occurrence of the specified element obj in the ArrayList, or -1 if the element is not found in the list. 

11) int size() : This method returns the number of elements present in the list. 

12) Object[ ] toArray() : This method returns an object class type array containing all the elements in the ArrayList in proper sequence.