首页 > 解决方案 > 将 lambda 表达式转换为 JAVA 7

问题描述

我想利用这个站点提供的工具,这些工具基本上能够采用简单的 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();
}

标签: javalambdajava-8java-7

解决方案


If you're using IntelliJ you can use expand the expressions. Alt + Enter -> Expand lambda


推荐阅读