Search⌘ K
AI Features

Solution: The Key-Value Config Loader

Explore how to design a generic key-value configuration loader in Java that enforces type safety. Understand how to create a generic class with type parameters to handle different data types, ensuring clean and reusable code for configuration settings.

We'll cover the following...
Java 25
public class ConfigLoader {
// The generic class definition required by the main method
public static class ConfigEntry<T> {
private String key;
private T value;
public ConfigEntry(String key, T value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public T getValue() {
return value;
}
}
public static void main(String[] args) {
// TEST CODE - DO NOT MODIFY
ConfigEntry<String> title = new ConfigEntry<>("AppTitle", "Dashboard Pro");
ConfigEntry<Integer> timeout = new ConfigEntry<>("Timeout", 3000);
ConfigEntry<Boolean> active = new ConfigEntry<>("IsActive", true);
System.out.println(title.getKey() + " : " + title.getValue());
System.out.println(timeout.getKey() + " : " + timeout.getValue());
System.out.println(active.getKey() + " : " + active.getValue());
}
}
...