4- Java Comparator Interface
Java Comparator Interface
Java Comparator interface is used to order the objects of a user-defined class.
It provides multiple sorting sequences, i.e., you can sort the elements on the basis of any data member, for example, rollno, name, age or anything else.
Image 1 - Methods of Java Comparator Interface
Create a package that have java classes to create a workable code based on concepts of comparator and lamda expression that sort books on the basis of names
Book.java (Model class for code)
package com.java.lamda.example;
public class Book {
private int id;
private String name;
private int pages;
/* * Getter , Setters,toString , AllArgsConstructor , NoArgsConstructor in SpringBoot framework * handled by Lombok Library with annotations like @Data,@Getter,@Setter,etc * Refer to SpringBoot Blog */
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getPages() { return pages; }
public void setPages(int pages) { this.pages = pages; }
@Override public String toString() { return "Book [id=" + id + ", name=" + name + ", pages=" + pages + "]"; }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + pages; return result; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Book other = (Book) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (pages != other.pages) return false; return true; }
public Book() { super(); }
public Book(int id, String name, int pages) { super(); this.id = id; this.name = name; this.pages = pages; }
}
package com.java.lamda.example;
public class Book {
private int id;
private String name;
private int pages;
/*
* Getter , Setters,toString , AllArgsConstructor , NoArgsConstructor in SpringBoot framework
* handled by Lombok Library with annotations like @Data,@Getter,@Setter,etc
* Refer to SpringBoot Blog
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", pages=" + pages + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + pages;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (pages != other.pages)
return false;
return true;
}
public Book() {
super();
}
public Book(int id, String name, int pages) {
super();
this.id = id;
this.name = name;
this.pages = pages;
}
}
BookDAO.java(Data Layer with an array list or Data from database)
package com.java.lamda.example;
import java.util.ArrayList;import java.util.List;
public class BookDAO { public List<Book> getBooks() { List<Book> books = new ArrayList<>(); books.add(new Book(100, "Core Java", 500)); books.add(new Book(101, "ZCore Java", 400)); books.add(new Book(363, "Hibernate", 180)); books.add(new Book(275, "Spring", 200)); books.add(new Book(893, "WebService", 300)); return books; }
}
package com.java.lamda.example;
import java.util.ArrayList;
import java.util.List;
public class BookDAO {
public List<Book> getBooks() {
List<Book> books = new ArrayList<>();
books.add(new Book(100, "Core Java", 500));
books.add(new Book(101, "ZCore Java", 400));
books.add(new Book(363, "Hibernate", 180));
books.add(new Book(275, "Spring", 200));
books.add(new Book(893, "WebService", 300));
return books;
}
}
BookService.java(Service Layer i.e performs functional code)
package com.java.lamda.example;
import java.util.Collections;
import java.util.List;
public class BookService {
public List<Book> getBooksInSortByName() {
List<Book> books = new BookDAO().getBooks();
//Sort to organize in ascending order of name(Format for dictionary)
Collections.sort(books, (o1, o2) -> o1.getName().compareTo(o2.getName()));
return books;
}
public static void main(String[] args) {
System.out.println(new BookService().getBooksInSortByName());
}
}
CONSOLE
[Book [id=100, name=Core Java, pages=500], Book [id=363, name=Hibernate, pages=180], Book [id=275, name=Spring, pages=200], Book [id=893, name=WebService, pages=300], Book [id=101, name=ZCore Java, pages=400]]
Comments
Post a Comment