Hi guys this post is specifically to give some introduction about collecton framework in java.
Collection : Is an Interface in java and
Collections : Is a class
toString(), equal() and hashCode() method introduction.
1.toString() : This method you can use if you want to read some meangifull text or data from your class.Overriding toString method ex.
public String toString() {
return ("I am a Bob, but you can call me " + nickName + ". My shoe size is " + shoeSize);
}
2. "==" in java compares only the references to the object. But if you want to comapre the objects itselt then you need to use the equals() method.
3.equals() : use this method if you want to compare two objects of a class.
ex.
public boolean equals(Object o) {
if ((o instanceof Moof) && (((Moof)o).getMoofValue() == this.moofValue))
{
return true;
} else{
return false;
}
}
4. hashCode() : is used with equals method.If you want to get some unique code from your class then override this method.
ex.
public int hashCode() {
return (x *100);
}
This is used to increase the applicaton specifically collectuion performance.If two objects are equal by equals() method then they should be equals by hashCode() also.But viceversa may or may ot be true.
5.Collection framework in java.
Core interfaces in collection framework are as below.
Some of the usefull implementaion classes and their respective interfaces are
In Following tree you can see the implements and extends relationship.
You can divide Collections in four basic flavors:
■ Lists -> Lists of things (classes that implement List).
■ Sets -> Unique things (classes that implement Set).
■ Maps -> Things with a unique ID (classes that implement Map).
■ Queues -> Things arranged by the order in which they are to be processed.
List Interface :
ArrayList :
- Gives you fast iteration and fast random access.
- It is ordered but not sorted.
- Implements RandomAccess interface(interface without methods.helps in faster access).
Vector :
- Same as ArrayList but methods are Synchronized(Thread safe).
Set Interface :
It doesn't allow duplicates.
HashSet :
Use this class when you want a collection with no duplicate values and you don't care about order.
LinkedHashSet :
- It is ordered version of HashSet.
- When you need iteration order use this class instead of HashSet.
- It will iterate in the same order in which we inserted the elements.But in HashSet iteration will be unpredictable.
TreeSet:
- TreeSet and TreeMap are the only two sorted collections.
Map Interface :
It cares about unique identifiers.uses the key/value pair to store the values.It uses equals() method to check the equality of objects.
HashMap :
- When you need a map and don't care about orders then use this collection.
- It allows one null key and multiple null values.
Hashtable :
- It is same as vector. It is synchronized version of HashMap.
- Doesn't allow anything to be null in this class.
LinkedHashMap :
- It maintains insertion order like LinkedHashSet.
- You can expect faster iteration with this.
TreeMap :
- It is sorted Map.
Collection : Is an Interface in java and
Collections : Is a class
toString(), equal() and hashCode() method introduction.
1.toString() : This method you can use if you want to read some meangifull text or data from your class.Overriding toString method ex.
public String toString() {
return ("I am a Bob, but you can call me " + nickName + ". My shoe size is " + shoeSize);
}
2. "==" in java compares only the references to the object. But if you want to comapre the objects itselt then you need to use the equals() method.
3.equals() : use this method if you want to compare two objects of a class.
ex.
public boolean equals(Object o) {
if ((o instanceof Moof) && (((Moof)o).getMoofValue() == this.moofValue))
{
return true;
} else{
return false;
}
}
4. hashCode() : is used with equals method.If you want to get some unique code from your class then override this method.
ex.
public int hashCode() {
return (x *100);
}
This is used to increase the applicaton specifically collectuion performance.If two objects are equal by equals() method then they should be equals by hashCode() also.But viceversa may or may ot be true.
5.Collection framework in java.
Core interfaces in collection framework are as below.
Some of the usefull implementaion classes and their respective interfaces are
In Following tree you can see the implements and extends relationship.
You can divide Collections in four basic flavors:
■ Lists -> Lists of things (classes that implement List).
■ Sets -> Unique things (classes that implement Set).
■ Maps -> Things with a unique ID (classes that implement Map).
■ Queues -> Things arranged by the order in which they are to be processed.
List Interface :
ArrayList :
- Gives you fast iteration and fast random access.
- It is ordered but not sorted.
- Implements RandomAccess interface(interface without methods.helps in faster access).
Vector :
- Same as ArrayList but methods are Synchronized(Thread safe).
- Synchronization add a performance hit for this class.
- It also implements RandomAccess interface.
LinkedList :
- Elements are doubly linked to one another.
- Use this if you need faster iteration and deletion.It may iterates more slowely than ArrayList.- It also implements RandomAccess interface.
LinkedList :
- Elements are doubly linked to one another.
Set Interface :
It doesn't allow duplicates.
HashSet :
Use this class when you want a collection with no duplicate values and you don't care about order.
LinkedHashSet :
- It is ordered version of HashSet.
- When you need iteration order use this class instead of HashSet.
- It will iterate in the same order in which we inserted the elements.But in HashSet iteration will be unpredictable.
TreeSet:
- TreeSet and TreeMap are the only two sorted collections.
Map Interface :
It cares about unique identifiers.uses the key/value pair to store the values.It uses equals() method to check the equality of objects.
HashMap :
- When you need a map and don't care about orders then use this collection.
- It allows one null key and multiple null values.
Hashtable :
- It is same as vector. It is synchronized version of HashMap.
- Doesn't allow anything to be null in this class.
LinkedHashMap :
- It maintains insertion order like LinkedHashSet.
- You can expect faster iteration with this.
TreeMap :
- It is sorted Map.
Comments
Post a Comment