Created
November 10, 2015 20:22
-
-
Save Mazyod/1a35d0170c067cda6445 to your computer and use it in GitHub Desktop.
Java Generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
public class Driver { | |
static Map<String, Object> KeyValueStore = new HashMap<String, Object>(); | |
public static void main(String[] args) throws Exception { | |
Driver.KeyValueStore.put("testInt", 5); | |
Driver.KeyValueStore.put("testString", "hello"); | |
String s = getValue(String.class, "testString"); | |
int i = getValue(Integer.class, "testInt"); | |
System.out.printf("%s, %d", s, i); | |
} | |
public static <T> T getValue(Class<T> type, String key) throws ClassCastException { | |
Object value = Driver.KeyValueStore.get(key); | |
return type.cast(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment