~~ CORE-JAVA ~~

 1. INTORDUCTION

     1.1 Java Intorduction
     1.2 Java history
     1.3 Java features
     1.4 Diff b/w Java & C++
     1.5 Java Variables
     1.6 Java Data Types
     1.7 Typecasting
     1.8 Arrays

 2. OOPS

     2.1 Introduction
     2.2 Class
     2.3 Object
     2.4 Static Keywords
     2.5 Constructors
     2.6 This KeyWord

 3. INHERITANCE

     3.1 Inheritance (IS-A) 
     3.2 Aggregation (HAS-A)

 4. POLYMORPHISM

     4.1 Polymorphism & Its Type
     4.2 Method Overloading
     4.3 Method Overriding
     4.4 Super Keyword
     4.5 Final Keyword
     4.6 Dynamic Binding

 5. ABSTRACTION

     5.1 Abstract Classes & Methods
     5.2 Interfaces
     5.3 Abstract vs Interface

 6. ENCAPSULATION

     6.1 Encapsulation

 7. STRING MANIPULATIONS

     7.1 String
     7.2 String Buffer
     7.3 String Tokenizer

 8. PACKAGES

     8.1 Predefined packages
     8.2 Userdefined Packages
     8.3 Access Specifiers

 9. EXCEPTION HANDLING

     9.1 Introduction
     9.2 Pre Defined Exceptions
     9.3 Try-Catch-Finally
     9.4 Throws, throw
     9.5 User Defined Exception

 10. MULTITHREADING

     10.1 Introduction
     10.2 Thread Creations
     10.3 Thread Life Cycle
     10.4 Life Cycle Methods
     10.5 Synchronization
     10.6 Wait() notify() notify all()

 11. WRAPPERCLASSES

     11.1 Introduction
     11.2 Wrapper Classes

 12. COLLECTION FRAME WORK

     12.1 Introduction
     12.2 List interface
     12.3 Set interface                     12.4 Map interface                           

 13. INNER CLASSES

     13.1 Introduction
     13.2 Member inner class
     13.3 Static inner class
     13.4 Local inner class
     13.5 Anonymous inner class

 14. CoreJava Interview Questions

collections in java with example

Java | Collections :

Array vs Collections :

There are some limitations in Array as below :

  1. Once we declare the Array, we can’t change its size. It means we need to know it’s size in advance all the time. Which is not possible.
  2. Array can store only homogeneous types of data. Homogeneous means same type of data.

To resolve all these problems, Sun People introduced Collections.

  1. Collections are growable in nature, So based on requirement we can increase or decrease it’s size.
  2. Collections can hold homogeneous and heterogeneous both type of data.
  3. But Performance point of view it is not recommanded, Array is good as compare to Collections performance point of view.

Array vs Collections :

Collection :

  • A group of individual Objects as a single entity, is nothing but Collection.
  • We can also say Collection is an interface, which can be represent group of indivisual Object as a single entity.
  • In general Collection Interface is consider as root Interface of Collection Framework.
  • Collection Interface defines the most common methods, which are applicable for any other Collection Object, below are some of the methods :
    • boolean add(Object o)
    • boolean addAll(Collection c)
    • boolean remove(Object o)
    • boolean removeAll(Object o)
    • boolean retainAll(Object o)
    • void clear()
    • boolean isEmpty()
    • int size()
    • boolean contains(Object o)
    • boolean containsAll(Collection c)
    • Object[] toArray()
    • Iterator iterator()

Collections :

  • Collections is an utility class, under java.util package. Which helps us to define several utility methods like searching, sorting, etc.

Collection Framework :

  • We can say Collection Framework is an group of several Classes & Interfaces, which can be used to represent a group of Objects as a single entity.

 

Hierarchy of Collection Framework :

collections in java with example

List Interface :

  • List Interface is the child interface of the Collection.
  • If you want to represent group of Indivisiul object in a single entity where duplicate objects are allowed and also insertion order is preserved, then we should go for List Interface.
  • Here insertion order will preserve means by its index order.
  • Below are some important methods, which is available in List :
    • boolean add(int index, Object o)
    •  boolean addAll(int index, Collection c)
    • Object remove(int index)
    • Object get(int index)
    • Object set(int index, Object new)
    • int indexOf(Object o)
    • int lastIndexOf(Object o)
    • ListIterator listIterator()

Classes of List Interface :

  1. ArrayList
  2. LinkedList
  3. Vector
  4. Stack

ArrayList :

  • When we want to represent group of indivisual object as single entity, we can go for ArrayList, also it will behaive as below :
    • ArrayList are growable in nature.
    • Duplicate objects are allowed.
    • Insertion Order is preserved.
    • ArrayList will allow Heterogeneous objects.
    • null insertion is also possible here.
    • Best suitable if we are doing frequently retrieve operation in middle.
    • Wrost choice if we are doing frequently insertion or deletion operation in the middle.
  • ArrayList specific methods are :
    1. kjkjkj
    2. kjkjk

Example :

LinkedList :

  • When we want to represent group of indivisual object as single entity, we can go for LinkedList, also it will behaive as below :
    • Duplicate objects are allowed.
    • Insertion Order is preserved.
    • Heterogeneous Objects are allowed.
    • null insertion is also possible.
    • Best suitable if we are doing frequently insertion or deletion operation in the middle.
    • Wrost choice if we are doing frequently retrieve operation in middle.
  • LinkList specific methods are :
    1. sds
    2. sdd

Example :

Vector : 

  • For Vector, we can say below points :
    • Vectors are also growable in nature.
    • Duplicate objects are allowed.
    • Insertion order is preserved.
    • Heterogeneous Objects are also allowed.
    • null insertion is also possible.
    • Best suitable if we are doing frequently retrieve operation in middle.
    • Wrost choice if we are doing frequently insertion or deletion operation in the middle.
    • Every method in Vector is Synechronized.
    • Vector Objects are thread safe also.
  • Vector Specific methods are :
    1. sdssd
    2. sdssd

Example :

Difference between ArrayList and Vector :

Stack :

  • Stack works on LIFO (Last In First Out) concept.
  • Stack is child class of Vector, which contains only one constructor as below :
    1. Stack s = new Stack()
  • Stack specific methods are :
    1. dfdd
    2. fdfdfd

Example :

 

Queue Interface :

  • Queue is the child Interface of the Collection.
  • When we want to represent goup of object in a single entity, where FIFO (First In First Out) should be follow, then we should go for Queue Interface. But based on requirement we can change it’s order.
  • From 1.5 version LinkedList also implements Queue Interface.
  • LinkedList based implementation of Queue will always follow FIFO algorithm.
  • Below are some important methods, which are available in Queue :
    • boolean offer(Object obj)
    • Object peek()
    • Object element()
    • Object pull()
    • Object remove()

 

Classes/Interface of Queue Interface :

  1. PriorityQueue
  2. Dequeue
  3. Array Deque

 

PriorityQueue :

  • PriorityQueue is child class of Queue Interface.
  • This is the data structure to hold group of indivisual object which will process, based on some priority.
  • The priority can be either default sorting order or customized sorting order.
    • If we are depending on Default sorting order then in that case compulsory object should follow Homogeneous structure, otherwise we will get “ClassCast Exception”.
    • If we are defining our own customized sorting order then the object need not be Homogeneous.
  • In Priority Queue, duplicate objects are not allowed.
  • Insertion Order is not preserve.
  • null insertion is not possible even as first element also.

Example :

 

DeQueue :

  • It is child interface of Queue Interface.
  • Dequeue is also known as “double ended queue”.
  • In Dequeue we can insert or remove element from both side.

Example :

 

Array DeQueue :

  • Array DeQueue is child class of DeQueue Interface.
  • It provides the facility of using queue and resizable array.
  • We can add or remove elements from both side.
  • null elements are not allowed.
  • Array DeQueue is not thread safe.
  • It is faster than LinkedList and Stack.
  • It extends AbstractCollection and implements Dequeue interface.

Example :

 

Set Interface :

  • Set is the child interface of Collection.
  • If we want to represent group of object where duplicates are not allowed and insertion order is not preserved, then we should go for Set Interface.
  • Set Interface doesn’t contain any method we have to use only Collection Interface methods. 

Classes/Interface of Set Interface :

  1. HashSet
  2. LinkedHashSet
  3. SortedSet
  4. TreeSet

 

HashSet :

  • HashSet is child class of Set Interface.
  • It uses hash table for storage.
  • Duplicate objects are not allowed here.
    • If we are trying to add duplicate method, then it will not throw any compile or run time error, simply add() method will return false. 
  • Insertion order is not preserved.
  • Heterogeneous objects are allowed.
  • null insertion is possible, but only once because it doesn’t allow duplicate.
  • HashSet implements Serializable and Clonable Interface.
  • Some important methods of HashSet are below :
    • boolean add(E e)
    • void clear()
    • object clone()
    • boolean contains(Object o)
    • boolean isEmpty()
    • Iterator<E> iterator()
    • boolean remove(Object o)
    • int size()
    • Spliterator<E> spliterator()

Example :

 

Linked HashSet :

  • LinkedHashSet is child class of Set Interface.
  • Duplicate objects are not allowed here.
  • It will maintain insertion order also.

Example :

 

Sorted Set :

  • SortedSet Interface is child interface of Set Interface.

Example :

 

Tree Set :

  • Tree Set Class is child class of SortedSet Interface.
  • The object of the TreeSet class are stored in ascending order.
  • Duplicate elements are not allowed.
  • null element are not allowed.
  • Accessing and retrieval time is very fast.
  • It maintains ascending order.
  • Some important methods of Tree Set are below :
    • boolean add(E e)
    • boolean addAll(Collection<? extends E> c)
    • E ceiling(E e)
    • Comparator<? super E> comparator()
    • Iterator descendingIterator()
    • NavigableSet descendingSet()
    • E floor(E e)
    • SortedSet headSet(E toElement)
    • NavigableSet headSet(E toElement, boolean inclusive)
    • E higher(E e)
    • Iterator iterator()
    • E lower(E e)
    • E pollFirst()
    • E pollLast()
    • Spliterator spliterator()
    • NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
    • SortedSet subSet(E fromElement, E toElement))
    • SortedSet tailSet(E fromElement)
    • boolean contains(Object o)
    • boolean isEmpty()
    • boolean remove(Object o)
    • void clear()
    • Object clone()
    • E first()
    • E last()
    • int size()

Example :

Help Others, Please Share