preloader

Collection is combination of different type of objects in a single entity.

Collection

  • COURSES

    04 Month

  • DURATION

    01 Hours

Collection is combination of different type of objects in a single entity.
Ex- group of different type of book in library

Difference between Collection and Collections in Java?

Collection Collections
Collection is an interface Collections is utility class
public interface Collection extends Iterable public class Collections extends Object
The important methods of Collection interface are
  • add()
  • remove()
  • size()
  • clear()
Collections class contains only static methods like
  • sort()
  • min()
  • max()
  • fill()
  • copy()
  • reverse()
It is used to store list of object in a single object It is used to operate on collection.

Difference between List and Set?


List Set
List is child interface of collection interface Set is also a child interface of Collection interface
List allows duplicate Set does not allow duplicate
List allows multiple null values Set allow only one null value
List maintain the insertion order Set does not maintain the insertion order

Difference between Array and ArrayList?


Array ArrayList
Array is fixed in size ArrayList is growable by nature.
Array support only homogenous data ArrayList support both homogenous
and heterogenous data
Array is not good for memory management ArrayList save memory
Array is faster in execution ArrayList is slower in execution

what are generics in java?

Generics is used to convert heterogenous datatype to homogenous datatype. generics force the java programmer to store a specific type of objects.
Generics is a part of collection interface, it used to specify the type of data either string or integer and specific wrapper class is used for it

Difference between ArrayList and LinkedList?


ArrayList LinkedList
ArrayList is implementation of dynamic array LinkedList is an implementation of doubly LinkedList
ArrayList is list based structure. LinkedList is node-based structure
ArrayList initial capacity is 10 Linked List Constructs empty list
ArrayList increases its size by 50% LinkedList increases its size by doubling its size
ArrayList is good for data retrieval i.e. insertion and deletion LinkedList is good for data manipulation such as sorting and searching
ArrayList provides random access of data LinkedList does not allowed random access of data
ArrayList is slower LinkedList is faster in execution

Difference Between ArrayList and Vector?


ArrayList Vector
part of JDK 1.2 part of 1.0 JDK
ArrayList does not belongs to Legacy Vector belongs to Legacy classes
ArrayList is not synchronized and non threadsafe i.e. not good for threaded application vector is threadsafe and Synchronized i.e. good for threaded application
ArrayList increases its size by 50% Vector increases its size by 100%

Difference between HashSet and Linked HashSet?


HashSet Linked HashSet
HashSet belongs to 1.2 JDK Linked HashSet belongs to 1.4 JDK
HashSet require less memory It requires more memory than HashSet.
It provides slightly faster performance than LinkedHashSet It provides low performance than HashSet
HashSet does not maintain insertion order Linked HashSet maintain insertion order

Difference between HashSet and TreeSet?


HashSet TreeSet
HashSet is implementation of HashTable TreeSet is implementation of Tree Structure
HashSet has time complexity of O(n) TreeSet has time complexity of O(log n)
HashSet does not maintain any order TreeSet maintain ascending order
HashSet is faster in execution TreeSet is slower in execution
HashSet use hashcode and equal method TreeSet use compare and CompareTo() method

HashMap

HashMap is part of map interface ,follow key and value pair

How HashMap works internally?

HashMap works internally on hashing principle. which internally generates the hashcode
.when put method is called it will check the value of key if key value is null
it will store at zero index. After that index value is calculated for hashcode
by index= hashcode(key)& (n-1) formula. Once the node value is calculated it followed LinkedList structure
which compares the hashcode value and equal method check the key value if hashcode is same but different key.
it will store in same bucket with same index value but different value,
but in the case of collision if both values are same for key
then value will be overridden by new value itself.

Difference between HashMap and HashTable?


HashTable HashMap
Implementation of dictionary classes Implementation of linked HashMap
HashTable is synchronized and threadsafe HashMap is non synchronized and non threadsafe
Slow in the execution Faster in the execution
HashTable does not allow null value for key HashMap allow one null key

Difference between HashMap and Linked HashMap?


HashMap Linked HashMap
HashMap is part of jdk 1.2 Linked HashMap is part of jdk 1.4
HashMap is implementation of map interface Linked HashMap is an implementation of HashMap
HashMap is allowed single null key Linked HashMap does not allow null key
HashMap is slower in terms of execution Linked HashMap is faster in execution

Difference between HashMap and Concurrent HashMap?


HashMap Concurrent HashMap
HashMap is part of jdk 1.2 Concurrent HashMap is part of jdk 1.5
Non synchronized and threadsafe It is thread safe and synchronized
Not good for threaded application Good for threaded application
Allow one null key Does not allow null key value
Faster wrt concurrent HashMap Slower in terms of execution

Difference between HashMap and TreeMap?


HashMap TreeMap
HashMap follow map structure TreeMap follow tree structure
HashMap is unordered structure TreeMap follow ascending order
HashMap have time complexity of O(n) TreeMap have a complexity of O(log n)
Hash map allow one null key TreeMap does not allow null key
HashMap is non synchronized and non threadsafe TreeMap is synchronized and threadsafe
HashMap is faster in execution TreeMap is slower in execution

Difference between Comparable and Comparator?


Comparable Comparator
Compare the value in default shorting order Compare the value in customized manner
Part of java.lang package Part of java.util package
Compare and equal are part of it compareTo method is used
String and wrapper class using it used by collectors or rule-based collectors.

Difference between Iterator and List Iterator?


Iterator List Iterator
Allowed implementation in forward direction
or Unidirectional
Allowed implementation in both direction
i.e. forward and backward or bidirectional.
Used for list, set and Map Used for list only
Single side implements at one time both side implementation possible at a same time

Difference between Queue and Stack?


Queue Stack
Queues are based on the FIFO principle Stacks are based on the LIFO principle
Insertion and deletion in queues takes place
from the opposite ends of the list. The insertion
takes place at the rear of the list and the deletion
takes place from the front of the list.
Insertion and deletion in stacks takes place only
from one end of the list called the top.
Insert operation is called enqueue operation Insert operation is called push operation.
Delete operation is called dequeue operation. Delete operation is called pop operation.
Queue is used in solving problems
having sequential processing
Stack is used in solving problems
works on recursion.

Related Course

course thumb

Basic Java

All basic level question for java covered in this section

Visit
course thumb

OOP Concept

Inheritance,Polimorphism,Abstraction and Encapsulation

Visit
course thumb

Exception handling

Exception handling ,it type ,error vs Exception

Visit