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);

No comments:

Post a Comment