使用番石榴变换地图

时间:2014-07-03 06:33:16

标签: java guava

我有一个Group对象,其中包含如下所示的元素列表。

class Group {
    List<E> elements;
    public List<E> getElements() { return elements; }
}

我的Map对象如下所示。

Map<Group, Double> groups = with some values....

我有另一张地图如下。

Map<Element, Double> elementVal = with some values....

我的最终地图应该是

Map<Element, Double> = with some values

其中value是元素组值的乘法(groups map)*元素的值(elementVal map)。

我正在寻找地图转换,最好使用Guava或任何其他更清晰的方式,而不是爆炸地图。

感谢。

1 个答案:

答案 0 :(得分:0)

所以......

带有排环的

版本

    @Test
    public void multiplyMaps() {
        Element e1 = new Element(1.);
        Element e2 = new Element(2.);
        Element e3 = new Element(3.);
        Element e4 = new Element(4.);

        Map elementVal = new HashMap();
        elementVal.put(e1, e1.d);
        elementVal.put(e2, e2.d);
        elementVal.put(e3, e3.d);
        elementVal.put(e4, e4.d);

        Group g12 = new Group(Lists.newArrayList(e1, e2));
        Group g23 = new Group(Lists.newArrayList(e2, e3));
        Group g34 = new Group(Lists.newArrayList(e3, e4));

        Map groups = new HashMap();
        groups.put(g12, e1.d + e2.d);
        groups.put(g23, e2.d + e3.d);
        groups.put(g34, e3.d + e4.d);

        for (final Map.Entry groupEntry : groups.entrySet()) {
            for (Map.Entry elementValEntry : elementVal.entrySet()) {
                System.out.println(groupEntry.getValue() + "*" + elementValEntry.getValue());
            }
        }
    }

然后我们将小番石榴用于内环;)

@Test
public void multiplyMaps2() {
    Element e1 = new Element(1.);
    Element e2 = new Element(2.);
    Element e3 = new Element(3.);
    Element e4 = new Element(4.);

    Map elementVal = new HashMap();
    elementVal.put(e1, e1.d);
    elementVal.put(e2, e2.d);
    elementVal.put(e3, e3.d);
    elementVal.put(e4, e4.d);

    Group g12 = new Group(Lists.newArrayList(e1, e2));
    Group g23 = new Group(Lists.newArrayList(e2, e3));
    Group g34 = new Group(Lists.newArrayList(e3, e4));

    Map groups = new HashMap();
    groups.put(g12, e1.d + e2.d);
    groups.put(g23, e2.d + e3.d);
    groups.put(g34, e3.d + e4.d);

    for (final Map.Entry groupEntry : groups.entrySet()) {

        Collection transform = Collections2.transform(elementVal.entrySet(), new Function, Object>() {
            @Nullable
            @Override
            public String apply(@Nullable Map.Entry elementValEntry) {
                return groupEntry.getValue() + "*" + elementValEntry.getValue();
            }
        });
        System.out.println(transform);
    }
}

最后转换外部循环并使用FluentIterable


    @Test
    public void multiplyMaps3() {
        Element e1 = new Element(1.);
        Element e2 = new Element(2.);
        Element e3 = new Element(3.);
        Element e4 = new Element(4.);

        final Map elementVal = new HashMap();
        elementVal.put(e1, e1.d);
        elementVal.put(e2, e2.d);
        elementVal.put(e3, e3.d);
        elementVal.put(e4, e4.d);

        Group g12 = new Group(Lists.newArrayList(e1, e2));
        Group g23 = new Group(Lists.newArrayList(e2, e3));
        Group g34 = new Group(Lists.newArrayList(e3, e4));

        Map groups = new HashMap();
        groups.put(g12, e1.d + e2.d);
        groups.put(g23, e2.d + e3.d);
        groups.put(g34, e3.d + e4.d);

        Collection> transform = Collections2.transform(groups.entrySet(), new Function, Collection>() {
            @Nullable
            @Override
            public Collection apply(@Nullable final Map.Entry groupEntry) {
                return Collections2.transform(elementVal.entrySet(), new Function, String>() {
                    @Nullable
                    @Override
                    public String apply(@Nullable Map.Entry elementValEntry) {
                        return groupEntry.getValue() + "*" + elementValEntry.getValue();
                    }
                });
            }
        });

        FluentIterable objects = FluentIterable.from(transform).transformAndConcat(new Function, Iterable>() {
            @Nullable
            @Override
            public Iterable apply(@Nullable Collection strings) {
                return strings;
            }
        });
        System.out.println(objects);
    }

最后我们有迭代对象包含乘法结果。

当然,它不能解决您的问题,因为您需要再应用一个转换,该转换会从objects中获取结果并与Element匹配。它并不那么难,因为你应该只将String改为某种Entry(对象对)。