What is a collection?
In programming, a collection is a class used to represent a set of similar data type items as a single unit. These unit classes are used for grouping and managing related objects.
A collection has an underlying data structure that is used for efficient data manipulation and storage. Code readability and maintenance improve when collections are used in logical constructs.
What is a collection interface in java?
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.
For Example, the HashSet class implements the Set interface which is a sub-interface of the Collection interface.
The Hierarchy of Collection
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 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. click here for List examples
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. click here for set examples
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. click here for set examples
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 on a 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.click here for queue examples
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 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.Click here for Dequeue examples
Syntax for Collections:
Collection<E> objectName = new ArrayList<E>();
Here, E is the type of elements stored in the collection.
Some basic operations in a Collection interface:
Since Collection is an interface, it can be used only with a class that implements this interface.
1. Add Operation:
The add(E e) and addAll(Collection c) methods provided by Collection can be used to add elements.
Example:
import java.util.ArrayList;
import java.util.Collection;
public class Main {
public static void main(String[] args) {
Collection<String> listOne = new ArrayList<String>();
Collection<String> listTwo = new ArrayList<>();
listOne.add("Coders");
listOne.add("Arts");
for (String string :listOne)
{
System.out.println(string);
}
listTwo.addAll(listOne);
System.out.println(listTwo);
}
}
Output:
Coders
Arts
[Coders, Arts]
2. Removing Operation:
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.
Example:
import java.util.ArrayList;
import java.util.Collection;
public class Main {
public static void main(String[] args) {
Collection<String> listOne = new ArrayList<String>();
Collection<String> listTwo = new ArrayList<>();
listOne.add("Coders");
listOne.add("Arts");
listTwo.addAll(listOne);
listOne.remove("Coders");
listTwo.removeAll(listTwo);
System.out.println(listOne);
System.out.println(listTwo);
}
}
Output:
[Arts]
[]
3. Iterating Operation :
To iterate over the elements of Collection we can use iterator() method.
Example:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
Collection<String> listOne = new ArrayList<String>();
listOne.add("Coders");
listOne.add("Arts");
listOne.add("is");
listOne.add("a");
listOne.add("product");
listOne.add("based");
listOne.add("company");
Iterator<String> iterator= listOne.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Output:
Coders
Arts
is
a
product
based
company
Methods of Collection:
The Collection interface contains the following methods
add(E e): Ensures that this collection contains the specified element (optional operation).
addAll(Collection<? extends E> c): Adds all the elements in the specified collection to this collection (optional operation).
clear(): Removes all the elements from this collection (optional operation).
contains(Object o): Returns true if this collection contains the specified element.
containsAll(Collection<?> c): Returns true if this collection contains all the elements in the specified collection.
equals(Object o): Compares the specified object with this collection for equality.
hashCode(): Returns the hash code value for this collection.
isEmpty(): Returns true if this collection contains no elements.
iterator(): Returns an iterator over the elements in this collection.
parallelStream(): Returns a possibly parallel Stream with this collection as its source.
remove(Object o): Removes a single instance of the specified element from this collection, if it is present (optional operation).
removeAll(Collection<?> c): Removes all of this collection’s elements that are also contained in the specified collection (optional operation).
removeIf(Predicate<? super E> filter): Removes all the elements of this collection that satisfy the given predicate.
retainAll(Collection<?> c): Retains only the elements in this collection that are contained in the specified collection (optional operation).
size(): Returns the number of elements in this collection.
spliterator(): Creates a Spliterator over the elements in this collection.
stream(): Returns a sequential Stream with this collection as its source.
toArray(): Returns an array containing all the elements in this collection.
toArray(IntFunction<T[]> generator): Returns an array containing all the elements in this collection, using the provided generator function to allocate the returned array.
toArray(T[] a): Returns an array containing all the elements in this collection; the runtime type of the returned array is that of the specified array.
How does CodersArts help you in Java coding?
CodersArts provide :
Java assignment Help
Help in Java development Projects
Mentorship from Experts Live 1:1 session
Course and Project completions
CourseWork help
Comentarios