Occasionally the average developer runs into a situation where he has to map values of arbitrary types within a particular container. However the Java collection API provides container related parameterization only. Which limits the type safe usage of HashMap for example to a single value type. But what if you want to mix apples and pears?

Luckily there is an easy design pattern that allows to map distinct value types using Java generics, which Joshua Bloch has described as typesafe hetereogeneous container in his book Effective Java (second edition, Item 29).

Stumbling across some not altogether congenial solutions regarding this topic recently, gave me the idea to explain the problem domain and elaborate on some implementation aspects in this post.

Map Distinct Value Types Using Java Generics

Consider for the sake of example that you have to provide some kind of application context that allows to bind values of arbitrary types to certain keys. A simple non type safe implementation using String keys backed by a HashMap might look like this:

public class Context {

  private final Map<String,Object> values = new HashMap<>();

  public void put( String key, Object value ) {
values.put( key, value );
} public Object get( String key ) {
return values.get( key );
} [...]
}

The following snippet shows how this Context can be used in a program:

Context context = new Context();
Runnable runnable = ...
context.put( "key", runnable ); // several computation cycles later...
Runnable value = ( Runnable )context.get( "key" );

The drawback of this approach can be seen at line six where a down cast is needed. Obviously this can lead to a ClassCastException in case the key-value pair has been replaced by a different value type:

Context context = new Context();
Runnable runnable = ...
context.put( "key", runnable ); // several computation cycles later...
Executor executor = ...
context.put( "key", executor ); // even more computation cycles later...
Runnable value = ( Runnable )context.get( "key" ); // runtime problem

The cause of such problems can be difficult to trace as the related implementation steps might be spread wide apart in your application. To improve the situation it seems reasonable to bind the value not only to its key but also to its type.

Common mistakes I saw in several solutions following this approach boil down more or less to the following Context variant:

public class Context {

  private final <String, Object> values = new HashMap<>();

  public <T> void put( String key, T value, Class<T> valueType ) {
values.put( key, value );
} public <T> T get( String key, Class<T> valueType ) {
return ( T )values.get( key );
} [...]
}

Again basic usage might look like this:

Context context = new Context();
Runnable runnable = ...
context.put( "key", runnable, Runnable.class ); // several computation cycles later...
Runnable value = context.get( "key", Runnable.class );

One first glance this code might give the illusion of being more type save as it avoids the down cast in line six. But running the following snippet gets us down to earth as we still run into the ClassCastException scenario during the assignment in line ten:

Context context = new Context();
Runnable runnable = ...
context.put( "key", runnable, Runnable.class ); // several computation cycles later...
Executor executor = ...
context.put( "key", executor, Executor.class ); // even more computation cycles later...
Runnable value = context.get( "key", Runnable.class ); // runtime problem

So what went wrong?

First of all the down cast in Context#get of type T is ineffective as type erasure replaces unbounded parameters with a static cast to Object. But more important the implementation does not use the type information provided by Context#put as key. At most it serves as superfluous cosmetic effect.

Typesafe Hetereogeneous Container

Although the last Context variant did not work out very well it points into the right direction. The question is how to properly parameterize the key? To answer this take a look at a stripped-down implementation according to the typesafe hetereogenous container pattern described by Bloch.

The idea is to use the class type as key itself. Since Class is a parameterized type it enables us to make the methods of Context type safe without resorting to an unchecked cast to T. A Class object used in this fashion is called a type token.

public class Context {

  private final Map<Class<?>, Object> values = new HashMap<>();

  public <T> void put( Class<T> key, T value ) {
values.put( key, value );
} public <T> T get( Class<T> key ) {
return key.cast( values.get( key ) );
} [...]
}

Note how the down cast within the Context#get implementation has been replaced with an effective dynamic variant. And this is how the context can be used by clients:

Context context = new Context();
Runnable runnable ...
context.put( Runnable.class, runnable ); // several computation cycles later...
Executor executor = ...
context.put( Executor.class, executor ); // even more computation cycles later...
Runnable value = context.get( Runnable.class );

This time the client code will work without class cast problems, as it is impossible to exchange a certain key-value pair by one with a different value type.

Bloch mentions two limitations to this pattern. ‘First, a malicious client could easily corrupt the type safety […] by using a class object in its raw form.’ To ensure the type invariant at runtime a dynamic cast can be used within Context#put.

public <T> void put( Class<T> key, T value ) {
values.put( key, key.cast( value ) );
}

The second limitation is that the pattern cannot be used on non-reifiable types (see Item 25, Effective Java). Which means you can store value types like Runnable or Runnable[] but not List<Runnable> in a type safe manner.

This is because there is no particular class object for List<Runnable>. All parameterized types refer to the same List.class object. Hence Bloch points out that there is no satisfactory workaround for this kind of limitation.

But what if you need to store two entries of the same value type? While creating new type extensions just for storage purpose into the type safe container might be imaginable, it does not sound as the best design decision. Using a custom key implementation might be a better approach.

Multiple Container Entries of the Same Type

To be able to store multiple container entries of the same type we could change the Context class to use a custom key. Such a key has to provide the type information we need for the type safe behaviour and an identifier for distinction of the actual value objects.

A naive key implementation using a String instance as identifier might look like this:

public class Key<T> {

  final String identifier;
final Class<T> type; public Key( String identifier, Class<T> type ) {
this.identifier = identifier;
this.type = type;
}
}

Again we use the parameterized Class as hook to the type information. And the adjusted Context now uses the parameterized Key instead of 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 ) );
} [...]
}

A client would use this version of Context like this:

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 );

Although this snippet works, the implementation is still flawed. The Key implementation is used as lookup parameter in Context#get. Using two distinct instances of Key initialized with the same identifier and class – one instance used with put and the other used with get – would return null on get. Which is not what we want.

Luckily this can be solved easily with an appropriate equals and hashCode implementation of Key. That allows the HashMap lookup to work as expected.

具体如何写hashCodeequals参见前一篇 Hash Map

Finally one might provide a factory method for key creation to minimize boilerplate (useful in combination with static imports):

public static  Key key( String identifier, Class type ) {
return new Key( identifier, type );
}

Conclusion

‘The normal use of generics, exemplified by the collection APIs, restricts you to a fixed number of type parameters per container. You can get around this restriction by placing the type parameter on the key rather than the container. You can use Class objects as keys for such typesafe heterogeneous containers’ (Joshua Bloch, Item 29, Effective Java).

Given these closing remarks, there is nothing left to be added except for wishing you good luck mixing apples and pears successfully…

Reference

译文链接

【Java 基础】Java Map中的Value值如何做到可以为任意类型的值的更多相关文章

  1. (转载)Java Map中的Value值如何做到可以为任意类型的值

    转载地址:http://www.importnew.com/15556.html     如有侵权,请联系作者及时删除. 搬到我的博客来,有空细细品味,把玩. 本文由 ImportNew - shut ...

  2. Java基础-Java中23种设计模式之常用的设计模式

    Java基础-Java中23种设计模式之常用的设计模式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.设计模式分类 设计模式是针对特定场景给出的专家级的解决方案.总的来说设 ...

  3. Java基础-JAVA中常见的数据结构介绍

    Java基础-JAVA中常见的数据结构介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是数据结构 答:数据结构是指数据存储的组织方式.大致上分为线性表.栈(Stack) ...

  4. Java基础关于Map(字典)的方法使用

    Java基础关于Map(字典)的方法使用 java中一般用map与hashmap来创建一个key-value对象 使用前提是要导入方法包: import java.util.HashMap: impo ...

  5. java基础(20):Map、可变参数、Collections

    1. Map接口 1.1 Map接口概述 我们通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它们存储数据的形式不同,如下图. Collection中的集合,元素是孤 ...

  6. 怎么在java 8的map中使用stream

    怎么在java 8的map中使用stream 简介 Map是java中非常常用的一个集合类型,我们通常也需要去遍历Map去获取某些值,java 8引入了Stream的概念,那么我们怎么在Map中使用S ...

  7. JAVA基础篇NO2--Java中的基本命名规则及数据类型

    1.Java中的常量及进制 1.常量: 在程序运行的过程中,不可以改变的量,就是常量 boolean类型的值只能是true或者false null: 空常量, 代表不存在! ------------- ...

  8. java基础---->java中正则表达式二

    跟正则表达式相关的类有:Pattern.Matcher和String.今天我们就开始Java中正则表达式的学习. Pattern和Matcher的理解 一.正则表达式的使用方法 一般推荐使用的方式如下 ...

  9. Java基础-Java中的堆内存和离堆内存机制

    Java基础-Java中的堆内存和离堆内存机制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

随机推荐

  1. 深入理解Spring IOC容器及扩展

    本文将从纯xml模式.xml和注解结合.纯注解的方式讲解Spring IOC容器的配置和相关应用. 纯XML模式 实例化Bean的三种方式: 使用无参构造函数 默认情况下,会使用反射调用无参构造函数来 ...

  2. 『学了就忘』Linux软件包管理 — 42、对RPM软件包的查询操作

    目录 1.查询RPM软件包是否安装 2.查询系统中所有已安装的RPM软件包 3.查询RPM软件包的详细信息 4.查询RPM软件包中的文件列表 5.查询系统文件属于哪个RPM包 6.查询RPM软件包所依 ...

  3. Python 常见运算符表达式

    常见运算符表达式    1.算数运算符    2.逻辑运算符    3.比较运算符    4.成员运算符    5.位运算符    6.身份运算符a.赋值运算符 =    格式:变量= 表达式     ...

  4. 中文NER的那些事儿5. Transformer相对位置编码&TENER代码实现

    这一章我们主要关注transformer在序列标注任务上的应用,作为2017年后最热的模型结构之一,在序列标注任务上原生transformer的表现并不尽如人意,效果比bilstm还要差不少,这背后有 ...

  5. ICCV2021 | Vision Transformer中相对位置编码的反思与改进

    ​前言  在计算机视觉中,相对位置编码的有效性还没有得到很好的研究,甚至仍然存在争议,本文分析了相对位置编码中的几个关键因素,提出了一种新的针对2D图像的相对位置编码方法,称为图像RPE(IRPE). ...

  6. 14-2-Unsupervised Learning ----Word Embedding

    Introduction 词嵌入(word embedding)是降维算法(Dimension Reduction)的典型应用 那如何用vector来表示一个word呢? 1-of-N Encodin ...

  7. 关于如何在MyEclipse下修改项目名包名,以及类

    1.修改项目名,右键选择properties->web->web-Context-root修改名称或者直接按F2修改.2,修改包名,右键选择Refactor->rename修改名称即 ...

  8. [hdu7000]二分

    不妨假设$x\le y$,可以通过翻转整个格子序列来调整 令$a_{i}$​​为$i$​​到$y$​​的期望步数,显然有方程$a_{i}=\begin{cases}0&(i=y)\\\frac ...

  9. [bzoj2257]瓶子和燃料

    先考虑选出k个后答案最小会是多少,容易发现其实就是所有的gcd(就是$ax+by=gcd(a,b)$的推广)然后相当于要最大化gcd,反过来可以将所有数的约数都打上+1标记,+1标记不少于k个且最大的 ...

  10. Java培训班4个月有用吗?

    很多想学Java都会经历这样一个选择,是自学还是报班?自学的话需要一步步摸索,从无到有硬啃下来,时间没保证:可如果报班的话,目前市面上五花八门的培训机构又是鱼龙混杂,并且现在越来越多的培训机构宣称&q ...