Collections in Java

Collections of double roti as a Single Unit

Hierarchy of Collection Framework

Iterable Interface

Collections Interface

List Interface

List<data-type> arr = new ArrayList<data-type>();
List<data-type> arr = new LinkedList<data-type>();
List<data-type> arr = new Vector<data-type>();
List<data-type> arr = new Stack<data-type>();

ArrayList

  • Java ArrayList class can contain duplicate elements.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList allows random access because the array works on an index basis.
import java.io.*;import java.util.*;class Add{   public static void main(String[] args){       List<Integer> arr = new ArrayList<Integer>();       for (int i = 1; i <= 5; i++){

arr.add(i);
}
System.out.println(arr);
}
}

LinkedList

  • Java LinkedList class maintains insertion order.
  • Java LinkedList class can contain duplicate elements.
  • In the Java LinkedList class, manipulation is fast because no shifting needs to occur.
  • Java LinkedList class can be used as a list, stack or queue.
  1. addFirst(object):- It is used to insert the given element at the beginning of a list.
  2. addLast(object):- It is used to insert the given element at the Last of a list.
  3. getFirst():- It is used to return the first element in a list.
  4. getLast():- It is used to return the last element in a list.
  5. peekFirst():- It retrieves the first element of a list or returns null if a list is empty.
  6. peekLast():- It retrieves the last element of a list or returns null if a list is empty.
import java.io.*;import java.util.*;class Add{public static void main(String[] args){List<Integer> arr = new LinkedList<Integer>();for (int i = 1; i <= 5; i++){

arr.add(i);
}
System.out.println(arr);
}
}

Vector

  1. It is synchronized.
  2. It maintains insertion order.
  3. It contains duplicate values.
  1. Vector increases 100% of its size while ArrayList increases 50% of its size.
  2. Vector is synchronized while ArrayList not synchronized.
  1. capacity():- It is used to get the current capacity of this vector.
  2. isEmpty():- It is used to check if this vector has no components.
  3. add():- It is used to append the specified element in the given vector.
  4. clone():- It returns a clone of this vector.
  5. clear():- It is used to delete all of the elements from this vector.

Stack

  1. Top:- It represents or gives the top element index. If the stack is empty its value is (-1). If the stack is full its value is (n-1).
  2. Push:- It appends the element in the stack and increases the value of the top by 1.
  3. Pop:- It removes that element whose top pointed that index number and decrease the value of top by 1.
  1. push(object):- The method pushes (insert) an element onto the top of the stack.
  2. pop():- The method removes an element from the top of the stack and returns the same element as the value of that function.
  3. peek():- The method looks at the top element of the stack without removing it.
  4. empty():- The method checks the stack is empty or not.

Set Interface

  1. HashSet
  2. LinkedSet
  3. TreeSet
  1. It represents the unordered set of elements.
  2. It does not allow duplicate elements in the set.
  3. It uses the hash function internally.
  4. For Searching an element in the set its take only O(1) time complexity.
Set<data-type> s1 = new HashSet<data-type>();Set<data-type> s2 = new LinkedHashSet<data-type>();Set<data-type> s3 = new TreeSet<data-type>();

HashSet

  1. It represents the unordered set of elements.
  2. It does not allow duplicate elements in the set.
  3. HashSet class is non synchronized.
  4. HashSet allows null value.
import java.util.*;public class HashSetDemo {public static void main(String args[])HashSet<String> hs = new HashSet<String>();hs.add("India");hs.add("is");hs.add("great");hs.add("country");hs.add("in the world");Iterator<String> itr = hs.iterator();while (itr.hasNext()) {System.out.println(itr.next());}}}
in the world
India
great
is

LinkedHashSet

  1. LinkedHashSet maintains the insertion order.
  2. LinkedHashSet only contains unique elements just like a HashSet.
  3. LinkedHashSet class is not synchronized.

TreeSet

  1. It contains only a unique element in the TreeSet.
  2. It maintains the ascending order of elements.
  3. It is not synchronized.

Queue Interface

  1. PriorityQueue
  2. ArrayDeque
  1. add(object):- It is used to insert the specified element into this queue and return true upon success.
  2. remove():- It is used to retrieves and removes the head of this queue.
  3. poll():- It is used to retrieves and removes the head of this queue, or returns null if this queue is empty.
  4. element():- It is used to retrieves, but does not remove, the head of this queue.
  5. peek():- It is used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
Queue<data-type> s1 = new PriorityQueue<data-type>();Queue<data-type> s2 = new ArrayDeque<data-type>();

PriorityQueue

ArrayDeque

Map Interface

  1. HashMap
  2. TreeMap
Map<data-type, data-type> hm = new HashMap<data-type, data-type> ();
Map<data-type, data-type> tm = new TreeMap<data-type, data-type> ();

HashMap

  1. It does not maintain order.
  2. It stores the value in key and value mannner
  3. It does not allow duplicate keys but duplicate values are allowed.
  4. It is not synchronized.
  1. put(Object key, Object value):- It is used to insert an entry in the map.
  2. Object clone():- It is used to return a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
  3. remove(Object key):- It is used to delete an entry for the specified key.
  4. size():- This method returns the number of entries in the map.
import java.util.*;public class HashMapDemo {public static void main(String args[]){HashMap<Integer, String> hm = new HashMap<Integer, String>();hm.put(1, "India");hm.put(2, "is");hm.put(3, "country");System.out.println("Value for 1 is " + hm.get(1));for (Map.Entry<Integer, String> e : hm.entrySet())System.out.println(e.getKey() + " " + e.getValue());}}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Analyze Data with Aerospike and Starburst anywhere

Top 8 Material Admin Dashboards

Learning Wikibase: Boost Your Project with Linked data

Projects are not to-do lists

Gitlab CI + Bitrise CLI: Lower cost, more flexibility

Bonny: Extending Kubernetes with Elixir — Part 2

Contentos Mainnet 2.0: NFT and DAO Function Launch

Optimizing Disjoint Set

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Achalgupta

Achalgupta

More from Medium

Object-Oriented Programming in Java

Introduction to Java

Java Coding Standards

Java Core Concepts (JDK, JRE, JVM)