JPad
Snippets
Download
Help
Contact
Example of creating inner classes and using comparators
Code
// https://projectlombok.org/features/Data.html // @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: // getters for all fields, setters for all non-final fields, and appropriate toString, equals and hashCode @Data public static class Student { private final String name; private final int grade; } public static class GradeComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { // descending order (ascending order would be: o1.getGrade()-o2.getGrade()) return o2.getGrade() - o1.getGrade(); } } public static void main(String args[]) { List<Student> students = new ArrayList<>(); students.add(new Student("Jim", 22)); students.add(new Student("Elizabeth", 19)); students.add(new Student("Bob", 30)); Dump(students, "Order of students before sorting is: "); Collections.sort(students, new GradeComparator()); Dump(students, "Order of students after sorting by student grade is"); Collections.sort(students, (Student r, Student s) -> r.getName().compareTo(s.getName())); Dump(students, "Order of students after sorting by student name is"); }
Result
Console
Order of students before sorting is: => [RunnContainer.Student(name=Jim, grade=22), RunnContainer.Student(name=Elizabeth, grade=19), RunnContainer.Student(name=Bob, grade=30)] Order of students after sorting by student grade is => [RunnContainer.Student(name=Bob, grade=30), RunnContainer.Student(name=Jim, grade=22), RunnContainer.Student(name=Elizabeth, grade=19)] Order of students after sorting by student name is => [RunnContainer.Student(name=Bob, grade=30), RunnContainer.Student(name=Elizabeth, grade=19), RunnContainer.Student(name=Jim, grade=22)]
Bytecode
Compiled from "RunnContainer.java" public class io.jpad.scratch.RunnContainer { public io.jpad.scratch.RunnContainer(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: new #2 // class java/util/ArrayList 3: dup 4: invokespecial #3 // Method java/util/ArrayList."<init>":()V 7: astore_1 8: aload_1 9: new #4 // class io/jpad/scratch/RunnContainer$Student 12: dup 13: ldc #5 // String Jim 15: bipush 22 17: invokespecial #6 // Method io/jpad/scratch/RunnContainer$Student."<init>":(Ljava/lang/String;I)V 20: invokeinterface #7, 2 // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z 25: pop 26: aload_1 27: new #4 // class io/jpad/scratch/RunnContainer$Student 30: dup 31: ldc #8 // String Elizabeth 33: bipush 19 35: invokespecial #6 // Method io/jpad/scratch/RunnContainer$Student."<init>":(Ljava/lang/String;I)V 38: invokeinterface #7, 2 // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z 43: pop 44: aload_1 45: new #4 // class io/jpad/scratch/RunnContainer$Student 48: dup 49: ldc #9 // String Bob 51: bipush 30 53: invokespecial #6 // Method io/jpad/scratch/RunnContainer$Student."<init>":(Ljava/lang/String;I)V 56: invokeinterface #7, 2 // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z 61: pop 62: aload_1 63: ldc #10 // String Order of students before sorting is: 65: invokestatic #11 // Method io/jpad/scratch/Dumper.Dump:(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object; 68: pop 69: aload_1 70: new #12 // class io/jpad/scratch/RunnContainer$GradeComparator 73: dup 74: invokespecial #13 // Method io/jpad/scratch/RunnContainer$GradeComparator."<init>":()V 77: invokestatic #14 // Method java/util/Collections.sort:(Ljava/util/List;Ljava/util/Comparator;)V 80: aload_1 81: ldc #15 // String Order of students after sorting by student grade is 83: invokestatic #11 // Method io/jpad/scratch/Dumper.Dump:(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object; 86: pop 87: aload_1 88: invokedynamic #16, 0 // InvokeDynamic #0:compare:()Ljava/util/Comparator; 93: invokestatic #14 // Method java/util/Collections.sort:(Ljava/util/List;Ljava/util/Comparator;)V 96: aload_1 97: ldc #17 // String Order of students after sorting by student name is 99: invokestatic #11 // Method io/jpad/scratch/Dumper.Dump:(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object; 102: pop 103: return private static int lambda$main$0(io.jpad.scratch.RunnContainer$Student, io.jpad.scratch.RunnContainer$Student); Code: 0: aload_0 1: invokevirtual #18 // Method io/jpad/scratch/RunnContainer$Student.getName:()Ljava/lang/String; 4: aload_1 5: invokevirtual #18 // Method io/jpad/scratch/RunnContainer$Student.getName:()Ljava/lang/String; 8: invokevirtual #19 // Method java/lang/String.compareTo:(Ljava/lang/String;)I 11: ireturn }
Jpad Version:1.04
viewCount:7974
Creator:
admin
Latest Editor:
admin
Tags
comparator
innerclass
lombok
Download
Copy As Markdown:
**code [Example of creating inner classes and using comparators.java](/example/1m/example-of-creating-inner-classes-and-using-comparators)** // https://projectlombok.org/features/Data.html // @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: // getters for all fields, setters for all non-final fields, and appropriate toString, equals and hashCode @Data public static class Student { private final String name; private final int grade; } public static class GradeComparator implements Comparator
{ @Override public int compare(Student o1, Student o2) { // descending order (ascending order would be: o1.getGrade()-o2.getGrade()) return o2.getGrade() - o1.getGrade(); } } public static void main(String args[]) { List
students = new ArrayList<>(); students.add(new Student("Jim", 22)); students.add(new Student("Elizabeth", 19)); students.add(new Student("Bob", 30)); Dump(students, "Order of students before sorting is: "); Collections.sort(students, new GradeComparator()); Dump(students, "Order of students after sorting by student grade is"); Collections.sort(students, (Student r, Student s) -> r.getName().compareTo(s.getName())); Dump(students, "Order of students after sorting by student name is"); } **Result:** Order of students before sorting is: => [RunnContainer.Student(name=Jim, grade=22), RunnContainer.Student(name=Elizabeth, grade=19), RunnContainer.Student(name=Bob, grade=30)] Order of students after sorting by student grade is => [RunnContainer.Student(name=Bob, grade=30), RunnContainer.Student(name=Jim, grade=22), RunnContainer.Student(name=Elizabeth, grade=19)] Order of students after sorting by student name is => [RunnContainer.Student(name=Bob, grade=30), RunnContainer.Student(name=Elizabeth, grade=19), RunnContainer.Student(name=Jim, grade=22)]