Skip to content

Instantly share code, notes, and snippets.

@sifue
Created February 14, 2012 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sifue/1826755 to your computer and use it in GitHub Desktop.
Save sifue/1826755 to your computer and use it in GitHub Desktop.
Javaにおける異種コンテナの実装(Effective Java 2ndから) ref: http://qiita.com/items/2488
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