[英]Transform Map using Guava
我有一個Group對象,它具有如下元素列表。
class Group {
List<E> elements;
public List<E> getElements() { return elements; }
}
我有地圖對象表示如下。
Map<Group, Double> groups = with some values....
我有另一個地圖如下。
Map<Element, Double> elementVal = with some values....
我的最終地圖應該是
Map<Element, Double> = with some values
其中value是元素的組值( groups
映射)*元素的值( elementVal
映射)的乘積。
我正在尋找地圖轉換,最好是使用Guava
或其他更清潔的方式來實現,而不是分解地圖。
謝謝。
所以...
帶有嵌套循環的版本
@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
(對象對)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.