You are creating a class that needs to be stored when in a collection which interface should you implement
Answers
Answer:
Hey guys follow me
Explanation:
There are some standard classes that implements Collection interface. Some of the classes provide full implementations that can be used as it is.Others are abstract classes, which provides skeletal implementations that can be used as a starting point for creating concrete collections.
The standard collection classes are:
Class Description
AbstractCollection Implements most of the Collection interface.
AbstractList Extends AbstractCollection and implements most of the List interface.
AbstractQueue Extends AbstractCollection and implements parts of the Queue interface.
AbstractSequentialList Extends AbstractList for use by a collection that uses sequential rather than random access of its elements.
LinkedList Implements a linked list by extending AbstractSequentialList
ArrayList Implements a dynamic array by extending AbstractList
ArrayDeque Implements a dynamic double-ended queue by extending AbstractCollection and implementing the Deque interface(Added by Java SE 6).
AbstractSet Extends AbstractCollection and implements most of the Set interface.
EnumSet Extends AbstractSet for use with enum elements.
HashSet Extends AbstractSet for use with a hash table.
LinkedHashSet Extends HashSet to allow insertion-order iterations.
PriorityQueue Extends AbstractQueue to support a priority-based queue.
TreeSet Implements a set stored in a tree. Extends AbstractSet.
Note:
To use any Collection class in your program, you need to import it in your program. It is contained inside java.util package.
Whenever you print any Collection class, it gets printed inside the square brackets [].
ArrayList class
Simple arrays have fixed size i.e it can store fixed number of elements. But, sometimes you may not know beforehand about the number of elements that you are going to store in your array. In such situations, We can use an ArrayList, which is an array whose size can increase or decrease dynamically.
ArrayList class extends AbstractList class and implements the List interface.
ArrayList supports dynamic array that can grow as needed. ArrayList has three constructors.
ArrayList() //It creates an empty ArrayList
ArrayList( Collection C ) //It creates an ArrayList that is initialized with elements of the Collection C
ArrayList( int capacity ) //It creates an ArrayList that has the specified initial capacity
ArrayLists are created with an initial size. When this size is exceeded, the size of the ArrayList increases automatically.
It can contain Duplicate elements and it also maintains the insertion order.
Manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.
ArrayLists are not synchronized.
ArrayList allows random access because it works on the index basis.
Example of ArrayList
import java.util.*
class Test
{
public static void main(String[] args)
{
ArrayList< String> al = new ArrayList< String>();
al.add("ab");
al.add("bc");
al.add("cd");
system.out.println(al);
}
}
[ab,bc,cd]
Getting Array from an ArrayList
toArray() method is used to get an array containing all the contents of the ArrayList. Following are some reasons for why you can need to obtain an array from your ArrayList:
To obtain faster processing for certain operations.
To pass an array to methods which do not accept Collection as arguments.
To integrate and use collections with legacy code.
Storing User-Defined classes
In the above mentioned example we are storing only string object in ArrayList collection. But You can store any type of object, including object of class that you create in Collection classes