Collections in Java

Hierarchy of the Collection framework

Methods of the Collection Interface

  1. add(Object): This method is used to add an object to the collection.
  2. addAll(Collection c): This method adds all the elements in the given collection to this collection.
  3. clear(): This method removes all of the elements from this collection.
  4. contains(Object o): This method returns true if the collection contains the specified element.
  5. equals(Object o): This method compares the specified object with this collection for equality.
  6. hashCode(): This method is used to return the hash code value for this collection.
  7. isEmpty(): This method returns true if this collection contains no element.
  8. iterator(): This method returns an iterator over elements in the collection.
  9. max(): This method is used to return the maximum value present in the collection.
  10. parallelStream(): This method returns a parallel Stream with this collection as its source.
  11. remove(Object o): This method is used to remove the given object from the collection. If there are duplicate values, then this method removes the first occurrence of the object.
  12. removeAll(Collection c): This method is used to remove all the objects mentioned in the given collection from the collection.
  13. removeIf(Predicate filter): This method is used to removes all the elements of this collection that satisfy the given predicate.
  14. retainAll(Collection c): This method is used to retains only the elements in this collection that are contained in the specified collection.
  15. size(): This method is used to return the number of elements in the collection.
  16. spliterator(): This method is used to create a Spliterator over the elements in this collection.
  17. stream(): This method is used to return a sequential Stream with this collection as its source.
  18. toArray(): This method is used to return an array containing all of the elements in this collection.

Interfaces that extend the Collections Interface

  1. Iterable Interface: This is the basic interface for the whole collection framework. The gathering interface extends the iterable interface. Therefore, inherently, all the interfaces and classes implement this interface. The most functionality of this interface is to supply an iterator for the collections.
    Syntax: Iterator iterator();
  • ArrayList: ArrayList provides us dynamic array list. The dimensions of an ArrayList are increased automatically if the gathering grows or shrinks if the objects are far away from the gathering.
    Syntax: List<T> al = new ArrayList<>();
  • LinkedList: It is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses.
    Syntax: List<T> al = new LinkedList<>();
  • Vector: A vector provides us with dynamic arrays. Though it’s going to be slower than standard arrays in programs where many manipulations within the array are required. This is often just like ArrayList in terms of implementation. The difference between a vector and an ArrayList is that a Vector is synchronized and an ArrayList is non-synchronized.
    Syntax: List<T> al = new Vector<>();
  • Stack: Stack class models and implements the stack arrangement. the category is predicated on the essential principle of last-in-first-out. additionally, to the essential push and pop operations, the category provides three more functions of empty, search and peek.
    Syntax: Stack<T> stack = new Stack<>();
  • Priority Queue: PriorityQueue is employed when the objects are alleged to be processed supported the priority. The PriorityQueue is predicated on the priority heap. the weather of the priority queue is ordered consistent with the natural ordering.
    Syntax: Queue<T> pq = new PriorityQueue<>();
  • HashSet: HashSet data structures are not inserted data in the same manner as implementation. The objects are inserted based on their hashcode.
    Syntax: Set<T> hs = new HashSet<>();
  • LinkedHashSet: LinkedHashSet is similar to a HashSet. It is a doubly-linked list to store the data and retains the ordering of the elements.
    Syntax: Set<T> lhs = new LinkedHashSet<>();
  • TreeSet: The ordering of the weather is maintained by a group using their natural ordering whether or not a particular comparator is provided. This must be according to equals if it’s to properly implement the Set interface. It also can be ordered by a Comparator provided at set creation time, counting on which constructor is employed and sorting occurs as BST.
    Syntax: Set<T> ts = new TreeSet<>();
  • HashMap: HashMap provides the basic implementation of the Map and it stores data in key-value pairs.
    Syntax: Map<T> hm = new HashMap<>();
  • TreeMap: TreeMap is similar to HashMap. The difference between both of them is TreeMap stores the data in sorted order of key in mapping.
    Map<T> tm = new TreeMap<>();




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Easily Publish an ASP.NET Core App in Linux Docker that Compresses PDF Documents

Reviving Java IoC/DI and why does it matters

Code2graph, easily visualize a project’s dependency graph

Getting Access Token in OAuth2.0 using Rest Assured

How To Use APIs To Update And Enrich Your Competitors’ Data

Best Read-Aloud Websites For Kids

Creating a Firebase Project and connecting it to an App

A step-by-step guide to getting started with HTML tables

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ace Mourya

Ace Mourya

More from Medium

Collections in Java

All new Java 8 — time API — LocalDate

What Is java Date