?????????????ò???????Class???????????????????????Context????ò???????Key??????Class??
????public class Context {
????private final Map<Key<?>?? Object> values = new HashMap<>();
????public <T> void put( Key<T> key?? T value ) {
????values.put( key?? value );
????}
????public <T> T get( Key<T> key ) {
????return key.type.cast( values.get( key ) );
????}
????[...]
????}
?????????????????????汾??Context??
????Context context = new Context();
????Runnable runnable1 = ...
????Key<Runnable> key1 = new Key<>( "id1"?? Runnable.class );
????context.put( key1?? runnable1 );
????Runnable runnable2 = ...
????Key<Runnable> key2 = new Key<>( "id2"?? Runnable.class );
????context.put( key2?? runnable2 );
????// several computation cycles later...
????Runnable actual = context.get( key1 );
????assertThat( actual ).isSameAs( runnable1 );
????????????????ο????????????????Context#get?У?Key??????????????????????identifier??class??????????????Key??????????????put???????????get????get??????????null ???????????????……
????//????????????
????Context context = new Context();
????Runnable runnable1 = ...
????Key<Runnable> key1 = new Key<>( "same-id"?? Runnable.class );
????Key<Runnable> key2 = new Key<>( "same-id"?? Runnable.class );
????context.put( key1?? runnable1 );//???????put
????context.get(key2); //?????????get --> return null;
?????????????Key???????equals ??hashCode ??????????????????????HashMap ?????????????????????????key???????????????????????????static import??????????????
????public static  Key key( String identifier?? Class type ) {
????return new Key( identifier?? type );
????}
????????
????“????API?????????????÷????????????????????й???????????????????????????????????????????????????????????????????????????????? ??????????????Class??????????”??Joshua Bloch????Effective Java????29????
?????????????????????????????????????????????????????……