package lukasz;
import java.util.Objects;
public class Book implements Comparable
<Book
> {
int pages;
this.title = title;
this.author = author;
this.pages = pages;
}
@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + Objects.hashCode(this.title);
hash = 31 * hash + Objects.hashCode(this.author);
hash = 31 * hash + this.pages;
return hash;
}
@Override
public boolean equals
(Object obj
) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
if (this.pages != other.pages) {
return false;
}
if (!Objects.equals(this.title, other.title)) {
return false;
}
if (!Objects.equals(this.author, other.author)) {
return false;
}
return true;
}
@Override
return "Book{" + "title=" + title + ", author=" + author + ", pages=" + pages + '}';
}
@Override
public int compareTo
(Book o
){
int compareTitle = title.compareTo(o.title);
if (compareTitle == 0){
return author.compareTo(o.author);
}
else{
return compareTitle;
}
}
}
-------------------------------------------------------------------------------------------------------
package lukasz;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Start {
public static void main
(String[] args
) {
List<Book> books = new ArrayList<>();
books.
add(new Book("Podstawy języka Java",
"A",
100));
books.
add(new Book("Java dla opornych",
"B",
100));
books.
add(new Book("Podstawy języka Java",
"C",
150));
books.
add(new Book("C# dla początkujących",
"B",
200));
books.
add(new Book("W pustyni i w puszczy",
"Henryk Sienkiewicz",
350));
}
System.
out.
println("\nPosortowane: ");
System.
out.
println(books.
toString());
}
books.sort(new CompareBook());
System.
out.
println("Posortowane wg. stron: ");
System.
out.
println(books.
toString());
books.sort(new CompareBook2());
System.
out.
println("Posorotwane wg. autora malejąco: ");
System.
out.
println(books.
toString());
Iterator<Book> b = books.iterator();
while (b.hasNext()){
if (bo.pages <= 150){
b.remove();
}
}
System.
out.
println("Usunięcie z listy wartości mniejszych niż 150: ");
}
}
---------------------------------------------------------------------------------------------------
package lukasz;
import java.util.Comparator;
public class CompareBook implements Comparator<Book> {
@Override
int a = o2.pages - o1.pages;
if (a == 0){
a = o1.author.compareTo(o2.author);
}
return a;
}
}
------------------------------------------------------------------------------------
package lukasz;
import java.util.Comparator;
public class CompareBook2 implements Comparator<Book> {
@Override
int compareAuthor = o1.author.compareTo(o2.author);
return compareAuthor;
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}