Jump to content

Java/j2Ee Discussions


lolliman

Recommended Posts

[quote name='Java01' timestamp='1372794104' post='1303912292']
Comparator -- mayaaa nuvvu neetho inko edavani ppolchavu anukoo adi comparator

Comparable -- nuvvu ee DB lo ni idharu neeku istam aina vedavalani okaritho okarini polchavu anukooo adi comparable
[/quote]

ROFL Comment assal....

Here is the dipperence in java language...

Comparable: The “Comparable” allows itself to compare with another similar object (i.e. A class that implements Comparable becomes an object to be compared with). The method compareTo() is specified in the interface.

Comparatot: The Comparator is used to compare two different objects. The following method is specified in the Comparator interface. public int compare(Object o1, Object o2)

Link to comment
Share on other sites

[quote name='cherlapalli_jailer' timestamp='1372788110' post='1303911744']
@sinni i modifed above and the working perfectly one is there...so pl try to use the above code now
[/quote]


Working mama... Thanks a lot....

Link to comment
Share on other sites

[quote name='Sinnababu_SillyPulka' timestamp='1372797652' post='1303912589']

ROFL Comment assal....

Here is the dipperence in java language...

Comparable: The “Comparable” allows itself to compare with another similar object (i.e. A class that implements Comparable becomes an object to be compared with). The method compareTo() is specified in the interface.

Comparatot: The Comparator is used to compare two different objects. The following method is specified in the Comparator interface. public int compare(Object o1, Object o2)
[/quote]
[quote name='Java01' timestamp='1372794104' post='1303912292']
Comparator -- mayaaa nuvvu neetho inko edavani ppolchavu anukoo adi comparator

Comparable -- nuvvu ee DB lo ni idharu neeku istam aina vedavalani okaritho okarini polchavu anukooo adi comparable
[/quote]exzaamplee kavaliii similar kind of objects ante?

Link to comment
Share on other sites

[quote name='HAPPYNESS' timestamp='1372797758' post='1303912601']
exzaamplee kavaliii similar kind of objects ante?
[/quote]

Comparable:
Many of the standard classes in the Java library like String, Integer, Date, File etc implement the Comparable interface to give the class a "Natural Ordering".

Comparator:
You can have more control by writing your Comparator class. For most cases natural ordering is fine as shown on the left but say we require a special scenario where we need to first sort by the id and then by Type. (Id and Type are attributes of a Class) We can achieve this by writing a “Comparator” class.

Example Class for Comparable:

public class Pet implements Comparable {
int petId;
String petType;
public Pet(int argPetId, String argPetType) {
petId = argPetId;
this.petType = argPetType;
}
public int compareTo(Object o) {
Pet petAnother = (Pet)o;
//[b]natural alphabetical ordering by type[/b]
//if equal returns 0, if greater returns +ve int,
//if less returns -ve int
return [b]this.petType.compareTo(petAnother.petType);[/b]
}
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Pet(2, "Dog"));
list.add(new Pet(1, "Parrot"));
list.add(new Pet(2, "Cat"));
Collections.sort(list); // sorts using compareTo method
for (Iterator iter = list.iterator(); iter.hasNext();) {
Pet element = (Pet) iter.next();
System.out.println(element);
}
}
public String toString() {
return petType;
}
}

If you want to sort first by Id and then Type, use Comparator.

Link to comment
Share on other sites

[quote name='HAPPYNESS' timestamp='1372797758' post='1303912601']
exzaamplee kavaliii similar kind of objects ante?
[/quote]

comparator example
class1:
public class People{
private String fn;
private String ln;
public People(String fn , String ln){
this.fn=fn;
this.ln=ln;
}
public String getFn(){
return fn;
}
public String getLn(){
return ln;
}

}


class2:
public class PeopleComparator implements Comparator<People>{


public int compare(People o1, People o2) {

return 0;
}

}

class3:

import java.util.*;
public class PeopleTest {

public static void main(String[] args) {
try
{

People p1 = new People("aj", "Jr");
People p2 = new People("aj", "Sr");
People p3 = new People("aj","Jhon");
Set<People> people = new TreeSet<People>();
people.add(p1);
people.add(p2);
people.add(p3);
Iterator<People> itr = people.iterator();
while(itr.hasNext()){
People p = itr.next();
System.out.println(p.getLn());
}
}
finally{

}

}
}

Link to comment
Share on other sites

think of shopping carts....or job searches setc....

and appy comparators to that ...u wiill get better picture

like take for example job search....try to compare the similar job like ...techs...etc..

Link to comment
Share on other sites

[quote name='Chinni_' timestamp='1372798958' post='1303912738']
what happens if u add null values to

1.array list
2.HashMap
[/quote]
emi avvadu .....Hash map memory ekkuva avtundi ..Array list fixed

Link to comment
Share on other sites

[quote name='HAPPYNESS' timestamp='1372799761' post='1303912801']
emi avvadu .....Hash map memory ekkuva avtundi ..[color=#ff0000][size=5]Array list fixed[/size][/color]
[/quote]
:surprised-038:[size=4] arrays are fixed not array list..[/size]

[size=4]array list and vectors grow in size [/size]dynamically[size=4] [/size][size=4] .. sSa_j@il[/size]

Link to comment
Share on other sites

[quote name='HAPPYNESS' timestamp='1372799761' post='1303912801']
emi avvadu .....Hash map memory ekkuva avtundi ..Array list fixed
[/quote]
neeku java kuda ochu endi [img]http://4.bp.blogspot.com/-lBbeM9C_140/Uc3C9Vjh3fI/AAAAAAAAH-s/F1dYmcvzYGk/s137/brahmam_fanski.gif[/img]

Link to comment
Share on other sites

[quote name='CNATION1' timestamp='1372800283' post='1303912816']
neeku java kuda ochu endi [img]http://4.bp.blogspot.com/-lBbeM9C_140/Uc3C9Vjh3fI/AAAAAAAAH-s/F1dYmcvzYGk/s137/brahmam_fanski.gif[/img]
[/quote]
[img]http://www.codeproject.com/KB/ajax/AjaxTutorial/AJAX2.jpg[/img]

Link to comment
Share on other sites

ooree nee eshaloooo ....... eee bomma lu endhi ... eee questions endhi ..... deshani brashtu patisthunavu kadhraa nayanaaa.....

[quote name='Chinni_' timestamp='1372800384' post='1303912822']
[img]http://www.codeproject.com/KB/ajax/AjaxTutorial/AJAX2.jpg[/img]
[/quote]

Link to comment
Share on other sites

[quote name='Java01' timestamp='1372801547' post='1303912875']
ooree nee eshaloooo ....... eee bomma lu endhi ... eee questions endhi ..... deshani brashtu patisthunavu kadhraa nayanaaa.....
[/quote]
CITI_c$y CITI_c$y CITI_c$y

Link to comment
Share on other sites

×
×
  • Create New...