Guava 转换地图<String, List<Double>> 到列表<TargetObject>
Guava Transform Map<String, List<Double>> to List<TargetObject>
我必须transform/convert将源对象转换为目标对象。请参阅下面使用 Guava 的示例代码。
下面是一个测试 class,其中我有一个源对象 Map<String, List<Double>>
,我需要将其转换为 Target
。
public class TestMapper {
public static void main(String[] args) {
String key = "123AA";
List<Double> values = new ArrayList<Double>();
values.add(15.0);
values.add(3.0);
values.add(1.0);
//source
Map<String, List<Double>> source = new HashMap<String, List<Double>>();
source.put(key, values);
//target
List<TargetObject> target = new ArrayList<>();
//transformation logic
}
}
目标对象:
public class TargetObject
{
private int pivotId;
private int amt1;
private int amt2;
private int amt3;
public int getPivotId() {
return pivotId;
}
public void setPivotId(int pivotId) {
this.pivotId = pivotId;
}
public int getAmt1() {
return amt1;
}
public void setAmt1(int amt1) {
this.amt1 = amt1;
}
public int getAmt2() {
return amt2;
}
public void setAmt2(int amt2) {
this.amt2 = amt2;
}
public int getAmt3() {
return amt3;
}
public void setAmt3(int amt3) {
this.amt3 = amt3;
}
}
你能否建议我是否可以使用 Guava 或任何其他好东西 API?
我想我可以用下面的方式来做...让我知道是否有更好的方法...
Map<Integer, TargetObject> transformEntries =
Maps.transformEntries(source, new EntryTransformer<Integer, List<Integer>, TargetObject>() {
@Override
public TargetObject transformEntry(Integer key, List<Integer> values) {
return new TargetObject(key, values.get(0), values.get(1), values.get(2));
}
});
使用 Guava Lists:
List<TargetObject> resultList = Lists.transform(source.entrySet(),
new Function<Entry<Integer, List<Integer>>, TargetObject>(){...});
我会推荐 Guava FluentIterable 进行此转换,因为它会生成一个不可变的列表,使用起来更容易也更安全:
List<TargetObject> resultList = FluentIterable.from(source.entrySet()).transform(
new Function<Map.Entry<String, List<Double>>, TargetObject>() {
@Override
public TargetObject apply(Map.Entry<String, List<Double>> integerListEntry) {
...
}
}).toList();
我必须transform/convert将源对象转换为目标对象。请参阅下面使用 Guava 的示例代码。
下面是一个测试 class,其中我有一个源对象 Map<String, List<Double>>
,我需要将其转换为 Target
。
public class TestMapper {
public static void main(String[] args) {
String key = "123AA";
List<Double> values = new ArrayList<Double>();
values.add(15.0);
values.add(3.0);
values.add(1.0);
//source
Map<String, List<Double>> source = new HashMap<String, List<Double>>();
source.put(key, values);
//target
List<TargetObject> target = new ArrayList<>();
//transformation logic
}
}
目标对象:
public class TargetObject
{
private int pivotId;
private int amt1;
private int amt2;
private int amt3;
public int getPivotId() {
return pivotId;
}
public void setPivotId(int pivotId) {
this.pivotId = pivotId;
}
public int getAmt1() {
return amt1;
}
public void setAmt1(int amt1) {
this.amt1 = amt1;
}
public int getAmt2() {
return amt2;
}
public void setAmt2(int amt2) {
this.amt2 = amt2;
}
public int getAmt3() {
return amt3;
}
public void setAmt3(int amt3) {
this.amt3 = amt3;
}
}
你能否建议我是否可以使用 Guava 或任何其他好东西 API?
我想我可以用下面的方式来做...让我知道是否有更好的方法...
Map<Integer, TargetObject> transformEntries =
Maps.transformEntries(source, new EntryTransformer<Integer, List<Integer>, TargetObject>() {
@Override
public TargetObject transformEntry(Integer key, List<Integer> values) {
return new TargetObject(key, values.get(0), values.get(1), values.get(2));
}
});
使用 Guava Lists:
List<TargetObject> resultList = Lists.transform(source.entrySet(),
new Function<Entry<Integer, List<Integer>>, TargetObject>(){...});
我会推荐 Guava FluentIterable 进行此转换,因为它会生成一个不可变的列表,使用起来更容易也更安全:
List<TargetObject> resultList = FluentIterable.from(source.entrySet()).transform(
new Function<Map.Entry<String, List<Double>>, TargetObject>() {
@Override
public TargetObject apply(Map.Entry<String, List<Double>> integerListEntry) {
...
}
}).toList();