This site "java learning concept" is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.Our coding lecture are just fun and hope you will enjoy.

Sunday, 14 October 2018

Sorting in java

Sorting in java By using collection frame work:

In java ,we can perform sorting operations by using comparable and Comparator interface.
Comparable interface denotes default natural sorting order and comparator interface  denotes customize sorting order or our own sorting order.

In  general, for String default natural sorting order is alphabetical order and for numbers default natural sorting is ascending order.In default natural sorting order our objects are should be homogeneous and comparable.other wise we will get classCalstException.
Example: Let me show the default natural sorting  by Strings in java.

import java.util.*;
class  DefaultNaturalSorting
{
public static void main(String[] args) 
{
TreeSet t=new TreeSet();
t.add("A");
t.add("H");
t.add("X");
t.add("P");
t.add("F");
t.add("U");
t.add("Z");
System.out.println(t);
}
}


Output:

[A, F, H, P, U, X, Z]

This rule is applicable only for homogeneous data elements and comparable data elements other wise we will get Run time Exception ,ClassCastException.

import java.util.*;
class  DefaultNaturalSorting
{
public static void main(String[] args) 
{
TreeSet t=new TreeSet();
t.add(10);
t.add("H");
t.add("X");
t.add("P");
t.add("F");
t.add("U");
System.out.println(t);
}
}


StringBuffer Class does not implement comparable interface so that String Buffer objects are not comparable so default sorting is not available for StringBuffer.If we are add StringBuffer objects then we will get ClasscastException.


import java.util.*;
class  DefaultNaturalSorting
{
public static void main(String[] args) 
{
TreeSet t=new TreeSet();
t.add(new StringBuffer("A"));
t.add(new StringBuffer("H"));
t.add(new StringBuffer("X"));
t.add(new StringBuffer("P"));
t.add(new StringBuffer("F"));
t.add(new StringBuffer("U"));
System.out.println(t);
}
}

output:
Exception in the thread "main" java.lang.ClassCastException: java.lang.StringBuffer can not be convert to java.lang.Comparable 

Comparable(I):

Comparable is an Interface which contains only one method is compareTo() method which ca take arguments and return +ve,-ve and 0;

Syntax:public int  compareTo(Object obj1,Object obj2)


if obj1 has to come before obj2 then it returns -ve, if obj1 has  to come after obj2 then it returns +ve .and obj1 and obj2 both are equal than it returns 0.

Example: We are sorting student progress according to the student id by using comparable interface.


import java.util.*;
class  Student implements Comparable
{
String name;
int sid;
Student(String name,int sid)
{
this.name=name;
this.sid=sid;
}
public String toString()
{
return name+"......"+sid;
}
public int compareTo(Object ob)
{
int sid1=this.sid;
Student s=(Student)ob;
int sid2=s.sid;
if(sid1<sid2)
{
return -1;
}
else if(sid1>sid2)
{
return 1;
}
else
{
return 0;
}
}
}

class Test
{
public static void main(String[] args)
{
Student s1=new Student("lucky",101);
      Student s2=new Student("deepu",102);
Student s3=new Student("lokesh",103);
Student  s4=new Student("suresh",104);
Student s5=new Student("somank",105);
TreeSet t=new TreeSet();
t.add(s1);
t.add(s2);
t.add(s3);
t.add(s4);
t.add(s5);
System.out.println(t);
}
}

output:

[lucky......101, deepu......102, lokesh......103, suresh......104, somank......105]


Comparator(I):

Comparator interface uses customize sorting by using compare method present inside comparator interface. It contains only one method is compare() method.

Syntax:  public int compare(Object ob1 ,Object ob2)

Example: let's sort the student progress according to the Student name by using comparator interface .


import java.util.*;
class  Student implements Comparable
{
String name;
int sid;
Student(String name,int sid)
{
this.name=name;
this.sid=sid;
}
public String toString()
{
return name+"......"+sid;
}
public int compareTo(Object ob)
{
int sid1=this.sid;
Student s=(Student)ob;
int sid2=s.sid;
if(sid1<sid2)
{
return -1;
}
else if(sid1>sid2)
{
return 1;
}
else
{
return 0;
}
}
}

class Test
{
public static void main(String[] args)
{
Student s1=new Student("lucky",101);
      Student s2=new Student("deepu",102);
Student s3=new Student("lokesh",103);
Student  s4=new Student("suresh",104);
Student s5=new Student("somank",105);
TreeSet t=new TreeSet();
t.add(s1);
t.add(s2);
t.add(s3);
t.add(s4);
t.add(s5);
System.out.println(t);
TreeSet t1=new TreeSet(new MyComparator());
t1.add(s1);
t1.add(s2);
t1.add(s3);
t1.add(s4);
t1.add(s5);
System.out.println(t1);



}
}

class MyComparator implements Comparator
{
public int compare(Object ob1,Object ob2)
{
Student st1=(Student)ob1;
Student st2=(Student)ob2;
String s1=st1.name;
String s2=st2.name;
return s1.compareTo(s2);
}
}


output:
[deepu......102, lokesh......103, lucky......101, somank......105, suresh......104]




No comments:

Post a Comment

Adbox