à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday, 8 October 2017

Collection Framework in JAVA 6


Collection Framework in JAVA 6

Comparator Interface

In Java, Comparator interface is used to order the object in your own way. It gives you ability to decide how element are stored within sorted collection and map.

Comparator Interface defines compare() method. This method compare two object and return 0 if two object are equal. It returns a positive value if object1 is greater than object2. Otherwise a negative value is return. The method can throw a ClassCastException if the type of object are not compatible for comparison.

Example of Comparator Interface

Student class

class Student
int roll;
String name;
Student(int r,String n)
{
 roll = r;
 name = n;
}
public String toString()
{
 return roll+" "+name;
}

MyComparator class

This class defines the comparison logic for Student class based on their roll. Student object will be sotred in ascending order of their roll.

class MyComparator implements Comparator
{
 public int compare(Student s1,Student s2)
 {
  if(s1.roll == s2.roll) return 0;
  else if(s1.roll > s2.roll) return 1;
  else return -1;
 } 
}

public class Test 
{
 public static void main(String[] args) 
 {
  TreeSet< Student> ts = new TreeSet< Student>(new MyComparator());
  ts.add(new Student(45, "Rahul"));
  ts.add(new Student(11, "Adam"));
  ts.add(new Student(19, "Alex"));
  System.out.println(ts);
 }
}

Output : [ 11 Adam, 19 Alex, 45 Rahul ]

As you can see in the ouput Student object are stored in ascending order of their roll.

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website