首先看段代码吧.
package demo.collection.transform;
import java.util.List;
import com.google.common.collect.Lists;
import demo.util.vo.Person;
public class GuavaList {
public static void main(String[] args) throws Exception {
List<String> names = Lists.newArrayList("Andy", "Bob");
List<Person> persons = Lists.transform(names, name -> new Person(name, 12));
System.out.println(persons);
// 设置所有age到20
for (Person person : persons) {
person.setAge(20);
}
System.out.println(persons);
}
}
Person 有两个属性, Name和Age.
问题来了, 在for循环里把每个person的age改完后, 会打印出什么来呢? 是结果A还是B?
结果A
[Person [name=Andy, age=12], Person [name=Bob, age=12]]
[Person [name=Andy, age=20], Person [name=Bob, age=20]]
[Person [name=Andy, age=20], Person [name=Bob, age=20]]
结果B
[Person [name=Andy, age=12], Person [name=Bob, age=12]]
[Person [name=Andy, age=12], Person [name=Bob, age=12]]
[Person [name=Andy, age=12], Person [name=Bob, age=12]]
正确是结果B. 具体原因看下源码就知道了, transform之后会返回一个TransformingSequentialList. 这个list会让每次被遍历的时候, 都会调用transform的function. 而导致每次生成新的Person.
同时介绍下其他的两种transform
- JDK 8 Stream Transform
- Apache Transform
JDK 8 Stream Transform 可以产生结果A
package demo.collection.transform;
import java.util.List;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import demo.util.vo.Person;
public class JdkStream {
public static void main(String[] args) throws Exception {
List<String> names = Lists.newArrayList("Andy", "Bob");
List<Person> persons = names.stream().map(name -> new Person(name, 12)).collect(Collectors.toList());
System.out.println(persons);
// 设置所有age到20
for (Person person : persons) {
person.setAge(20);
}
System.out.println(persons);
}
}
结果
[Person [name=Andy, age=12], Person [name=Bob, age=12]]
[Person [name=Andy, age=20], Person [name=Bob, age=20]]
[Person [name=Andy, age=20], Person [name=Bob, age=20]]
Apache Transform 可以产生结果A, 但是是改变了原List的内容.
package demo.collection.transform;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import com.google.common.collect.Lists;
import demo.util.vo.Person;
public class ApacheTransform {
public static void main(String[] args) throws Exception {
List<Object> objects = Lists.newArrayList("Andy", "Bob");
CollectionUtils.transform(objects, name -> new Person((String) name, 12));
System.out.println(objects);
// 设置所有age到20
for (Object person : objects) {
((Person) person).setAge(20);
}
System.out.println(objects);
}
}
结果
[Person [name=Andy, age=12], Person [name=Bob, age=12]]
[Person [name=Andy, age=20], Person [name=Bob, age=20]]
[Person [name=Andy, age=20], Person [name=Bob, age=20]]
可以看到之前List里面是String, transform之后List内容变成了Person