You are on page 1of 1

import java.util.

*;
class Book {
int id;
String name,author,publisher;
int quantity;
Book() {
}
Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}

// java 8 for each


list.forEach(b-> System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publi
sher+" "+b.quantity));
double endTime = System.nanoTime();
System.out.println("Time taken: " + (endTime-startTime));
long count = 0;
// java 8 forEach with if
/*startTime = System.nanoTime();
for(Book b:list){
if(b.id==0)
count++;
}
System.out.println("Count: " + count);
list.remove(list.size()-1);
endTime = System.nanoTime();
System.out.println("Time taken: " + (endTime-startTime));*/
// java 8 stream operation
startTime = System.nanoTime();
count = list.stream().filter(b -> b.id==0).count();
System.out.println("Count: " + count);
list.remove(list.size()-1);
endTime = System.nanoTime();

You might also like