自: https://blog.csdn.net/mnmlist/article/details/53870520
meta_ws 连接: https://github.com/kse-music/demo/blob/064663ac7251285745dc915dad4a6837f723538f/src/test/java/com/hiekn/demo/test/util/GuavaTest.java
import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import com.google.common.collect.*; import com.google.common.collect.Table.Cell; import com.google.common.eventbus.AllowConcurrentEvents; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.Subscribe; //import com.google.common.graph.*; //import com.hiekn.demo.test.TestBase; import org.junit.Test; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.Callable; import java.util.concurrent.Executors; public class dd { public static void main(String[] args) { List<PersonDb> personDbs = Lists.newArrayList( new PersonDb("a1", 10), new PersonDb("a2", 20), new PersonDb("a3", 30) ); //返回的列表是原有列表的一个转换视图,对原有集合的修改当然会反映到新集合中,transform是单向的,无法向结果列表中添加新元素 List<PersonVo> personVos = Lists.transform(personDbs, new Function<PersonDb, PersonVo>() { @Override public PersonVo apply(PersonDb personDb) { return viewTransformer(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 static PersonVo viewTransformer(PersonDb personDb) { Preconditions.checkNotNull(personDb, "[PersonDbToVo]personDb为null"); PersonVo personVo = new PersonVo(); personVo.setName(personDb.getName() + ",from Db"); personVo.setAge(personDb.getAge()); personVo.setMsg(personDb.getMsg()); return personVo; } static class PersonDb { private String name; private int age; private String msg; public PersonDb(String name, int age){ this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } @Override public String toString() { return MoreObjects.toStringHelper(this) .add("name", name) .add("age", age) .add("msg", msg).toString(); } } static class PersonVo { private String name; private int age; private String msg; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } @Override public String toString() { return MoreObjects.toStringHelper(this) .add("name", name) .add("age", age) .add("msg", msg).toString(); } } }
原文地址:https://www.cnblogs.com/hahajava/p/9653104.html
时间: 2024-10-16 00:10:48