Java Collections

Cahit Barkin Ozer
4 min readNov 15, 2021

--

In Java, a Collection (a single unit of objects) is a framework that provides an architecture for storing and manipulating a collection of objects. [1]

All data actions, including searching, sorting, insertion, manipulation, and deletion, can be accomplished with Java Collections. [1]

Many interfaces are available in the Java Collection framework. [1]

The collections in Java are represented by the UML diagram below:

Hierarchy of the Collection Framework[1]

Iterable interface

Because the Collection interface extends the Iterable interface, all of the Collection interface’s subclasses also implement the Iterable interface. The iterator() function is the single method in the iterable interface. [1]

Iterator<T> iterator()

Iterator interface

Used for traversing elements in a collection. [1]

The Iterator interface allows you to iterate elements in a forward direction only. [1]

public boolean hasNext(): Returns true if the iterator has more elements.
public Object next(): Returns the element and moves the cursor pointer to the next element.[1]

public void remove(): Removes the last elements returned by the iterator (less common).[1]

Collection Interface

Every class in the collection framework implements the Collection interface. It specifies which methods are available to each collection. [1]

The Collection interface’s methods: [1]

Boolean add ( Object obj)

Boolean addAll ( Collection c)

void clear(), etc.

All of the Collection interface’s subclasses implement these methods. [1]

List Interface

It prevents us from storing an ordered collection of things in a list-type data structure. It can have duplicate values. [1]

The list interface is implemented by the ArrayList, LinkedList, Vector, and Stack classes.[1]

ArrayList

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. [1]

Note: Synchronized access denotes the presence of a locking mechanism for data access. If you’re using a collection in several threads, ensure sure you’re accessing it in a synchronized manner, or that the collection is thread-safe, meaning it handles locking internally.[4]

It is very important because it is widely used.

import java.util.*;class TestJavaCollection1{  public static void main(String args[]){    //Creating   arraylist  
ArrayList<String> list=new ArrayList<String>();
list.add(“Ravi”);//Adding object in arraylist list.add(“Vijay”); list.add(“Ravi”); list.add(“Ajay”); //Traversing list through Iterator Iterator itr=list.iterator(); while(itr.hasNext()){
System.out.println(itr.next());}}}

LinkedList

The elements are stored in a doubly-linked list, internally. It can store duplicate elements. It does not synchronize and keeps the insertion order. The manipulation of a LinkedList is quick since no shifting is necessary.
(The code has been trimmed since the truncated sections are identical to the prior code). [1]

LinkedList<String> al=new LinkedList<String>();al.add(“Ravi”);al.add(“Vijay”);al.add(“Ravi”);al.add(“Ajay”);Iterator<String> itr=al.iterator();

Vector

The data elements are stored as a dynamic array in Vector. It’s a bit similar to ArrayList. It is, however, synchronized and contains numerous methods not found in the Collection framework.[1]

Vector<String> v=new Vector<String>();v.add(“Ayush”);

Stack

The stack is the subclass of Vector. It implements the last-in-first-out data structure.[1]

The stack contains all of the Vector class’s methods, as well as its methods such as boolean push(), boolean peek(), and boolean push(Object o), which specify the class’s properties. [1]

Stack<String> stack = new Stack<String>();stack.push(“Ayush”);stack.push(“Garvit”);stack.pop();

Queue Interface

The first-in-first-out order is maintained by the queue interface. It’s a type of ordered list that’s utilized to store the elements that are about to be processed. The Queue interface is implemented by several classes, including PriorityQueue, Deque, and ArrayDeque. [1]

PriorityQueue

The Queue interface is implemented by the PriorityQueue class. 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. [1]

PriorityQueue<String> queue=new PriorityQueue<String>();queue.add(“Amit Sharma”);System.out.println(“head:”+queue.element());System.out.println(“head:”+queue.peek());queue.remove();queue.poll();

Deque Interface

Deque interface extends the Queue interface. The term Deque refers to a double-ended queue that allows us to perform operations on both ends. [1]

Set Interface

In Java, the java.util package contains the Set Interface. It adds to the Collection interface’s functionality. It represents an unsorted collection of elements that prevents us from storing duplicates. In Set, we can only save one null value. HashSet, LinkedHashSet, and TreeSet are all implementations of Set. [1]

HashSet

The Set Interface is implemented by the HashSet class. It describes a collection that stores data in a hash table and hence contains unique entries. [1]

Java Map Interface

A map is a collection of values based on a key, i.e. a key and value pair. Each key-value pair is referred to as an entry. A map has its own set of keys. [2]

Because maps do not store data in a logical sequence, we had to convert them to sets to traverse them, but now it is generic enough that we can traverse on a map. [2]

Map<Integer,String> map=new HashMap<Integer,String>();map.put(100,”Amit”);map.put(101,”Vijay”);map.put(102,”Rahul”);//Elements can traverse in any orderfor(Map.Entry m:map.entrySet()){System.out.println(m.getKey()+” “+m.getValue());}

Java Comparator interface

A user-defined class’s objects are ordered using the Java Comparator interface. [3]

This interface has two methods and is included in java.util package.
[3]

This interface is found in java.util package and contains 2 methods.[3]

compare(Object obj1, Object obj2): Compares the first object with the second object.

equals(Object element): Compares the current object with the specified object.

Please do not forget to clap if you enjoyed my article. Thank you.

Source:

[1]javatpoint,(2021), Collections in java:

[https://www.javatpoint.com/collections-in-java]

[2] javatpoint,(2021), Java Map:

[https://www.javatpoint.com/java-map]

[3] javatpoint,(2021), Comparator interface in collection framework:

[https://www.javatpoint.com/Comparator-interface-in-collection-framework]

[4]aioobe,( Jun 21 2011), About unsynchronized & synchronized access in Java Collections Framework:

[https://stackoverflow.com/questions/6422887/about-unsynchronized-synchronized-access-in-java-collections-framework/6422946]

--

--

Cahit Barkin Ozer

Üretken Yapay Zeka başta olmak üzere teknoloji alanındaki yenilikleri öğrenip sizlerle paylaşıyorum.