Collection Framework

 


The Collection Interface

It declares the core methods that all collections will have.  
  1.  boolean add(Object obj) : Adds obj to the invoking collection. Returns true if obj was added to the collection. Returns false if obj is already a member of the collection, or if the collection does not allow duplicates.
  2. boolean addAll(Collection c) : Adds all the elements of c to the invoking collection. Returns true if the operation succeeds (i.e., the elements were added). Otherwise, returns false.
  3. void clear( ) : Removes all elements from the invoking collection.
  4. boolean contains(Object obj) : Returns true if obj is an element of the invoking collection. Otherwise, returns false.
  5. boolean containsAll(Collection c) : Returns true if the invoking collection contains all elements of c. Otherwise, returns false.
  6. boolean equals(Object obj) : Returns true if the invoking collection and obj are equal. Otherwise, returns false.
  7. int hashCode( ) : Returns the hash code for the invoking collection
  8. boolean isEmpty( ) : Returns true if the invoking collection is empty. Otherwise, returns false.
  9. Iterator iterator( ) : Returns an iterator for the invoking collection.
  10. boolean remove(Object obj) : Removes one instance of obj from the invoking collection. Returns true if the element was removed. Otherwise, returns false.
  11. boolean removeAll(Collection c) : Removes all elements of c from the invoking collection. Returns true if the collection changed (i.e., elements were removed). Otherwise, returns false.
  12. boolean retainAll(Collection c) : Removes all elements from the invoking collection except those in c. Returns true if the collection changed (i.e., elements were removed). Otherwise, returns false.
  13. int size( ) : Returns the number of elements held in the invoking collection.
  14. Object[ ] toArray( ) : Returns an array that contains all the elements stored in the invoking collection. The array elements are copies of the collection elements.
  15. Object[ ] toArray(Object array[ ]) : Returns an array containing only those collection elements whose type matches that of array.

The List Interface

The List interface extends Collection 
  1. Elements can be inserted or accessed by their position in the list, using a zero-based index 
  2. A list may contain duplicate elements.
  3. In addition to the methods defined by Collection, List defines some of its own, which are
Methods

  1. void add(int index, Object obj) : Inserts obj into the invoking list at the index passed in the index. Any pre-existing elements at or beyond the point of insertion are shifted up. Thus, no elements are overwritten.
  2. boolean addAll(int index, Collection c) : Inserts all elements of c into the invoking list at the index passed in the index. Any pre-existing elements at or beyond the point of insertion are shifted up. Thus, no elements are overwritten. Returns true if the invoking list changes and returns false otherwise.
  3. Object get(int index) : Returns the object stored at the specified index within the invoking collection.
  4. int indexOf(Object obj) : Returns the index of the first instance of obj in the invoking list. If obj is not an element of the list, .1 is returned.
  5. int lastIndexOf(Object obj) : Returns the index of the last instance of obj in the invoking list. If obj is not an element of the list, .1 is returned.
  6. ListIterator listIterator( ) : Returns an iterator to the start of the invoking list.
  7. ListIterator listIterator(int index) : Returns an iterator to the invoking list that begins at the specified index.
  8. Object remove(int index) : Removes the element at position index from the invoking list and returns the deleted element. The resulting list is compacted. That is, the indexes of subsequent elements are decremented by one.
  9. Object set(int index, Object obj) : Assigns obj to the location specified by index within the invoking list.
  10. List subList(int start, int end) : Returns a list that includes elements from start to end.1 in the invoking list. Elements in the returned list are also referenced by the invoking object.

The Set Interface

A Set is a Collection that cannot contain duplicate elements.
The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
The objects that we insert into the HashSet do not guarantee to be inserted in the same order. The objects are inserted based on their hashcode. This class also allows the insertion of NULL elements.
HashSet also implements Serializable and Cloneable interfaces.
For More information about :
HashSet Class Click here https://javatfromp.blogspot.com/2022/08/hashset-class.html

LinkedHashSet Class Click Here : https://javatfromp.blogspot.com/2022/08/linkedhashset-class.html

TreeSet Class Click Here https://javatfromp.blogspot.com/2022/08/treeset-class.html#more

Queue Interface

used to hold the elements about to be processed in FIFO(First In First Out) order.


  • common classes are the PriorityQueue and LinkedList in Java. Note that neither of these implementations is thread-safe. PriorityBlockingQueue is one alternative implementation if the thread-safe implementation is needed.
  • If any null operation is performed on BlockingQueues, NullPointerException is thrown
  • All Queues except the Deques supports insertion and removal at the tail and head of the queue respectively. The Deques support element insertion and removal at both ends.



 



Comments

Popular posts from this blog

Software Engineering PPT

ERP Software Links

Include Tag JSP