//This class is used to sort a collection in descending order of age and name
//A comparator class is usually written when a programmer usually does not have access to
//source code and then he can create his own custom sort using custom Comparator class as shown in
//Person class below
class Person implements Comparator
{
String name;
int age;
public Person(String name,int age)
{
this.name=name;
this.age=age;
}
public Person()
{
name="";
age=0;
}
public int compare(Person p1,Person p2)
{
if (p2.age>p1.age) return (p2.age-p1.age);
else if (p2.age < p1.age) return (p2.age-p1.age);
else
{
return (p2.name.compareTo(p1.name));
}
}
//Implementing equals method is optional. If implemented to over-ride, make sure the result is consistent with the compare method
public boolean equals(Object o)
{
Person p2 = (Person) o;
if (p2.age>this.age) return (true);
else if (p2.age < this.age) return (false);
else
{
if (p2.name.compareTo(this.name)>= 0)
return true;
else
return false;
}
}
//Over-ride from the object class.
public String toString()
{
return ("Name=" + this.name + ", age=" + this.age);
}
}
//This class sorts a collection in ascending order of age and name and implements comparable.
class PersonComparable implements Comparable
String name;
int age;
public PersonComparable(String name,int age)
{
this.name=name;
this.age=age;
}
public int compareTo(PersonComparable p2)
{
if (this.age>p2.age) return (this.age-p2.age);
else if (this.age < p2.age) return (this.age-p2.age);
else
{
return (this.name.compareTo(p2.name));
}
}
//Implementing equals method is optional. If implemented to over-ride, make sure the result is consistent with the compareTo method
public boolean equals(Object o)
{
PersonComparable p2 = (PersonComparable) o;
if (this.age>p2.age) return (true);
else if (this.age < p2.age) return (false);
else
{
if (this.name.compareTo(p2.name)>= 0)
return true;
else
return false;
}
}
public String toString()
{
return ("Name=" + this.name + ", age=" + this.age);
}
}
public class CollectionsTest01
{
public static void main(String args[])
{
//hashset allows duplicate and null values. This set is created with Primitive class- string
//hash sets are not ordered by default
Set
System.out.println("The original size of the hashset is="+ hs1.size());
hs1.add("temp");
hs1.add(Integer.toString(1));
hs1.add(Boolean.toString(true));
hs1.add("Dushyant");
hs1.add("temp");
hs1.add(null);
System.out.println("The current size of the hashset is="+ hs1.size());
System.out.println("The String hash set is="+hs1);
//hashset allows duplicate and null values. This set is created with Custom class- Person
//hash sets are not ordered by default even if you implement the comparator or comparable interfaces.
HashSet
ps1.add(new Person("Donde Estas",35));
ps1.add(new Person("Ronnie Baron",35));
ps1.add(new Person("Google froogle",31));
ps1.add(new Person("Simple simon",24));
ps1.add(new Person("Greg Bruno",3));
ps1.add(new Person("Ronnie Baron",35));
System.out.println("Iterating through Person hash set");
Iterator it = ps1.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
//When sorting a tree set pass the comparator in the the tree set constructor.
//Tree sets are sorted order sets and apply the compare method
TreeSet
personTreeSet1.add(new Person("Donde Estas",35));
personTreeSet1.add(new Person("Ronnie Baron",35));
personTreeSet1.add(new Person("Google froogle",31));
personTreeSet1.add(new Person("Simple simon",24));
personTreeSet1.add(new Person("Greg Bruno",3));
personTreeSet1.add(new Person("Ronnie Baron",35));
System.out.println("Iterating through Person Tree set");
it = personTreeSet1.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
//Creating a tree set implementing Comparable.
//Tree sets are sorted order sets and apply the comparable interface. Use the compareTo method here to sort.
TreeSet
ts1.add(new PersonComparable("Damini",35));
ts1.add(new PersonComparable("Gamini",35));
ts1.add(new PersonComparable("Kumari",31));
ts1.add(new PersonComparable("Tsunami",24));
ts1.add(new PersonComparable("Kumari",31));
Iterator it2 = ts1.iterator();
System.out.println("The size of the tree set is="+ts1.size());
System.out.println("Iterating through PersonComparable Tree set");
while(it2.hasNext())
{
System.out.println(it2.next());
}
//Example to apply the static collections method sort and reverseOrder. These static methods only apply on lists.
//The array list is created with Comparable constructor.
ArrayList
lst1.add(new PersonComparable("Raj Kapoor",36));
lst1.add(new PersonComparable("Vinod Khanna",39));
lst1.add(new PersonComparable("Dilip Kumar",36));
lst1.add(new PersonComparable("Dilip Kumar",36));
System.out.println("Display the array list before sorting="+lst1);
Collections.sort(lst1);
System.out.println(lst1);
System.out.println("Display the array list after sorting="+lst1);
Collections.sort(lst1,Collections.reverseOrder());
System.out.println("Reverse order sorted list="+lst1);
//Example to apply the static collections method sort and reverseOrder. These static methods only apply on lists.
//The array list is created with Comparator constructor when applied later. In this example Person comparator is applied
ArrayList
lstPerson.add(new Person("Robin Hood",36));
lstPerson.add(new Person("John Peter",39));
lstPerson.add(new Person("Dwight York",36));
lstPerson.add(new Person("Kevin Pieterson",36));
Collections.sort(lstPerson,new Person());
System.out.println("Applying comparator Person to sort the list="+lstPerson);
TreeSet
lst2.add(new PersonComparable("Rajarshi",36));
lst2.add(new PersonComparable("JP",39));
lst2.add(new PersonComparable("Mangesh",36));
lst2.add(new PersonComparable("JP",39));
//Collections.sort(lst2); You cannot apply this Static method on a set. It only applies on a list.
System.out.println(lst2);
ArrayList
c.add(1);c.add(2);
c.add(3);
ArrayList
System.out.println("The current size of the collection is="+ al.size());
Set
set.add("zab");
set.add("one");
set.add("two");
set.add("three");
set.add("four");
set.add("one");
set.add("abc");
set.add(Integer.toString(3));
System.out.println(set);
Set
System.out.println(sortedSet);
}
}
No comments:
Post a Comment