Updated April 17, 2023
Introduction to Java Collection Example
In Java, a collection is a framework that provides an architecture for storing and manipulating a collection of objects. Java Collections are capable of doing any data operations such as searching, insertion, deletion, manipulation, and sorting. A single unit of objects in Java is referred to as a collection. Many interfaces like List, Queue, Dequeue, set and classes like LinkedList, ArrayList, LinkedHashSet, HashSet, TreeSet, Vector, PriorityQueue are available in the Java Collection framework. The java.util package has classes as well as interfaces for the Java Collection framework. In this topic, we are going to learn about Java Collection Example.
Java Collection Methods
Below is the list of java collection methods.
- public boolean add(E e): This method is used for inserting the elements in the collection.
- public int hashCode(): This method is used to return the hash code number of the specified collection.
- public boolean addAll(Collection<? extends E> c): This method is used to insert the elements of the specified collection into invoking collections.
- default boolean removeIf(Predicate<? super E> filter): This method is used to delete all the elements of the collection that match the conditions.
- public boolean remove(Object element): This method is used to delete the element from the collection.
- public boolean removeAll(Collection<?> c): This method is used to delete all the specified elements from the invoking collection.
- public boolean retainAll(Collection<?> c): This method is used to delete all the elements of the invoking collection except the specified collection.
- public boolean contains(Object element): This method is used for searching, the elements in the collection.
- public int size(): This method is used to count the total number of elements present in the collection.
- public void clear(): This method is used for deleting all the elements of the collection.
- public Iterator iterator(): This method is used to return the iterator.
- public boolean containsAll(Collection<?> c): This method is used to search the specified elements in the collection.
- public boolean isEmpty(): This method is used to check whether the specified collection is empty or not.
- public Object[] toArray(): This method is used to convert the collection into an array.
- public <T> T[] toArray(T[] a): This method is also used to convert the collection into array. default Stream<E> stream(): This method is used to return the sequence of streams with the collection as a source.
- default Stream<E> parallelStream(): This method is used to return the parallel stream with the collection as a source.
- public boolean equals(Object element): This method is used to match the two collections.
- default Spliterator<E> spliterator(): This method is used to generate the spliterator for the collection.
Java collection examples
Here are the examples mention below
Example #1
Program to implement ArrayList in Java
Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<String>();
list.add("eduCBA");
list.add("Java");
list.add("Collection");
list.add("Example");
list.add("On");
list.add("ArrayList");
//Traversing list through Iterator
Iterator z = list.iterator();
while(z.hasNext()){
System.out.println(z.next());
}
}
}
Output:
It stores duplicate elements of various data kinds in a dynamic array. The ArrayList class is non-synchronized and preserves the insertion order. The elements in the ArrayList class can be accessed at any time.
Example #2
Program to implement Vector in Java
Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Vector<String> c = new Vector<String>();
c.add("Java Collcetiom");
c.add("Example");
c.add("on");
c.add("Vector");
Iterator<String> z = c.iterator();
while(z.hasNext()){
System.out.println(z.next());
}
}
}
Output:
The data elements are stored as a dynamic array in Vector. It’s a lot like ArrayList. It is, however, synchronized and contains numerous methods not found in the Collection framework.
Example #3
Program to implement PriorityQueue in Java
Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
PriorityQueue<String> q = new PriorityQueue<String>();
q.add("Java Collection");
q.add("Example");
q.add("on");
q.add("PriorityQueue");
System.out.println("head:"+q.element());
System.out.println("head:"+q.peek());
System.out.println("Iteration on the elements:");
Iterator z = q.iterator();
while(z.hasNext()){
System.out.println(z.next());
}
}
}
Output:
The PriorityQueue class implements the Queue interface. It contains the items or objects that will be handled according to their priority. Null values are not allowed to be stored in the PriorityQueue queue.
Example #4
Program to implement dequeue in Java
Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Deque<String> dq = new ArrayDeque<String>();
dq.add("Example to");
dq.add("Implement");
dq.add("Dequeue");
dq.add("in Java");
for (String result : dq) {
System.out.println(result);
}
}
}
Output:
The Deque interface is a variant of the Queue interface. We can delete and add items from both sides in Deque. Deque is an abbreviation for a double-ended queue that allows us to conduct operations on both ends.
Example #5
Program to implement HashSet in Java
Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
HashSet<String> p = new HashSet<String>();
p.add("Example to");
p.add("Implement");
p.add("HashSet");
p.add("in Java");
Iterator<String> z = p.iterator();
while(z.hasNext()){
System.out.println(z.next());
}
}
}
Output:
The Set Interface is implemented by the HashSet class. It denotes a collection that stores data in a hash table. The items of the HashSet are stored using hashing. It contains one-of-a-kind things.
Conclusion
Here, in this article, we have discussed the java collection concepts. We have also implemented this concept with examples. Hope you enjoyed the article.
Recommended Articles
This is a guide to Java Collection Example. Here we discuss the implementation of Java Collection along with the examples and outputs. You may also have a look at the following articles to learn more –