Guest User

Untitled

a guest
Dec 10th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.15 KB | None | 0 0
  1. ReportKey
  2. StudentNumber
  3. School
  4.  
  5. Collections.sort(reportList, new Comparator<Report>() {
  6.  
  7. @Override
  8. public int compare(final Report record1, final Report record2) {
  9. return (record1.getReportKey() + record1.getStudentNumber() + record1.getSchool())
  10. .compareTo(record2.getReportKey() + record2.getStudentNumber() + record2.getSchool());
  11. }
  12.  
  13. });
  14.  
  15. @Override
  16. public int compare(final Report record1, final Report record2) {
  17. int c;
  18. c = record1.getReportKey().compareTo(record2.getReportKey());
  19. if (c == 0)
  20. c = record1.getStudentNumber().compareTo(record2.getStudentNumber());
  21. if (c == 0)
  22. c = record1.getSchool().compareTo(record2.getSchool());
  23. return c;
  24. }
  25.  
  26. public class ReportComparator implements Comparator<Report> {
  27. public int compare(Report r1, Report r2) {
  28. return ComparisonChain.start()
  29. .compare(r1.getReportKey(), r2.getReportKey())
  30. .compare(r1.getStudentNumber(), r2.getStudentNumber())
  31. .compare(r1.getSchool(), r2.getSchool())
  32. .result();
  33. }
  34. }
  35.  
  36. Collections.sort(pizzas, new Comparator<Pizza>() {
  37. @Override
  38. public int compare(Pizza p1, Pizza p2) {
  39. int sizeCmp = p1.size.compareTo(p2.size);
  40. if (sizeCmp != 0) {
  41. return sizeCmp;
  42. }
  43. int nrOfToppingsCmp = p1.nrOfToppings.compareTo(p2.nrOfToppings);
  44. if (nrOfToppingsCmp != 0) {
  45. return nrOfToppingsCmp;
  46. }
  47. return p1.name.compareTo(p2.name);
  48. }
  49. });
  50.  
  51. ComparatorChain chain = new ComparatorChain(Arrays.asList(
  52. new BeanComparator("size"),
  53. new BeanComparator("nrOfToppings"),
  54. new BeanComparator("name")));
  55.  
  56. Collections.sort(pizzas, chain);
  57.  
  58. Collections.sort(pizzas, new Comparator<Pizza>() {
  59. @Override
  60. public int compare(Pizza p1, Pizza p2) {
  61. return ComparisonChain.start().compare(p1.size, p2.size).compare(p1.nrOfToppings, p2.nrOfToppings).compare(p1.name, p2.name).result();
  62. // or in case the fields can be null:
  63. /*
  64. return ComparisonChain.start()
  65. .compare(p1.size, p2.size, Ordering.natural().nullsLast())
  66. .compare(p1.nrOfToppings, p2.nrOfToppings, Ordering.natural().nullsLast())
  67. .compare(p1.name, p2.name, Ordering.natural().nullsLast())
  68. .result();
  69. */
  70. }
  71. });
  72.  
  73. Collections.sort(pizzas, new Comparator<Pizza>() {
  74. @Override
  75. public int compare(Pizza p1, Pizza p2) {
  76. return new CompareToBuilder().append(p1.size, p2.size).append(p1.nrOfToppings, p2.nrOfToppings).append(p1.name, p2.name).toComparison();
  77. }
  78. });
  79.  
  80. class MultiComparator<T> implements Comparator<T> {
  81. private final List<Comparator<T>> comparators;
  82.  
  83. public MultiComparator(List<Comparator<? super T>> comparators) {
  84. this.comparators = comparators;
  85. }
  86.  
  87. public MultiComparator(Comparator<? super T>... comparators) {
  88. this(Arrays.asList(comparators));
  89. }
  90.  
  91. public int compare(T o1, T o2) {
  92. for (Comparator<T> c : comparators) {
  93. int result = c.compare(o1, o2);
  94. if (result != 0) {
  95. return result;
  96. }
  97. }
  98. return 0;
  99. }
  100.  
  101. public static <T> void sort(List<T> list, Comparator<? super T>... comparators) {
  102. Collections.sort(list, new MultiComparator<T>(comparators));
  103. }
  104. }
  105.  
  106. Collections.sort(list, ComparatorUtils.chainedComparator(comparators));
  107.  
  108. public class ReportComparator implements Comparator<Report>
  109. {
  110. public int compare(Report r1, Report r2)
  111. {
  112. int result = r1.getReportKey().compareTo(r2.getReportKey());
  113. if (result != 0)
  114. {
  115. return result;
  116. }
  117. result = r1.getStudentNumber().compareTo(r2.getStudentNumber());
  118. if (result != 0)
  119. {
  120. return result;
  121. }
  122. return r1.getSchool().compareTo(r2.getSchool());
  123. }
  124. }
  125.  
  126. import java.util.ArrayList;
  127. import java.util.Collections;
  128. import java.util.Comparator;
  129. import java.util.List;
  130.  
  131. /**
  132. * Compares multiple parts of the Report object.
  133. */
  134. public class SimpleJava8ComparatorClass {
  135.  
  136. public static void main(String[] args) {
  137. List<Report> reportList = new ArrayList<>();
  138. reportList.add(new Report("reportKey2", "studentNumber2", "school1"));
  139. reportList.add(new Report("reportKey4", "studentNumber4", "school6"));
  140. reportList.add(new Report("reportKey1", "studentNumber1", "school1"));
  141. reportList.add(new Report("reportKey3", "studentNumber2", "school4"));
  142. reportList.add(new Report("reportKey2", "studentNumber2", "school3"));
  143.  
  144. System.out.println("pre-sorting");
  145. System.out.println(reportList);
  146. System.out.println();
  147.  
  148. Collections.sort(reportList, Comparator.comparing(Report::getReportKey)
  149. .thenComparing(Report::getStudentNumber)
  150. .thenComparing(Report::getSchool));
  151.  
  152. System.out.println("post-sorting");
  153. System.out.println(reportList);
  154. }
  155.  
  156. private static class Report {
  157.  
  158. private String reportKey;
  159. private String studentNumber;
  160. private String school;
  161.  
  162. public Report(String reportKey, String studentNumber, String school) {
  163. this.reportKey = reportKey;
  164. this.studentNumber = studentNumber;
  165. this.school = school;
  166. }
  167.  
  168. public String getReportKey() {
  169. return reportKey;
  170. }
  171.  
  172. public void setReportKey(String reportKey) {
  173. this.reportKey = reportKey;
  174. }
  175.  
  176. public String getStudentNumber() {
  177. return studentNumber;
  178. }
  179.  
  180. public void setStudentNumber(String studentNumber) {
  181. this.studentNumber = studentNumber;
  182. }
  183.  
  184. public String getSchool() {
  185. return school;
  186. }
  187.  
  188. public void setSchool(String school) {
  189. this.school = school;
  190. }
  191.  
  192. @Override
  193. public String toString() {
  194. return "Report{" +
  195. "reportKey='" + reportKey + ''' +
  196. ", studentNumber='" + studentNumber + ''' +
  197. ", school='" + school + ''' +
  198. '}';
  199. }
  200. }
  201. }
  202.  
  203. package com.java8.chapter1;
  204.  
  205. import java.util.Arrays;
  206. import java.util.Comparator;
  207. import java.util.List;
  208. import static java.util.Comparator.*;
  209.  
  210.  
  211.  
  212. public class Example1 {
  213.  
  214. public static void main(String[] args) {
  215. List<Employee> empList = getEmpList();
  216.  
  217.  
  218. // Before Java 8
  219. empList.sort(new Comparator<Employee>() {
  220.  
  221. @Override
  222. public int compare(Employee o1, Employee o2) {
  223. int res = o1.getDesignation().compareTo(o2.getDesignation());
  224. if (res == 0) {
  225. return o1.getSalary() > o2.getSalary() ? 1 : o1.getSalary() < o2.getSalary() ? -1 : 0;
  226. } else {
  227. return res;
  228. }
  229.  
  230. }
  231. });
  232. for (Employee emp : empList) {
  233. System.out.println(emp);
  234. }
  235. System.out.println("---------------------------------------------------------------------------");
  236.  
  237. // In Java 8
  238.  
  239. empList.sort(comparing(Employee::getDesignation).thenComparing(Employee::getSalary));
  240. empList.stream().forEach(System.out::println);
  241.  
  242. }
  243. private static List<Employee> getEmpList() {
  244. return Arrays.asList(new Employee("Lakshman A", "Consultent", 450000),
  245. new Employee("Chaitra S", "Developer", 250000), new Employee("Manoj PVN", "Developer", 250000),
  246. new Employee("Ramesh R", "Developer", 280000), new Employee("Suresh S", "Developer", 270000),
  247. new Employee("Jaishree", "Opearations HR", 350000));
  248. }
  249. }
  250.  
  251. class Employee {
  252. private String fullName;
  253. private String designation;
  254. private double salary;
  255.  
  256. public Employee(String fullName, String designation, double salary) {
  257. super();
  258. this.fullName = fullName;
  259. this.designation = designation;
  260. this.salary = salary;
  261. }
  262.  
  263. public String getFullName() {
  264. return fullName;
  265. }
  266.  
  267. public String getDesignation() {
  268. return designation;
  269. }
  270.  
  271. public double getSalary() {
  272. return salary;
  273. }
  274.  
  275. @Override
  276. public String toString() {
  277. return "Employee [fullName=" + fullName + ", designation=" + designation + ", salary=" + salary + "]";
  278. }
  279.  
  280. }
  281.  
  282. "2" < "11"
  283.  
  284. "11" < "2"
  285.  
  286. List<Report> reportList = new ArrayList<Report>();
  287. reportList.sort(Comparator.comparing(Report::getRecord1).thenComparing(Report::getRecord2));
  288.  
  289. Comparator<Person> comparator = Comparator.comparingLong(Person::getId)
  290. .thenComparingInt(Person::getAge)
  291. .thenComparing(Person::getName);
  292. personList.sort(comparator);
  293.  
  294. public class Test {
  295.  
  296. public static void main(String[] args) {
  297.  
  298. Collator myCollator;
  299. myCollator = Collator.getInstance(Locale.US);
  300.  
  301. List<Item> items = new ArrayList<Item>();
  302.  
  303. items.add(new Item("costrels", 1039737, ""));
  304. items.add(new Item("Costs", 1570019, ""));
  305. items.add(new Item("costs", 310831, ""));
  306. items.add(new Item("costs", 310832, ""));
  307.  
  308. Collections.sort(items, new Comparator<Item>() {
  309. @Override
  310. public int compare(final Item record1, final Item record2) {
  311. int c;
  312. //c = record1.item1.compareTo(record2.item1); //optional comparison without Collator
  313. c = myCollator.compare(record1.item1, record2.item1);
  314. if (c == 0)
  315. {
  316. return record1.item2 < record2.item2 ? -1
  317. : record1.item2 > record2.item2 ? 1
  318. : 0;
  319. }
  320. return c;
  321. }
  322. });
  323.  
  324. for (Item item : items)
  325. {
  326. System.out.println(item.item1);
  327. System.out.println(item.item2);
  328. }
  329.  
  330. }
  331.  
  332. public static class Item
  333. {
  334. public String item1;
  335. public int item2;
  336. public String item3;
  337.  
  338. public Item(String item1, int item2, String item3)
  339. {
  340. this.item1 = item1;
  341. this.item2 = item2;
  342. this.item3 = item3;
  343. }
  344. }
  345.  
  346. }
Add Comment
Please, Sign In to add comment