Tuesday, June 29, 2010
Comparable versus Comparator
Comparable and Comparator are nearly identical in purpose: They both determine if one object is greater-than, less-than, or equal to another object. This behavior creates what is known in mathematics as a "partial ordering" and these interfaces can be used to sort lists of objects. The Comparable interface is implemented by the object type itself; whereas the Comparator is an independent class that compares two objects.
Integer Objects
Java SE 5 introduced the concept of autoboxing.
Integer i1 = 1000;
Integer i2 = 1000;
assert (i1 != i2); // i1 and i2 are *not* the same object ---true
assert (i1.equals(i2)); true
Is equivalent to
Integer i1 = new Integer(1000);
Integer i2 = new Integer(1000);
assert (i1 != i2); true
assert (i1.equals(i2));true
So i1 holds an object of type Integer (as opposed to a primitive int value). When you create two *new* objects then they will always be non-identical (!=) even though the data within the objects *is* the same (equals). This is the major difference between the == operator and the equals() method; the == operator determines object identity (same object reference).
However, for numbers under a certain value (I think under 128) this equivalency is *not* true. For example...
Integer i1 = 10;
Integer i2 = 10;
assert (i1 == i2); // i1 and i2 are the same object true
assert (i1.equals(i2)); true
So, the object referred to by i1 is identical to the object referred to by i2. Why does this happen? It turns out that the Java compiler performs an optimization for "creating" Integer objects (using autoboxing) for small numeric values. The compiler has an object cache of the first 128 integer values and it will always reuse the same Integer object when autoboxing the same int value.
Integer i1 = 1000;
Integer i2 = 1000;
assert (i1 != i2); // i1 and i2 are *not* the same object ---true
assert (i1.equals(i2)); true
Is equivalent to
Integer i1 = new Integer(1000);
Integer i2 = new Integer(1000);
assert (i1 != i2); true
assert (i1.equals(i2));true
So i1 holds an object of type Integer (as opposed to a primitive int value). When you create two *new* objects then they will always be non-identical (!=) even though the data within the objects *is* the same (equals). This is the major difference between the == operator and the equals() method; the == operator determines object identity (same object reference).
However, for numbers under a certain value (I think under 128) this equivalency is *not* true. For example...
Integer i1 = 10;
Integer i2 = 10;
assert (i1 == i2); // i1 and i2 are the same object true
assert (i1.equals(i2)); true
So, the object referred to by i1 is identical to the object referred to by i2. Why does this happen? It turns out that the Java compiler performs an optimization for "creating" Integer objects (using autoboxing) for small numeric values. The compiler has an object cache of the first 128 integer values and it will always reuse the same Integer object when autoboxing the same int value.
List size versus List Capacity
The capacity is the "potential size" and size is the "actual number of elements".
Note that most List implementations will grow past the initial capacity.
Note that most List implementations will grow past the initial capacity.
Monday, June 28, 2010
The Comparable Interface
1-The Comparable Interface Is Used by the Collections.sort()method and java.utils.Arrays.sort() method to sort Lists and arrays of objects.
2-int x=thisObject.compareTo(anotherObject)
3-The sort() method used compareTo() method to determine which the List or Object Array should be sorted.
4-All element in the list must be implement the Comparable interface .
5-All element are must mutually comparable
6-It means i1.compareTo(i2) must be throw a ClassCastException for any element of i1 and i2.
7-compareTo() method return negative integer,zero or positive integer as this object is less than,equal to, or greater than the specifies object .
8-ClassCastException if the specified object's type prevents it from being compared to this object.
9-it is paramount that when you going to override equals() you must tske an argument of type Object but that when you override compareTo() you should take an argument of the type you are shorting .
Example:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ArrayListSorting implements Comparable < ArrayListSorting > {
String firstName;
String middeleName;
String lastName;
String collegeName;
Long mobileNumber;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddeleName() {
return middeleName;
}
public void setMiddeleName(String middeleName) {
this.middeleName = middeleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
public Long getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(Long mobileNumber) {
this.mobileNumber = mobileNumber;
}
public ArrayListSorting(String firstName1,String middelName1,String lastName1,String collegeName1,Long mobileNumber1) {
firstName=firstName1;
middeleName=middelName1;
lastName=lastName1;
collegeName=collegeName1;
mobileNumber=mobileNumber1;
}
public String toString(){
return firstName +" "+middeleName+" "+lastName+" "+collegeName+" "+mobileNumber;
}
public static void main(String[] args) {
List< ArrayListSorting > example=new ArrayList< ArrayListSorting >();
ArrayListSorting prateekc=new ArrayListSorting("prateek", "kumar", "shaw", "ABES", 9891445027L);
ArrayListSorting anoopc=new ArrayListSorting("anoop", "chandra", "verma", "ABES", 9891445026L);
ArrayListSorting loljeetc=new ArrayListSorting("laljeet", "", "yadav", "ABES", 9891445029L);
ArrayListSorting jamalc=new ArrayListSorting("jamal", "ur", "rahman", "ABES", 9891445030L);
ArrayListSorting hariomc=new ArrayListSorting("hari", "om", "patel", "ABES", 9891445031L);
example.add(prateekc);
example.add(anoopc);
example.add(loljeetc);
example.add(jamalc);
example.add(hariomc);
System.out.println("unsorted name "+example);
Collections.sort(example);
System.out.println("sorted name "+example);
ArrayListSorting1 example1=new ArrayListSorting1();
Collections.sort(example,example1 );
System.out.println("sorte name is "+example1);
}
public int compareTo(ArrayListSorting listsorting){
return firstName.compareTo(listsorting.getFirstName());
}
}
2-int x=thisObject.compareTo(anotherObject)
3-The sort() method used compareTo() method to determine which the List or Object Array should be sorted.
4-All element in the list must be implement the Comparable interface .
5-All element are must mutually comparable
6-It means i1.compareTo(i2) must be throw a ClassCastException for any element of i1 and i2.
7-compareTo() method return negative integer,zero or positive integer as this object is less than,equal to, or greater than the specifies object .
8-ClassCastException if the specified object's type prevents it from being compared to this object.
9-it is paramount that when you going to override equals() you must tske an argument of type Object but that when you override compareTo() you should take an argument of the type you are shorting .
Example:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ArrayListSorting implements Comparable < ArrayListSorting > {
String firstName;
String middeleName;
String lastName;
String collegeName;
Long mobileNumber;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddeleName() {
return middeleName;
}
public void setMiddeleName(String middeleName) {
this.middeleName = middeleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
public Long getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(Long mobileNumber) {
this.mobileNumber = mobileNumber;
}
public ArrayListSorting(String firstName1,String middelName1,String lastName1,String collegeName1,Long mobileNumber1) {
firstName=firstName1;
middeleName=middelName1;
lastName=lastName1;
collegeName=collegeName1;
mobileNumber=mobileNumber1;
}
public String toString(){
return firstName +" "+middeleName+" "+lastName+" "+collegeName+" "+mobileNumber;
}
public static void main(String[] args) {
List< ArrayListSorting > example=new ArrayList< ArrayListSorting >();
ArrayListSorting prateekc=new ArrayListSorting("prateek", "kumar", "shaw", "ABES", 9891445027L);
ArrayListSorting anoopc=new ArrayListSorting("anoop", "chandra", "verma", "ABES", 9891445026L);
ArrayListSorting loljeetc=new ArrayListSorting("laljeet", "", "yadav", "ABES", 9891445029L);
ArrayListSorting jamalc=new ArrayListSorting("jamal", "ur", "rahman", "ABES", 9891445030L);
ArrayListSorting hariomc=new ArrayListSorting("hari", "om", "patel", "ABES", 9891445031L);
example.add(prateekc);
example.add(anoopc);
example.add(loljeetc);
example.add(jamalc);
example.add(hariomc);
System.out.println("unsorted name "+example);
Collections.sort(example);
System.out.println("sorted name "+example);
ArrayListSorting1 example1=new ArrayListSorting1();
Collections.sort(example,example1 );
System.out.println("sorte name is "+example1);
}
public int compareTo(ArrayListSorting listsorting){
return firstName.compareTo(listsorting.getFirstName());
}
}
Saturday, June 12, 2010
Domain Class
A Domain Class is Class which presents aspects either
1-The Problem Domain Class
2-The Solution Domain Class
The Problem Domain Model: The Problem Domain Model is the set of classes attributes and relationships that describes the conceptual space of the System Under Development.
The PDM is used During Requirements Analysis to understand the types of information that the users of the sud must records or manipulate.
The solution Domain Model(SDM) :Is the set of classes ,attributes and relationship that must be coded to solve the requirements of the sud.
Typically one uses the Problem Domain Model as the starting point for the solution Domain model.
-->Domain classes Content "things I know"
-->Service classes Content "things I do"
Domain class are conceptual entities. They often related directly to things we talk about or use in the real world.
Service classes are strictly a component that exists only in the "solution" space; classes that you create in order to accomplish the goals of the SuD
1-The Problem Domain Class
2-The Solution Domain Class
The Problem Domain Model: The Problem Domain Model is the set of classes attributes and relationships that describes the conceptual space of the System Under Development.
The PDM is used During Requirements Analysis to understand the types of information that the users of the sud must records or manipulate.
The solution Domain Model(SDM) :Is the set of classes ,attributes and relationship that must be coded to solve the requirements of the sud.
Typically one uses the Problem Domain Model as the starting point for the solution Domain model.
-->Domain classes Content "things I know"
-->Service classes Content "things I do"
Domain class are conceptual entities. They often related directly to things we talk about or use in the real world.
Service classes are strictly a component that exists only in the "solution" space; classes that you create in order to accomplish the goals of the SuD
Subscribe to:
Posts (Atom)