I have method that return Optional
of Book object (do not mind with names as it is just a sample of code). I want to fetch the size of list which is inside Book class:
public class Book {
Integer b;
DateTime dateOfCreation;
Integer a;
List<Integer> bookList;
}
So it goes like this:
public class Main {
public static void main(String[] args) {
Optional<Book> book = getBookObject();
int sizeOfList = book.get().bookList.size();
}
static Optional<Book> getBookObject() {
return Optional.of(new Book());
}
}
I have warning within the get()
which goes like Optional.get() without .ifPresent
what is the proper approach to fetch this size of the list?