Personal Library(Java project) stuck with error code

java

#1

This is the error:

Note: Library.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

This is my code:

import java.util.HashMap;

public class Library
{
  
  public Library()
  {
    
  }
  
 public void getFinishedBooks(HashMap<String, Boolean> library)
 {
   if(library.size() < 1)
   {
     System.out.println("Error! Hashmap is empty");
   }
   else
   {
     for(String book : library.keySet())
     {
       if(library.containsKey(true))
       {
         System.out.println("This book has been completed" + book);
       }
     }
   }
 }
  
  public static void main(String[] args)
  {
    HashMap myBooks = new HashMap();
    myBooks.put("Road Down the Funneel", true);
    myBooks.put("Rat, A Biology", false);
    myBooks.put("Timeln", true);
    myBooks.put("3D Food Printing", false);
    
    Library myLibrary = new Library();
    myLibrary.getFinishedBooks(myBooks);
  }
  
}

#2

That's not an error, if it's not compiling then it's saying something more as well.

Nonetheless you'll want to google it and/or follow the accompanying suggestion for getting more details.


#3

This isn't an error. It's just a warning. In the future, you can try to find out what the unchecked or unsafe operations are by recompiling using javac -Xlint:unchecked File.java.

In this case, it would appear to be your HashMap that's causing this warning. You're not telling it what types to expect, therefore it's guessing. This is an unchecked/unsafe operation.

Try redefining your HashMap for the values you're providing it like so:

HashMap<String, Boolean> myBooks = new HashMap<String, Boolean>();

This makes sure the HashMap will "check" for String, Boolean key pairs only.

Thanks for your question and best of luck!