What is the name of the interface used to represent collections that maintain non unique elements in order?
The Collection interface is a member of the Java Collections Framework. It is a part of java.util package. It is one of the root interfaces of the Collection Hierarchy. The Collection interface is not directly implemented by any class. However, it is implemented indirectly via its subtypes or subinterfaces like List, Queue, and Set. Show
For Example, the HashSet class implements the Set interface which is a subinterface of the Collection interface. If a collection implementation doesn’t implement a particular operation, it should define the corresponding method to throw UnsupportedOperationException. The Hierarchy of Collection It implements the
Iterable SubInterfaces of Collection Interface All the Classes of the Collection Framework implement the subInterfaces of the Collection Interface. All the methods of Collection interfaces are also contained in it’s subinterfaces. These subInterfaces are sometimes called as Collection Types or SubTypes of Collection. These include the following: List: This is a child interface of the collection interface. This interface is dedicated to the data of the list type in which we can store all the ordered collection of the objects. This also allows duplicate data to be present in it. This list interface is implemented by various classes like ArrayList, Vector, Stack, etc. Since all the subclasses implement the list, we can instantiate a list object with any of these classes. For example,
Set: A set is an unordered collection of objects in which duplicate values cannot be stored. This collection is used when we wish to avoid the duplication of the objects and wish to store only the unique objects. This set interface is implemented by various classes like HashSet, TreeSet, LinkedHashSet, etc. Since all the subclasses implement the set, we can instantiate a set object with any of these classes. For example,
SortedSet: This interface is very similar to the set interface. The only difference is that this interface has extra methods that maintain the ordering of the elements. The sorted set interface extends the set interface and is used to handle the data which needs to be sorted. The class which implements this interface is TreeSet. Since this class implements the SortedSet, we can instantiate a SortedSet object with this class. For example,
Queue: As the name suggests, a queue interface maintains the FIFO(First In First Out) order similar to a real-world queue line. This interface is dedicated to storing all the elements where the order of the elements matter. For example, whenever we try to book a ticket, the tickets are sold at the first come first serve basis. Therefore, the person whose request arrives first into the queue gets the ticket. There are various classes like PriorityQueue, Deque, ArrayDeque, etc. Since all these subclasses implement the queue, we can instantiate a queue object with any of these classes. For example,
Deque: This is a very slight variation of the queue data structure. Deque, also known as a double-ended queue, is a data structure where we can add and remove the elements from both the ends of the queue. This interface extends the queue interface. The class which implements this interface is ArrayDeque. Since this class implements the deque, we can instantiate a deque object with this class. For example,
Declaration: public interface CollectionHere, E is the type of elements stored in the collection. Example: Javaimport java.io.*; import java.util.*; public class CollectionDemo { public static void main(String args[]) { Collection list.add("Geeks"); list.add("for"); list.add("Geeks"); System.out.println("The list is: " + list); list.add("Last"); list.add("Element"); System.out.println("The new List is: " + list); } } Output The list is: [Geeks, for, Geeks] The new List is: [Geeks, for, Geeks, Last, Element]Implementing ClassesThe Collection interface is implemented by AbstractCollection, AbstractList, AbstractQueue, AbstractSequentialList, AbstractSet, ArrayBlockingQueue, ArrayDeque, ArrayList, AttributeList, BeanContextServicesSupport, BeanContextSupport, ConcurrentHashMap.KeySetView, ConcurrentLinkedDeque, ConcurrentLinkedQueue, ConcurrentSkipListSet, CopyOnWriteArrayList, CopyOnWriteArraySet, DelayQueue, EnumSet, HashSet, JobStateReasons, LinkedBlockingDeque, LinkedBlockingQueue, LinkedHashSet, LinkedList, LinkedTransferQueue, PriorityBlockingQueue, PriorityQueue, RoleList, RoleUnresolvedList, Stack, SynchronousQueue, TreeSet, Vector. Syntax: CollectionHere, E is the type of elements stored in the collection. Note: In the above syntax, we can replace any class with ArrayList if that class implements the Collection interface. Basic Operations1. Adding Elements The add(E e) and addAll(Collection c) methods provided by Collection can be used to add elements. Javaimport java.io.*; import java.util.*; public class AddingElementsExample { public static void main(String[] args) { Collection list1.add(15); list1.add(20); list1.add(25); for (Integer number : list1) { System.out.println("Number = " + number); } Collection list2.addAll(list1); System.out.println("The new ArrayList is: " + list2); } } Output Number = 15 Number = 20 Number = 25 The new ArrayList is: [15, 20, 25]2. Removing Elements The remove(E e) and removeAll(Collection c) methods can be used to remove a particular element or a Collection of elements from a collection. Javaimport java.util.*; public class RemoveElementsExample { public static void main(String[] argv) throws Exception { Collection set1.add(1); set1.add(2); set1.add(3); set1.add(4); set1.add(5); System.out.println("Initial set1 : " + set1); set1.remove(4); System.out.println("set1 after removing 4 : " + set1); Collection set2.add(1); set2.add(2); set2.add(3); System.out.println("Collection Elements to be removed : " + set2); set1.removeAll(set2); System.out.println("set 1 after removeAll() operation : " + set1); } } Output Initial set1 : [1, 2, 3, 4, 5] set1 after removing 4 : [1, 2, 3, 5] Collection Elements to be removed : [1, 2, 3] set 1 after removeAll() operation : [5]3. Iterating To iterate over the elements of Collection we can use iterator() method. Javaimport java.util.*; public class IteratingExample { public static void main(String[] args) { Collection list.add("Geeks"); list.add("for"); list.add("Geeks"); list.add("is"); list.add("a"); list.add("CS"); list.add("Students"); list.add("Portal"); System.out.println("The list is: " + list); Iterator System.out.println("\nThe iterator values" + " of list are: "); while (iter.hasNext()) { System.out.print(iter.next() + " "); } } } Output The list is: [Geeks, for, Geeks, is, a, CS, Students, Portal] The iterator values of list are: Geeks for Geeks is a CS Students PortalMethods of Collection
Methods declared in interface java.lang.Iterable
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html What is the name of the collection interface used to represent elements in a sequence?A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements.
Which collection interface is responsible for maintaining unique?Which of this interface must contain a unique element? Explanation: Set interface extends collection interface to handle sets, which must contain unique elements.
What is the name of collection interface?The sub-interfaces of Collection are BeanContext, BeanContextServices, BlockingDeque
Which collection would you use to keep sorted collection of unique objects?If you just want to sort a list, use any kind of List and use Collections. sort(). If you want to make sure elements in the list are unique and always sorted, use a SortedSet.
|