将Lambda表达式转换为JAVA 7

时间:2018-12-06 20:36:04

标签: java lambda java-8 java-7

我想利用此site给出的工具,该工具本质上能够采用简单的POJO类,并输出生成的junit测试。但是,该代码使用Java 8(需要在Java 7中)。与以下代码段等效的Java 7是什么?主要是需要转换的lambda表达式。

代码:

公共抽象类DtoTest {

/** A map of default mappers for common objects. */
private static final ImmutableMap<Class<?>, Supplier<t>> DEFAULT_MAPPERS;

static {
    final Builder<Class<?>, Supplier<?>> mapperBuilder = ImmutableMap.builder();

    /* Primitives */
    mapperBuilder.put(int.class, () -> 0);
    mapperBuilder.put(double.class, () -> 0.0d);
    mapperBuilder.put(float.class, () -> 0.0f);
    mapperBuilder.put(long.class, () -> 0l);
    mapperBuilder.put(boolean.class, () -> true);
    mapperBuilder.put(short.class, () -> (short) 0);
    mapperBuilder.put(byte.class, () -> (byte) 0);
    mapperBuilder.put(char.class, () -> (char) 0);

    mapperBuilder.put(Integer.class, () -> Integer.valueOf(0));
    mapperBuilder.put(Double.class, () -> Double.valueOf(0.0));
    mapperBuilder.put(Float.class, () -> Float.valueOf(0.0f));
    mapperBuilder.put(Long.class, () -> Long.valueOf(0));
    mapperBuilder.put(Boolean.class, () -> Boolean.TRUE);
    mapperBuilder.put(Short.class, () -> Short.valueOf((short) 0));
    mapperBuilder.put(Byte.class, () -> Byte.valueOf((byte) 0));
    mapperBuilder.put(Character.class, () -> Character.valueOf((char) 0));

    mapperBuilder.put(BigDecimal.class, () -> BigDecimal.ONE);
    mapperBuilder.put(Date.class, () -> new Date());

    /* Collection Types. */
    mapperBuilder.put(Set.class, () -> Collections.emptySet());
    mapperBuilder.put(SortedSet.class, () -> Collections.emptySortedSet());
    mapperBuilder.put(List.class, () -> Collections.emptyList());
    mapperBuilder.put(Map.class, () -> Collections.emptyMap());
    mapperBuilder.put(SortedMap.class, () -> Collections.emptySortedMap());

    DEFAULT_MAPPERS = mapperBuilder.build();
}

1 个答案:

答案 0 :(得分:1)

如果使用的是IntelliJ,则可以使用扩展表达式。 Alt + Enter->展开lambda