Class SimpleDB
java.lang.Object
SimpleDB
SimpleDB is a basic key-value database using a HashMap for in-memory storage.
It supports basic CRUD operations and can persist/load data to/from a file using serialization.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all key-value pairs from the database.booleancontainsKey(String key) Checks if the database contains the given key.voidRemoves the key-value pair for the given key from the database.Retrieves the value associated with the given key.voidload()Loads the database from a file using deserialization.voidpersist()Serializes and saves the database to a file.voidAdds or updates a key-value pair in the database.intsize()Returns the number of key-value pairs in the database.
-
Constructor Details
-
SimpleDB
public SimpleDB()Constructs a new, empty SimpleDB.
-
-
Method Details
-
put
Adds or updates a key-value pair in the database.- Parameters:
key- the key to add or updatevalue- the value to associate with the key
-
get
Retrieves the value associated with the given key.- Parameters:
key- the key to look up- Returns:
- the value associated with the key, or null if not found
-
delete
Removes the key-value pair for the given key from the database.- Parameters:
key- the key to remove
-
containsKey
Checks if the database contains the given key.- Parameters:
key- the key to check- Returns:
- true if the key exists, false otherwise
-
size
public int size()Returns the number of key-value pairs in the database.- Returns:
- the size of the database
-
clear
public void clear()Removes all key-value pairs from the database. -
persist
public void persist()Serializes and saves the database to a file. -
load
public void load()Loads the database from a file using deserialization. Suppresses unchecked cast warning.
-