daily pastebin goal
91%
SHARE
TWEET

Untitled

a guest May 16th, 2018 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.sourceit.hw3;
  2.  
  3. import static java.util.Collections.*;
  4.  
  5. import java.util.Arrays;
  6. import java.util.Comparator;
  7. import java.util.function.Predicate;
  8.  
  9. /**
  10.  * Created by tankomaz on 14.12.13.
  11.  */
  12. public class Lesson5 {
  13.     public static void main(String[] args) {
  14.         // create books
  15.         // add books to array Book[] books
  16.         Book[] books = {};
  17.  
  18.         // HOW TO SORT BY DIFFERENT CONDITIONS via COMPARATOR
  19.         // print books sorted by author
  20.         Book[] sortedBooksByAuthor =
  21.                 Book.sortBooks(books, (book1, book2) -> {
  22.                     return book1.getAuthor().compareToIgnoreCase(book2.getAuthor());
  23.                 });            
  24. //      printBooksToConsole(sortedBooksByAuthor);
  25.        
  26.        
  27.         // ================ filter by author
  28.        
  29.         Book[] filteredBooksByAuthor = Arrays.stream(books)
  30.             .filter((book) -> book.getAuthor().equals("Specific Author"))
  31.             .toArray(Book[]::new);
  32. //      printBooksToConsole(filteredBooksByAuthor);
  33.        
  34.         Book[] filteredBooksByAuthor2 =
  35.                 Book.filterBooks(books, (book) -> book.getAuthor().equals(""));
  36.     }
  37. }
  38.  
  39. class Book {
  40.    
  41.  
  42.     int id;
  43.     String title;
  44.     String author;
  45.     String publishingHouse;
  46.     int dateOfRelease;
  47.     int pages;
  48.     int price;
  49.     String binding;
  50.  
  51.     public static Book[] sortBooks(Book[] books, Comparator<Book> comparator) {
  52.         Book[] resultBooks = Arrays.copyOf(books, books.length);
  53.         Arrays.sort(resultBooks, comparator);
  54.         return resultBooks;
  55.     }
  56.    
  57.     public static Book[] filterBooks(Book[] books, Predicate<Book> predicate) {
  58.         return Arrays.stream(books)
  59.                 .filter(predicate)
  60.                 .toArray(Book[]::new);
  61.     }
  62.    
  63.    
  64.  
  65.    
  66.     public String getAuthor() {
  67.         return this.author;
  68.     }
  69.    
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top