Search⌘ K
AI Features

Solution: The Key-Value Config Loader

Explore the implementation of a generic Key-Value Config Loader class in Java. Understand how to use type parameters to ensure type safety and enable the class to handle different data types like String, Integer, and Boolean without type casting.

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());
}
}
...