我有两个对象列表; List<X>
和List<Y>
。 X
和Y
是看起来像的对象:
public class X {
String a;
String b;
String v;
String w;
String m;
String n;
}
public class Y {
String a;
String b;
List<A> aList;
}
public class A {
String v;
String w;
List<B> bList;
}
public class B {
String m;
String n;
}
如何根据规则将List<X>
转换为List<Y>
:
某些字段的值必须相等
例如:
在List<Y>
中,对于一个对象Y,字段a的值必须相等
在Y的字段List<A>
中,对于一个对象A,字段w的值必须相等
在A的字段List<B>
中,对于一个对象B,字段m的值必须相等,等等。
Guava有这种方法Lists#transform,但我不知道如何改造。
还是其他任何方式?
答案 0 :(得分:73)
public static <F,T> List<T> transform(List<F> fromList,
Function<? super F,? extends T> function
您可能希望阅读Lists.transform()和Function的API文档,但基本上转换的调用者提供了一个Function
对象,可将F
转换为T
。
例如,如果您有一个List<Integer> intList
,并且您想创建一个List<String>
,使得后者的每个元素都包含该数字的英语表示(1变成“一个”等)并且您有访问IntToEnglish然后
Function<Integer, String> intToEnglish =
new Function<Integer,String>() {
public String apply(Integer i) { return new IntToEnglish().english_number(i); }
};
List<String> wordsList = Lists.transform(intList, intToEnglish);
是否转换。
您可以应用相同的模式将List<X>
转换为List<Y>
答案 1 :(得分:10)
使用java lambda:
public static <K,V,Q extends K> List<V> transform( final List<Q> input, final java.util.function.Function<K,V> tfunc ) {
if( null == input ) {
return null;
}
return input.stream().map(tfunc).collect( Collectors.toList() );
}
你只需要实现: java.util.function.Function
答案 2 :(得分:6)
这个怎么样?
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
public class GuavaTransform {
public static void main(String[] args) {
List<X> xList = new ArrayList<X>();
xList.add(new X("a", "b", "v", "w", "m", "n"));
xList.add(new X("a1", "b1", "v1", "w1", "m1", "n1"));
for(X elem: xList) {
System.out.println("An instance of X:"+ elem);
}
System.out.println();
List<Y> yList = Lists.transform(xList, new TransformXY());
for(Y elem: yList) {
System.out.println("The corresponding instance of Y: \n"+elem);
}
}
}
class TransformXY implements Function<X, Y> {
@Override
public Y apply(X x) {
List<B> bList = new ArrayList<B>();
bList.add(new B(x.m, x.n));
List<A> aList = new ArrayList<A>();
aList.add(new A(x.v, x.w, bList));
return new Y(x.a, x.b, aList);
}
}
class X {
String a;
String b;
String v;
String w;
String m;
String n;
X(String a, String b, String v, String w, String m, String n) {
super();
this.a = a;
this.b = b;
this.v = v;
this.w = w;
this.m = m;
this.n = n;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(a+",");
sb.append(b+",");
sb.append(v+",");
sb.append(w+",");
sb.append(m+",");
sb.append(n);
sb.append(")");
return sb.toString();
}
}
class Y {
String a;
String b;
List<A> aList;
Y(String a, String b, List<A> aList) {
super();
this.a = a;
this.b = b;
this.aList = aList;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(a+"\n");
sb.append(b+"\n");
for(A elem: aList) {
sb.append(elem+"\n");
}
return sb.toString();
}
}
class A {
String v;
String w;
List<B> bList;
A(String v, String w, List<B> bList) {
super();
this.v = v;
this.w = w;
this.bList = bList;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("--------"+v+"\n");
sb.append("--------"+w+"\n");
for(B elem: bList) {
sb.append(elem+"\n");
}
return sb.toString();
}
}
class B {
String m;
String n;
B(String m, String n) {
super();
this.m = m;
this.n = n;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("----------------"+m+"\n");
sb.append("----------------"+n+"\n");
return sb.toString();
}
}
控制台输出:
An instance of X:(a,b,v,w,m,n)
An instance of X:(a1,b1,v1,w1,m1,n1)
The corresponding instance of Y:
a
b
--------v
--------w
----------------m
----------------n
The corresponding instance of Y:
a1
b1
--------v1
--------w1
----------------m1
----------------n1
答案 3 :(得分:2)
Java 8风格,IntelliJ IDEA帮助了我:
List<X> xList = new ArrayList<>();
List<Y> yList = xList
.stream()
.map(X::getY)
.collect(Collectors.toList());
答案 4 :(得分:1)
与@Isaace相同,但具有lambda语法(从this example获得):
List<X> xList = new ArrayList<>();
List<Y> yList = xList
.stream()
.map(n -> someTransformFunc(n))
.collect(Collectors.toList());
答案 5 :(得分:0)
1.declare泛型方法
public static <TSourse, TResult> void ToList(List<TSourse> list, List<TResult> results) {
if (list.size() > 0) {
for (TSourse obj : list) {
TResult tResult = (TResult) obj;
if (tResult == null) {
throw new AppException("error....");
}
results.add(tResult);
}
}
}
2.调用此方法
List<EntityBase> entityBaseList = new ArrayList<>();
Coach coach = new Coach();
coach.setId("123");
entityBaseList.add(coach);
List<Coach> coachList = new ArrayList<>();
ToList(entityBaseList, coachList);
//will complete List<AObj> to another List<BObj>