For example, whenever we try to book a ticket, the tickets are sold at the first come first serve basis. Because all collections implement Collection, familiarity with its methods is necessary for a clear understanding of the framework. Otherwise, returns false. Returns false if obj is already a member of the collection, or if the collection does not allow duplicates. Difference between == and .equals() method in Java. Adds obj to the invoking collection. The implementation classes of List interface are ArrayList, LinkedList, Stack and Vector. It inhibits a list type data … Returns an array containing only those collection elements whose type matches that of array. Removes all elements from the invoking collection except those in c. Returns true if the collection changed (i.e., elements were removed). This list interface is implemented by various classes like ArrayList, Vector, Stack, etc. The Collection interface is a member of the Java Collections Framework. The sub-interfaces of Collection are BeanContext, BeanContextServices, BlockingDeque, BlockingQueue, Deque, EventSet, List, NavigableSet, Queue, Set, SortedSet, TransferQueue . Returns a possibly parallel Stream with this collection as its source. It is a factory of ListIterator interface. Since this class implements the deque, we can instantiate a deque object with this class. Returns true if the invoking collection and obj are equal. If a collection implementation doesn’t implement a particular operation, it should define the corresponding method to throw UnsupportedOperationException. Returns the number of elements in this collection. Here, E is the type of elements stored in the collection. Returns true if obj was added to the collection. Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Otherwise, returns false. code. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. The Collection interface is used to pass around collections of objects where maximum generality is desired. This collection is used when we wish to avoid the duplication of the objects and wish to store only the unique objects. List interface is the child interface of Collection interface. Returns true if the operation succeeds (i.e., the elements were added). Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html. Compares the specified object with this collection for equality. 2. boolean addAll (Collection c) Adds all the elements of c to the invoking collection. Returns an array that contains all the elements stored in the invoking collection. The Collection interface is the foundation upon which the collections framework is built. The only difference is that this interface has extra methods that maintain the ordering of the elements. Returns a sequential Stream with this collection as its source. To iterate over the elements of Collection we can use iterator() method. Several of these methods can throw an UnsupportedOperationException. The sorted set interface extends the set interface and is used to handle the data which needs to be sorted. Interface java.util.Collection size. Attention reader! We use cookies to ensure you have the best browsing experience on our website. Removes all the elements from this collection (optional operation). The Collection interface is not directly implemented by any class. Returns true if the element was removed. However, it is implemented indirectly via its subtypes or subinterfaces like List, Queue, and Set. All the Classes of the Collection Framework implement the subInterfaces of the Collection Interface. The class which implements this interface is TreeSet. For example. Returns true if the invoking collection is empty. Removes a single instance of the specified element from this collection, if it is present (optional operation). It declares the core methods that all collections will have. Otherwise, returns false. By using our site, you Since all the subclasses implement the list, we can instantiate a list object with any of these classes. Deque: This is a very slight variation of the queue data structure. Adds all the elements of c to the invoking collection. How to determine length or size of an Array in Java? The add(E e) and addAll(Collection c) methods provided by Collection can be used to add elements. The Collection interface is not directly implemented by any class. Returns an iterator over the elements in this collection. acknowledge that you have read and understood our, GATE CS Original Papers and Official Keys, ISRO CS Original Papers and Official Keys, ISRO CS Syllabus for Scientist/Engineer Exam, Split() String method in Java with examples, Object Oriented Programming (OOPs) Concept in Java, Different ways for Integer to String Conversions In Java. Returns the hash code value for this collection. The Collection interface is a member of the Java Collections Framework. Therefore, the person whose request arrives first into the queue gets the ticket. This interface extends the queue interface. For example, by convention all general-purpose collection implementations have a constructor that takes a Collection argument. Experience. Returns the hash code for the invoking collection. Note: In the above syntax, we can replace any class with ArrayList if that class implements the Collection interface. This interface is dedicated to the … This also allows duplicate data to be present in it. Otherwise, returns false. Since this class implements the SortedSet, we can instantiate a SortedSet object with this class. Please write to us at contribute@geeksforgeeks.org to report any issue with the above content. The class which implements this interface is ArrayDeque. It is a part of java.util package. For example, the ArrayList class implements the List interface which is a subinterface of the Collection Interface. 1. boolean add (Object obj) Adds obj to the invoking collection. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html, Java 8 | Consumer Interface in Java with Examples, Java 8 | BiConsumer Interface in Java with Examples, Java 8 | IntToDoubleFunction Interface in Java with Examples, Java 8 | DoubleToLongFunction Interface in Java with Examples, Java.util.function.BiPredicate interface in Java with Examples, Java.util.function.DoublePredicate interface in Java with Examples, Java.util.function.LongPredicate interface in Java with Examples, Java.util.function.IntPredicate interface in Java with Examples, Collection addAll() method in Java with Examples, Collection add() method in Java with Examples, Collection clear() method in Java with Examples, Collection isEmpty() method in Java with Examples, Collection contains() method in Java with Examples, SortedSet Interface in Java with Examples, SortedMap Interface in Java with Examples, ToLongFunction Interface in Java with Examples, LongToIntFunction Interface in Java with Examples, ToIntFunction Interface in Java with Examples, Java Program to Change a Collection to an Array, Different ways of Reading a text file in Java, Comparator Interface in Java with Examples, Difference between Abstract Class and Interface in Java.