Created
February 14, 2012 13:28
-
-
Save sifue/1826755 to your computer and use it in GitHub Desktop.
Javaにおける異種コンテナの実装(Effective Java 2ndから) ref: http://qiita.com/items/2488
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 Test | |
{ | |
private Map<Class<?>, Object> contents = new HashMap<Class<?>, Object>(); | |
private <T> T getContent(Class<T> type){ | |
return type.cast(contents.get(type)); | |
} | |
public static void main(String[] args) | |
{ | |
Test test = new Test(); | |
test.contents.put(String.class, "メモですよ"); | |
String newMemo = test.getContent(String.class); | |
System.out.println(newMemo); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment