package TestCollection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
class SetData{
public Integer id;
private String name;
private String city;
private Integer age;
public SetData (Integer id, String name, String city, Integer age) {
this.id = id;
this.name = name;
this.city = city;
this.age = age;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String nity) {
this.city = city;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String toString(){
return "[" + " ID: " + this.getId() + " Name: "+ this.getName() + " City: " + this.getCity() + " Age: " + this.getAge() +"]";
}
}
class Sample2{
public static void main(String args[]){
new Sample2();
Scanner scan = new Scanner(System.in);
SetData setdata1 = new SetData(101,"Aman","Noida",28);
SetData setdata2 = new SetData(119,"Vijay","Delhi",25);
SetData setdata5 = new SetData(204,"chetan","Noida",28);
SetData setdata3 = new SetData(601,"Rajesh","Ghaziabad",27);
SetData setdata4 = new SetData(701,"Brijesh","Noida",26);
List <SetData> l1 = new ArrayList <SetData>();
List l2 = new ArrayList();
l1.add(setdata1);
l1.add(setdata2);
l1.add(setdata3);
l1.add(setdata4);
l1.add(setdata5);
//l2.add(l1);
Iterator itr = l1.iterator();
while(itr.hasNext()){
Object o = itr.next();
//System.out.println(o);
}
Collections.sort(l1,new MyIdComparable1());
itr = l1.iterator();
System.out.println("------------------Sort By ID----------------------");
while(itr.hasNext()){
Object o = itr.next();
System.out.println(o);
}
Collections.sort(l1,new MyNameComparable2());
System.out.println("------------------Sort By Name----------------------");
for(Object name : l1){
System.out.println(name);
}
Collections.sort(l1,new MyCityComparable3());
System.out.println("------------------Sort By City----------------------");
for(Object city : l1){
System.out.println(city);
}
Collections.sort(l1,new MyAgeComparable4());
System.out.println("------------------Sort By Age----------------------");
for(Object age : l1){
System.out.println(age);
}
}
}
class MyIdComparable1 implements Comparator <SetData>{
public int compare(SetData s1, SetData s2){
return (s1.getId()<s2.getId() ? -1 : (s1.getId()==s2.getId() ? 0 : 1));
}
}
class MyNameComparable2 implements Comparator <SetData>{
public int compare(SetData s1, SetData s2){
return (s1.getName().compareToIgnoreCase(s2.getName()));
}
}
class MyCityComparable3 implements Comparator <SetData>{
public int compare(SetData s1, SetData s2){
return (s1.getCity().compareToIgnoreCase(s2.getCity()));
}
}
class MyAgeComparable4 implements Comparator <SetData>{
public int compare(SetData s1, SetData s2){
return (s1.getAge()<s2.getAge() ? -1 : (s1.getAge()==s2.getAge() ? 0 : 1));
}
}
// Out Put :
No comments:
Post a Comment