Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ReportKey
- StudentNumber
- School
- Collections.sort(reportList, new Comparator<Report>() {
- @Override
- public int compare(final Report record1, final Report record2) {
- return (record1.getReportKey() + record1.getStudentNumber() + record1.getSchool())
- .compareTo(record2.getReportKey() + record2.getStudentNumber() + record2.getSchool());
- }
- });
- @Override
- public int compare(final Report record1, final Report record2) {
- int c;
- c = record1.getReportKey().compareTo(record2.getReportKey());
- if (c == 0)
- c = record1.getStudentNumber().compareTo(record2.getStudentNumber());
- if (c == 0)
- c = record1.getSchool().compareTo(record2.getSchool());
- return c;
- }
- public class ReportComparator implements Comparator<Report> {
- public int compare(Report r1, Report r2) {
- return ComparisonChain.start()
- .compare(r1.getReportKey(), r2.getReportKey())
- .compare(r1.getStudentNumber(), r2.getStudentNumber())
- .compare(r1.getSchool(), r2.getSchool())
- .result();
- }
- }
- Collections.sort(pizzas, new Comparator<Pizza>() {
- @Override
- public int compare(Pizza p1, Pizza p2) {
- int sizeCmp = p1.size.compareTo(p2.size);
- if (sizeCmp != 0) {
- return sizeCmp;
- }
- int nrOfToppingsCmp = p1.nrOfToppings.compareTo(p2.nrOfToppings);
- if (nrOfToppingsCmp != 0) {
- return nrOfToppingsCmp;
- }
- return p1.name.compareTo(p2.name);
- }
- });
- ComparatorChain chain = new ComparatorChain(Arrays.asList(
- new BeanComparator("size"),
- new BeanComparator("nrOfToppings"),
- new BeanComparator("name")));
- Collections.sort(pizzas, chain);
- Collections.sort(pizzas, new Comparator<Pizza>() {
- @Override
- public int compare(Pizza p1, Pizza p2) {
- return ComparisonChain.start().compare(p1.size, p2.size).compare(p1.nrOfToppings, p2.nrOfToppings).compare(p1.name, p2.name).result();
- // or in case the fields can be null:
- /*
- return ComparisonChain.start()
- .compare(p1.size, p2.size, Ordering.natural().nullsLast())
- .compare(p1.nrOfToppings, p2.nrOfToppings, Ordering.natural().nullsLast())
- .compare(p1.name, p2.name, Ordering.natural().nullsLast())
- .result();
- */
- }
- });
- Collections.sort(pizzas, new Comparator<Pizza>() {
- @Override
- public int compare(Pizza p1, Pizza p2) {
- return new CompareToBuilder().append(p1.size, p2.size).append(p1.nrOfToppings, p2.nrOfToppings).append(p1.name, p2.name).toComparison();
- }
- });
- class MultiComparator<T> implements Comparator<T> {
- private final List<Comparator<T>> comparators;
- public MultiComparator(List<Comparator<? super T>> comparators) {
- this.comparators = comparators;
- }
- public MultiComparator(Comparator<? super T>... comparators) {
- this(Arrays.asList(comparators));
- }
- public int compare(T o1, T o2) {
- for (Comparator<T> c : comparators) {
- int result = c.compare(o1, o2);
- if (result != 0) {
- return result;
- }
- }
- return 0;
- }
- public static <T> void sort(List<T> list, Comparator<? super T>... comparators) {
- Collections.sort(list, new MultiComparator<T>(comparators));
- }
- }
- Collections.sort(list, ComparatorUtils.chainedComparator(comparators));
- public class ReportComparator implements Comparator<Report>
- {
- public int compare(Report r1, Report r2)
- {
- int result = r1.getReportKey().compareTo(r2.getReportKey());
- if (result != 0)
- {
- return result;
- }
- result = r1.getStudentNumber().compareTo(r2.getStudentNumber());
- if (result != 0)
- {
- return result;
- }
- return r1.getSchool().compareTo(r2.getSchool());
- }
- }
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- /**
- * Compares multiple parts of the Report object.
- */
- public class SimpleJava8ComparatorClass {
- public static void main(String[] args) {
- List<Report> reportList = new ArrayList<>();
- reportList.add(new Report("reportKey2", "studentNumber2", "school1"));
- reportList.add(new Report("reportKey4", "studentNumber4", "school6"));
- reportList.add(new Report("reportKey1", "studentNumber1", "school1"));
- reportList.add(new Report("reportKey3", "studentNumber2", "school4"));
- reportList.add(new Report("reportKey2", "studentNumber2", "school3"));
- System.out.println("pre-sorting");
- System.out.println(reportList);
- System.out.println();
- Collections.sort(reportList, Comparator.comparing(Report::getReportKey)
- .thenComparing(Report::getStudentNumber)
- .thenComparing(Report::getSchool));
- System.out.println("post-sorting");
- System.out.println(reportList);
- }
- private static class Report {
- private String reportKey;
- private String studentNumber;
- private String school;
- public Report(String reportKey, String studentNumber, String school) {
- this.reportKey = reportKey;
- this.studentNumber = studentNumber;
- this.school = school;
- }
- public String getReportKey() {
- return reportKey;
- }
- public void setReportKey(String reportKey) {
- this.reportKey = reportKey;
- }
- public String getStudentNumber() {
- return studentNumber;
- }
- public void setStudentNumber(String studentNumber) {
- this.studentNumber = studentNumber;
- }
- public String getSchool() {
- return school;
- }
- public void setSchool(String school) {
- this.school = school;
- }
- @Override
- public String toString() {
- return "Report{" +
- "reportKey='" + reportKey + ''' +
- ", studentNumber='" + studentNumber + ''' +
- ", school='" + school + ''' +
- '}';
- }
- }
- }
- package com.java8.chapter1;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.List;
- import static java.util.Comparator.*;
- public class Example1 {
- public static void main(String[] args) {
- List<Employee> empList = getEmpList();
- // Before Java 8
- empList.sort(new Comparator<Employee>() {
- @Override
- public int compare(Employee o1, Employee o2) {
- int res = o1.getDesignation().compareTo(o2.getDesignation());
- if (res == 0) {
- return o1.getSalary() > o2.getSalary() ? 1 : o1.getSalary() < o2.getSalary() ? -1 : 0;
- } else {
- return res;
- }
- }
- });
- for (Employee emp : empList) {
- System.out.println(emp);
- }
- System.out.println("---------------------------------------------------------------------------");
- // In Java 8
- empList.sort(comparing(Employee::getDesignation).thenComparing(Employee::getSalary));
- empList.stream().forEach(System.out::println);
- }
- private static List<Employee> getEmpList() {
- return Arrays.asList(new Employee("Lakshman A", "Consultent", 450000),
- new Employee("Chaitra S", "Developer", 250000), new Employee("Manoj PVN", "Developer", 250000),
- new Employee("Ramesh R", "Developer", 280000), new Employee("Suresh S", "Developer", 270000),
- new Employee("Jaishree", "Opearations HR", 350000));
- }
- }
- class Employee {
- private String fullName;
- private String designation;
- private double salary;
- public Employee(String fullName, String designation, double salary) {
- super();
- this.fullName = fullName;
- this.designation = designation;
- this.salary = salary;
- }
- public String getFullName() {
- return fullName;
- }
- public String getDesignation() {
- return designation;
- }
- public double getSalary() {
- return salary;
- }
- @Override
- public String toString() {
- return "Employee [fullName=" + fullName + ", designation=" + designation + ", salary=" + salary + "]";
- }
- }
- "2" < "11"
- "11" < "2"
- List<Report> reportList = new ArrayList<Report>();
- reportList.sort(Comparator.comparing(Report::getRecord1).thenComparing(Report::getRecord2));
- Comparator<Person> comparator = Comparator.comparingLong(Person::getId)
- .thenComparingInt(Person::getAge)
- .thenComparing(Person::getName);
- personList.sort(comparator);
- public class Test {
- public static void main(String[] args) {
- Collator myCollator;
- myCollator = Collator.getInstance(Locale.US);
- List<Item> items = new ArrayList<Item>();
- items.add(new Item("costrels", 1039737, ""));
- items.add(new Item("Costs", 1570019, ""));
- items.add(new Item("costs", 310831, ""));
- items.add(new Item("costs", 310832, ""));
- Collections.sort(items, new Comparator<Item>() {
- @Override
- public int compare(final Item record1, final Item record2) {
- int c;
- //c = record1.item1.compareTo(record2.item1); //optional comparison without Collator
- c = myCollator.compare(record1.item1, record2.item1);
- if (c == 0)
- {
- return record1.item2 < record2.item2 ? -1
- : record1.item2 > record2.item2 ? 1
- : 0;
- }
- return c;
- }
- });
- for (Item item : items)
- {
- System.out.println(item.item1);
- System.out.println(item.item2);
- }
- }
- public static class Item
- {
- public String item1;
- public int item2;
- public String item3;
- public Item(String item1, int item2, String item3)
- {
- this.item1 = item1;
- this.item2 = item2;
- this.item3 = item3;
- }
- }
- }
Add Comment
Please, Sign In to add comment