Thursday 12 February 2015

Collections

Collections:

ArrayList:
It is an ordered collection
It implements List interface
It stores objects in sequential order
It is non synchronized
It allows duplicates
we can get object using index

Vector
It is a collection in java
It implements List interface
It stores objects in sequential order
It is synchronized
It allows duplicates
we can get object using index

HashSet
It is a collection in java
It implements Set interface
It stores Unique objects(Doesn’t allow duplicate objects)
Cannot get objects using index
Doesn’t store in sequential order
Uses equals() for comparison
TreeSet
It is a collection in java
It implements Set interface
It stores Unique objects(Doesn’t allow duplicate objects)
It does sorting in Ascending order
Cannot get objects using index
Doesn’t store in sequential order
Uses compareTo() for comparison

HashMap
It is a collection in java
It implements Map interface
It stores Key and Values and it maps keys to the values
It allow only one key as a null
It is non synchronized  






HashTable
It is a collection in java
It implements Map interface
It stores Key and Values and it maps keys to the values
It doesn’t allow null as a key and value
It is synchronized

TreeMap
It is a collection in java
It implements Map interface
It stores Key and Values and it maps keys to the values
It doesn’t null as a key but allow value as a key
It does sorting automatically for the keys in ascending order


Set & List
Both are interfaces in Collection framework
Set is an unordered collection. List is an ordered collection
Set store unique objects. List stores duplicate objects
Objects cannot be retrieved using index in Set.
Objects can be retrieved using index in List.
Set implementations are HashSet & TreeSet
List implementations are ArrayList & Vector

How Set store unique objects ?
Set uses equals() to check the object is existing or not

What is diff between HashSet and TreeSet ?
Both implements Set interface
Both stores unique objects
TreeSet does sorting automatically in ascending order
HashSet used equals() for comparison
TreeSet use compareTo() for comparison




What is diff between Vector and ArrayList ?
Both implements List interface
Both are ordered collections
Both are used to get the objects using index
Vector is Sync and ArrayList is non Sync



What is diff between HashMap and HashTable ?
Both implements Map interface
Both stores keys and values and maps keys to values
Hashtable is Synchronized and HashMap is non synchronized.
Hashtable doesn’t allow null value and null key
HashMap allows only one key as null and it allows null values


Sortings
How do you sort a List ?
Collections.sort(list);

How do you sort a Map ?
By using TreeMap we can do sorting.
We need to pass Map to Treemap.putAll(). It does sorting

How do you sort Set ?
We have TreeSet it does sorting automatically

How do you sort HashSet ?
We use TreeSet for sorting. We need to add HashSet all elements to TreeSet by using TreeSet.addAll(hashset);

Iterations
How do you iterate a List ?
using EnhancedForLoop. Enhanced for loop internally uses Iterator

How do you iterate a Set ?
using EnhancedForLoop. Enhanced for loop internally uses Iterator





How do you sort a Map ?
First we need to get all the keys by using keySet() it return the Set with keys
We need iterate the Set and use get() to get the object
Set <String> keys = hm.keySet();
System.out.println("keys are "+keys);
//Iterate keys
for(String key : keys){
System.out.println("Hashmap object is "+hm.get(key));
}


How do you synchronize a ArrayList ?
Collections.synchronizeList(arrayList);

How do you synchronize a HashSet ?

Collections.synchronizeSet(hashset);

Monday 9 February 2015

Object Class

Object Class:

Object:
an object is an instance of the class

Object Class:
It is Superclass for all the classes in java
Object class methods: equals(),hashcode(),toString(),finalize(),wait(),notify(),notifyall();
New operator is used to create a object

Constructor:
Constructor is used to initialize the instance variable/global variable

Garbage Collection:
A Garbage Collection is a process to remove the objects from heap memory which doesn’t have any references

Stack Memory: Stores reference variables

Heap Memory: Stores objects

GC will call finalize() before removing the objects.
Starting the GC Process: JVM runs the Garbage collection process
System.gc();
Runtime.gc();
It doesn’t guarantee that garbage process will start
we will just tell to JVM to start the process

Hashcode:
Hashcode is integer creates by JVM for each object
== is an operator used to check the memory address of the object
equals() used to check the content of the objects

Serializable is a marker interface
Marker interface which doesn’t have any methods
what are the other markable interfaces
Seriazable, Cloneable

Static Block:
It's executed when the class is loaded
Java static blocks will be called when JVM loads the class into memory, means it will be called only once. But constructor will be called every time when you create an object.