importcom.google.common.base.Function;importcom.google.common.base.MoreObjects;importcom.google.common.base.Preconditions;importcom.google.common.cache.Cache;importcom.google.common.cache.CacheBuilder;importcom.google.common.cache.CacheLoader;importcom.google.common.cache.LoadingCache;import com.google.common.collect.*;importcom.google.common.collect.Table.Cell;importcom.google.common.eventbus.AllowConcurrentEvents;importcom.google.common.eventbus.AsyncEventBus;importcom.google.common.eventbus.Subscribe;//import com.google.common.graph.*;//import com.hiekn.demo.test.TestBase;
importorg.junit.Test;import java.util.*;importjava.util.Map.Entry;importjava.util.concurrent.Callable;importjava.util.concurrent.Executors;public classdd {public static voidmain(String[] args) {
List personDbs =Lists.newArrayList(new PersonDb("a1", 10),new PersonDb("a2", 20),new PersonDb("a3", 30)
);//返回的列表是原有列表的一个转换视图,对原有集合的修改当然会反映到新集合中,transform是单向的,无法向结果列表中添加新元素
List personVos = Lists.transform(personDbs, new Function() {
@OverridepublicPersonVo apply(PersonDb personDb) {returnviewTransformer(personDb);
}
});for(PersonDb personDb : personDbs) {
personDb.setMsg("dbs修改!");
}//Collections.shuffle(personVos);//随机打乱原来的顺序,和洗牌一样//personVos = ImmutableList.copyOf(personVos);
personVos =Lists.newArrayList(personVos);//我认为直接循环遍历、转换生成resultList在时间和空间复杂度上会更好。
for(PersonVo personVo : personVos) {
personVo.setMsg("Vos修改!");
}//personVos.add(viewTransformer(new PersonDb("a5", 50)));
personVos.forEach(s -> System.out.println("--2-> "+s));
}public staticPersonVo viewTransformer(PersonDb personDb) {
Preconditions.checkNotNull(personDb,"[PersonDbToVo]personDb为null");
PersonVo personVo= newPersonVo();
personVo.setName(personDb.getName()+ ",from Db");
personVo.setAge(personDb.getAge());
personVo.setMsg(personDb.getMsg());returnpersonVo;
}static classPersonDb {privateString name;private intage;privateString msg;public PersonDb(String name, intage){this.name =name;this.age =age;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getMsg() {returnmsg;
}public voidsetMsg(String msg) {this.msg =msg;
}
@OverridepublicString toString() {return MoreObjects.toStringHelper(this)
.add("name", name)
.add("age", age)
.add("msg", msg).toString();
}
}static classPersonVo {privateString name;private intage;privateString msg;publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getMsg() {returnmsg;
}public voidsetMsg(String msg) {this.msg =msg;
}
@OverridepublicString toString() {return MoreObjects.toStringHelper(this)
.add("name", name)
.add("age", age)
.add("msg", msg).toString();
}
}
}