Project : My personal Library


#1

import java.util.HashMap;
// This library helps to find completion status of the books //
class Library{
public Library() {

}
public void getFinishedBooks(HashMap<String, Boolean> library) {

if (library.size() < 1) {
  System.out.println("The HashMap is empty.");
  
} else {
  for (String book: library.keySet()) {
    if (library.get(book) == true) {
      System.out.println(book);
    }
  }
}

}

public static void main (String args) {
HashMap<String, Boolean> myBooks = new
HashMap<String,Boolean>();

myBooks.put("Road Down The Funnel", true);
myBooks.put("Rat: A Biology",false);
myBooks.put("TimeIn",true);
myBooks.put("3D Food Printing", false);

Library myLibrary = new Library();
myLibrary.getFinishedBooks(myBooks);

}
}

So now my code works but I wonder this: Why the result return to the book " TimeIn" first then second is the book “Road Down The Funnel”. although the book “Road Down The Funnel” was called first on the code.

The same thing happen when I set the condition: if (library.get(book) == false)

The book “3D FootPrinting” appeared first on the box and then on the second line is the book “Rat: A Biology” although the book “Rat: A Biology” was called before 3D FootPrinting.


#2

if we look at the documentation:

https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

we find this in the first paragraph:

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.