Guest User

Untitled

a guest
Jan 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. // Java program to demonstrate working of Comparator
  2. // interface
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. // A class to represent a student.
  8. class Student
  9. {
  10. int rollno;
  11. String name, address;
  12.  
  13. // Constructor
  14. public Student(int rollno, String name,
  15. String address)
  16. {
  17. this.rollno = rollno;
  18. this.name = name;
  19. this.address = address;
  20. }
  21.  
  22. // Used to print student details in main()
  23. public String toString()
  24. {
  25. return this.rollno + " " + this.name +
  26. " " + this.address;
  27. }
  28. }
  29.  
  30. class Sortbyroll implements Comparator<Student>
  31. {
  32. // Used for sorting in ascending order of
  33. // roll number
  34. public int compare(Student a, Student b)
  35. {
  36. return a.rollno - b.rollno;
  37. }
  38. }
  39.  
  40. class Sortbyname implements Comparator<Student>
  41. {
  42. // Used for sorting in ascending order of
  43. // roll name
  44. public int compare(Student a, Student b)
  45. {
  46. return a.name.compareTo(b.name);
  47. }
  48. }
  49. class Main
  50. {
  51. public static void main (String[] args)
  52. {
  53. ArrayList<Student> ar = new ArrayList<Student>();
  54. ar.add(new Student(111, "bbbb", "london"));
  55. ar.add(new Student(131, "aaaa", "nyc"));
  56. ar.add(new Student(121, "cccc", "jaipur"));
  57.  
  58. System.out.println("Unsorted");
  59. for (int i=0; i<ar.size(); i++)
  60. System.out.println(ar.get(i));
  61.  
  62. Collections.sort(ar, new Sortbyroll());
  63.  
  64. System.out.println("\nSorted by rollno");
  65. for (int i=0; i<ar.size(); i++)
  66. System.out.println(ar.get(i));
  67.  
  68. Collections.sort(ar, new Sortbyname());
  69.  
  70. System.out.println("\nSorted by name");
  71. for (int i=0; i<ar.size(); i++)
  72. System.out.println(ar.get(i));
  73. }
  74. }
  75. // for more than one field
  76.  
  77. // Java program to demonstrate working of Comparator
  78. // interface more than one field
  79.  
  80. import java.util.ArrayList;
  81. import java.util.Collections;
  82. import java.util.Iterator;
  83. import java.util.List;
  84. import java.util.Comparator;
  85.  
  86. class Student {
  87.  
  88. // instance member variables
  89. String Name;
  90. int Age;
  91.  
  92. // parameterized constructor
  93. public Student(String Name, Integer Age) {
  94. this.Name = Name;
  95. this.Age = Age;
  96. }
  97.  
  98. public String getName() {
  99. return Name;
  100. }
  101.  
  102. public void setName(String Name) {
  103. this.Name = Name;
  104. }
  105.  
  106. public Integer getAge() {
  107. return Age;
  108. }
  109.  
  110. public void setAge(Integer Age) {
  111. this.Age = Age;
  112. }
  113.  
  114. // overriding toString() method
  115. @Override
  116. public String toString() {
  117. return "Customer{" + "Name=" + Name + ", Age=" + Age + '}';
  118. }
  119.  
  120. static class CustomerSortingComparator implements Comparator<Student> {
  121.  
  122. @Override
  123. public int compare(Student customer1, Student customer2) {
  124.  
  125. // for comparison
  126. int NameCompare = customer1.getName().compareTo(customer2.getName());
  127. int AgeCompare = customer1.getAge().compareTo(customer2.getAge());
  128.  
  129. // 2-level comparison using if-else block
  130. if (NameCompare == 0) {
  131. return ((AgeCompare == 0) ? NameCompare : AgeCompare);
  132. } else {
  133. return NameCompare;
  134. }
  135. }
  136. }
  137.  
  138. public static void main(String[] args) {
  139.  
  140. // create ArrayList to store Student
  141. List<Student> al = new ArrayList<>();
  142.  
  143. // create customer objects using constructor initialization
  144. Student obj1 = new Student("Ajay", 27);
  145. Student obj2 = new Student("Sneha", 23);
  146. Student obj3 = new Student("Simran", 37);
  147. Student obj4 = new Student("Ajay", 22);
  148. Student obj5 = new Student("Ajay", 29);
  149. Student obj6 = new Student("Sneha", 22);
  150.  
  151. // add customer objects to ArrayList
  152. al.add(obj1);
  153. al.add(obj2);
  154. al.add(obj3);
  155. al.add(obj4);
  156. al.add(obj5);
  157. al.add(obj6);
  158.  
  159. // before Sorting arraylist: iterate using Iterator
  160. Iterator<Student> custIterator = al.iterator();
  161.  
  162. System.out.println("Before Sorting:\n");
  163. while (custIterator.hasNext()) {
  164. System.out.println(custIterator.next());
  165. }
  166.  
  167. // sorting using Collections.sort(al, comparator);
  168. Collections.sort(al, new CustomerSortingComparator());
  169.  
  170. // after Sorting arraylist: iterate using enhanced for-loop
  171. System.out.println("\n\nAfter Sorting:\n");
  172. for (Student customer : al) {
  173. System.out.println(customer);
  174. }
  175. }
  176. }
Add Comment
Please, Sign In to add comment