We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

Collections in Java

Collections in Java

Collection Framework

  • Collection could be a framework that gives the simplest way to store and manipulate the cluster of objects.
  • Java provides assortment Framework that defines many categories and interfaces to represent a gaggle of objects as one unit.
  • The assortment interface (java.util.Collection) and Map interface (java.util.Map) square measure the 2 most vital interfaces to carry cluster of objects.

Collection Framework

Why not Array?

  • Fixed in Size
  • Fixed type(Homogeneous)

So we need something we should be fixed in size and should hold heterogeneous data.

That’s where collection framework comes into picture.

There are many classes and interfaces are given by collection framework to hold group of objects.

List Interface

  • List interface is that the sub interface of assortment interface.
  • List contains an inventory kind organisation within which we will store the ordered assortment of objects.
  • List will have duplicate values.
  • List interface is enforced by the categories ArrayList, LinkedList, Vector, and Stack.
  • To instantiate the List interface, we tend to should use :
                            List  l1= new ArrayList();
                            List  l2 = new LinkedList();
  • There area unit numerous ways in List interface which will be wont to insert, delete and access the weather from the list.

Example:

List Interface

ArrayList is better for storing and accessing data and LinkedList is better for manipulation of data.

Click Here-> Get Prepared for Java Interviews

Set Interface

Set represents the unordered set of elements which doesn’t allow us to store the duplicate items.

Syntax:

Set<data-type> s1 = new HashSet<data-type>();  

Example:

Set Interface
list can hold duplicate elements whereas Set holds unique elements only.

Map Interface

  • A Map contains solely distinctive keys.
  • A Map does not permit duplicate keys, however you’ll be able to have duplicate values.
  • HashMap and LinkedHashMap permit null keys and values, however TreeMap does not permit any null key or worth.
  • A Map cannot be traversed, therefore you would like to convert it into Set victimization keySet() or entrySet() methodology.
  • Map interface implementation is given by HashMap,LinkedHashMap,TreeMap.
Class Description
HashMap HashMap is that the implementation of Map, however it does not maintain any order.
LinkedHashMap LinkedHashMap is that the implementation of Map. It inherits HashMap category. It maintains insertion order.
TreeMap TreeMap is that the implementation of Map and SortedMap. It maintains ascending order.

HashMap Example:

Hashmap Example

TreeMap Example:

Treemap Example

 HashMap Vs TreeMap


HashMap TreeMap
1) HashMap can contain one null key. TreeMap cannot contain any null key.
2) HashMap maintains no order. TreeMap maintains ascending order.

Click Here-> Get Java Training with Real-time Projects

Besant Technologies WhatsApp