我有一个ImmutableMap< String,ImmutableSet< String>>。是否有一种简单的方法来转动"这个孩子还是我必须自己编码?我想弄清楚最好的观察方式......
{
type1=[FieldA, FieldB, FieldC],
type2=[FieldB, FieldC, FieldD],
type3=[FieldC, FieldD, FieldE]
}
...作为...
{
FieldA=[type1],
FieldB=[type1, type2],
FieldC=[type1, type2, type3],
FieldD=[type1, type2],
FieldE=[type3]
}
MultiMap是否解决了这个问题?
答案 0 :(得分:5)
看起来MultiMap是最佳选择。我正在寻找ImmutableMultiMap#inverse()方法:
ImmutableMultimap.Builder<String, String> builder = new ImmutableMultimap.Builder<String, String>();
builder.putAll( "type1", Arrays.asList( "FieldA", "FieldB", "FieldC") );
builder.putAll( "type2", Arrays.asList( "FieldB", "FieldC", "FieldD") );
builder.putAll( "type3", Arrays.asList( "FieldC", "FieldD", "FieldE") );
ImmutableMultimap<String, String> typesToFields = builder.build();
ImmutableMultimap<String, String> fieldsToTypes = typesToFields.inverse();
System.out.println( "typesToFields: " + typesToFields );
System.out.println( "fieldsToTypes: " + fieldsToTypes );
... ...产生
typesToFields: {type1=[FieldA, FieldB, FieldC], type2=[FieldB, FieldC, FieldD], type3=[FieldC, FieldD, FieldE]}
fieldsToTypes: {FieldA=[type1], FieldB=[type1, type2], FieldC=[type1, type2, type3], FieldD=[type2, type3], FieldE=[type3]}